// TODO: implement an O(n^2) sorting algorithm
int c,i,s;
c = 0;
for (i=0; i < n; i++)
    if (values[i] > values[i + 1])
        s = values[i];
        values[i] = values[i+1];
        values[i] = s;
if ((i = n) && (c > 0))
    i = 0;
    void sort(int values[], int n);

    for (int b = 0; b < n; b++)
        printf("%d ,", values[b]);


It seems this code doesn't do any kind of sort whenever it prints down the values. Can someone please tell me what's wrong? Thanks!


There are problems here. First, it only makes one pass through the array, so all it will do is push the largest value to the end. The rest of the list will likely not sort. Next, since i can increase to n-1, there's a problem. If i = n-1, then i+1 = n, which is beyond the end of the array. This is an error, but it usually won't report as an error. Instead, it will usually end up pushing the largest value past the end of the array and grabbing whatever data is there and pulling it into the last element in the array - usually a 0.

The c variable will record the number of swaps made, but nothing is ever done with it, so it can be removed.

There is a signature line for sort() in the middle of this code. This makes no sense to me, unless maybe it is an attempt to do a recursive call.

There are probably more issues, but this should get you back on track. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thank you for your help!
    – Thunder
    Jan 14 '17 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .