Pset3 Sorting-sort. Program doesn't execute through all the code lines

33 views Asked by At

I was rewriting the sort function in helper.c. The program compiled, but upon running, it froze. To simplify the problem, I've separated the sort function into another program with a certain set of ints to be sorted. Still, the execution froze at the sorting loop.

Can anyone help?

#include <cs50.h>
#include <stdio.h>

void sort(int values[], int n);

int main(void)
{
    int randoms[] = {5,486,48,89,78,164,57,54,9};
    sort(randoms, 9);
    for(int i = 0; i < 9; i++)
    {
        printf("%i\n", randoms[i]);
    }
}

void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm
    for(int i = 0, counter; i < n - 1; i++)
    {
        counter = 0;
        for(int j = 0, extra; j < n; i++)
        {
            //swap adjacent values in wrong order
            if (values[j] > values[j+1])
            {
                extra = values[j];
                values[j] = values[j+1];
                values[j+1] = extra;
                counter++;
            }
        }
        if (counter == 0)
        break;
    }
    return;
}
1

There are 1 answers

1
PartlyCloudy On

In your second for loop, you are increasing the wrong variable, it should be j++ instead of i++.