Array in descending order

I wrote a code for sorting an array in descending order, it seems overly complex. I think I should be using a for loop and passing a value as far to the left as possible. I'm not seeing the obvious answer. Should I be using a nested for loop perhaps? I know there are some sort functions when I briefly googled descending order but I'd prefer to write something on my own. Any thoughts to make it simpler?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 void sortArray(int array[], int elements)
{
    int j = 0;
    int temp = 0;
    for (int i = 0; i < elements; i++)
    {
        j = 0;
        while (array[j] >= array[j + 1])
        {
            j++;
            while (array[j] <= array[j + 1] && (j + 1) < elements)
            {
                temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
                j++;
            }
        }
        while (array[j] <= array[j + 1] && (j + 1) < elements)
        {
            temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
            j++;
        }

    }
}


I should also point it out does work! which is nice
Last edited on
Are you required to create your own sorting function? If not, just use std::sort instead:
http://www.cplusplus.com/reference/algorithm/sort/
http://en.cppreference.com/w/cpp/algorithm/sort
Topic archived. No new replies allowed.