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 Answer 1

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. ;-)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.