C
Clinto
Hello,
I am using the below code to sort an array in ascending order, I also
need to sort in descending order. I cannot get figure out what has to
change to make this happen. If someone could *point me in the right
direction, it would be appreciated. pLast is (comes from another
function) the largest element in the array and paAry is the array
being passed
/* ==================== selectSort ====================
Sorts by selecting smallest element in unsorted
portion of the array and exchanging it with element
at the beginning of the unsorted list.
Pre array must contain at least one item
pLast is pointer to last element in array
Post array rearranged smallest to largest
*/
void selectSort (int* pAry, int* pLast)
{
// Local Declarations
int* pWalker;
int* pSmallest;
// Statements
for (pWalker = pAry; pWalker < pLast; pWalker++)
{
pSmallest = smallest (pWalker, pLast);
exchange (pWalker, pSmallest);
} // for
return;
} // selectSort
/* ==================== smallest ====================
Find smallest element starting at current pointer.
Pre pAry points to first unsorted element
Post smallest element identified and returned
*/
int* smallest (int* pAry, int* pLast)
{
// Local Declarations
int* pLooker;
int* pSmallest;
// Statements
for (pSmallest = pAry, pLooker = pAry + 1;
pLooker <= pLast;
pLooker++)
if (*pLooker < *pSmallest)
pSmallest = pLooker;
return pSmallest;
} // smallest
/* ====================== exchange ====================
Given pointers to two array elements, exchange them
Pre p1 & p2 are pointers to exchange values
Post exchange is completed
*/
void exchange (int* p1, int* p2)
{
// Local Declarations
int temp;
// Statements
temp = *p1;
*p1 = *p2;
*p2 = temp;
return;
} // exchange
I am using the below code to sort an array in ascending order, I also
need to sort in descending order. I cannot get figure out what has to
change to make this happen. If someone could *point me in the right
direction, it would be appreciated. pLast is (comes from another
function) the largest element in the array and paAry is the array
being passed
/* ==================== selectSort ====================
Sorts by selecting smallest element in unsorted
portion of the array and exchanging it with element
at the beginning of the unsorted list.
Pre array must contain at least one item
pLast is pointer to last element in array
Post array rearranged smallest to largest
*/
void selectSort (int* pAry, int* pLast)
{
// Local Declarations
int* pWalker;
int* pSmallest;
// Statements
for (pWalker = pAry; pWalker < pLast; pWalker++)
{
pSmallest = smallest (pWalker, pLast);
exchange (pWalker, pSmallest);
} // for
return;
} // selectSort
/* ==================== smallest ====================
Find smallest element starting at current pointer.
Pre pAry points to first unsorted element
Post smallest element identified and returned
*/
int* smallest (int* pAry, int* pLast)
{
// Local Declarations
int* pLooker;
int* pSmallest;
// Statements
for (pSmallest = pAry, pLooker = pAry + 1;
pLooker <= pLast;
pLooker++)
if (*pLooker < *pSmallest)
pSmallest = pLooker;
return pSmallest;
} // smallest
/* ====================== exchange ====================
Given pointers to two array elements, exchange them
Pre p1 & p2 are pointers to exchange values
Post exchange is completed
*/
void exchange (int* p1, int* p2)
{
// Local Declarations
int temp;
// Statements
temp = *p1;
*p1 = *p2;
*p2 = temp;
return;
} // exchange