0

I think i figured out my bubble sort so far. However, if I try to test my implementation with ./find (I added a printf in helpers to get the sorted array) , the IDE doesn't allow me to input more than 3 numbers (those number are sorted but i wanted to try with more) and goes bust with a forth or more. Plus, I see my CPU usage going to 99%.

I'm posting my code in the first place to understand if it is correct (i hope it is)

https://gist.github.com/eb3404b132d75f6c0b4ba34e1bd494c3

this is what happens if i try to input a forth number

https://gyazo.com/69d8e54abdd69e17731f35043414dc34

1

You're causing an infinite loop.

You're not doing correct bubble sort there.

Bubble sort requires two nested loops, not three.

If for example the inner loop used j as its counter, it would compare an element values[j] to the next element values[j+1]. Now you could try all kinds of optimizations on the range and direction of that inner loop (for example "shaker sort" is a bubble sort with alternating direction, or you could track the index of the last swap, or both), but it's always comparing two neighbouring elements.

1
  • HI Blauelf and thank you for pointing this out. Removing one for loop and adjusting the values in the square brackets did the trick. – Alessio Novi Apr 18 '17 at 14:16

You must log in to answer this question.

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