0

I have tried to code bubble sort as my sort function, but when I run find it no longer exits when using ctrl d and just gives me a blank terminal line. My code is below, i'd be grateful if someone can help.

void sort(int values[], int n)
{   int counter = 0 ;
char temp ;
do {    for (int i = 0; i < n; i++)
        {if (values[i] > values [i+1])
            {temp = values [i] ;
            values [i] = values [i+1] ;
            values [i+1] = temp ;
            counter++ ;
            }

        }

}
while (counter > 0) ;

// TODO: implement an O(n^2) sorting algorithm
return;
} 
1

If the list is not already sorted, you have an infinite loop. the counter variable will be incremented at some point, meaning that the while condition is true. Since counter is only incremented and never reset to 0, the do/while loop will execute forever.

Perhaps you wanted to set counter to 0 as the first line inside the do/while and before the for loop?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |

You must log in to answer this question.

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