What to do if I want it to descending instead of ascending

This is my code for ascending order, but i want to change it to descending. Can anyone give me a hint to execute this problem.

#include <iostream>
using namespace std;

void arrSelectSort(int *[], int);
void showArray(int [], int);
void showArrPtr(int *[], int);

int main ()
{
const int NUMBERS = 10;

int original[NUMBERS] = {1,7,4,0,9,4,8,8,2,4};

int *arrPtr[NUMBERS];
int *nums = original;

for (int count = 0; count < NUMBERS; count++)
arrPtr[count] = &original[count];

arrSelectSort(arrPtr, NUMBERS);

cout << "The numbers, sorted in ascending order are: \n";
showArrPtr(arrPtr, NUMBERS);







while(nums > original)
{
nums--;
cout << *nums << " ";
}




cout <<"The original order are: \n";
showArray(original, NUMBERS);
return 0;
}

void arrSelectSort(int *array[], int size)
{
int startScan, minIndex;
int *minElem;

for (startScan = 0; startScan < (size -1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index ++)
{
if(*(array[index]) <*minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}

void showArray(int array[], int size)
{
for (int count = 0; count < size; count++)
cout << array[count] << " ";
cout << endl;
}

void showArrPtr(int *array[], int size)
{
for (int count = 0; count < size; count++)
cout << *(array[count]) << " ";
cout << endl;
}

if(*(array[index]) <*minElem)
This is your main comparison for the sort. If you change it to
if(*(array[index]) >*minElem) you have descending... :D
You simple check if your number is greater in order to move it up...
Hope this helps
Last edited on
Some problems I'd like to point out:
1. minElem is declared as an int *, but you do minElem = array[startScan] and also array[startScan] = minElem
2. The swap is incorrect. You can't use it like that if minElem is a pointer. I'd replace it with this:
1
2
3
4
5
if (array[minIndex]<array[startScan]){
    int temp=array[minIndex];
    array[minIndex]=array[startScan];
    array[startScan]=temp;
}
Thank you.
Topic archived. No new replies allowed.