Skip to main content
Missed the errors in the sorting (did not even realise the code was there)
Source Link
Blauelf
  • 21k
  • 2
  • 13
  • 22

Use while (left <= right) to loop as long as the search interval has elements. No need for break;.

You compare value value to index middle, but you have to compare value value to middle value values[middle]. You need three cases, one for ==, one for <, one for > (the actions seem correct, though, just the conditions aren't)

You're trying to implement a selection sort. To do so, you have to store the index, not the value.

Your inner loop should start at index i+1, not 0, you don't want to compare to elements you have already sorted.

Make sure to compare only index to index or value to value. So it becomes

int min = i;

and

if (values[min] > values[i2])
{
    min = i2;
}

The swap then has to happen between values[min] and values[i].

The swap should be inside the outer loop, but after the inner loop, and definitely not in the if (values[min] > values[i2]) area.

Likewise, the return should be after the outer loop, not within, otherwise you would return after first swap.

Use while (left <= right) to loop as long as the search interval has elements. No need for break;.

You compare value value to index middle, but you have to compare value value to middle value values[middle]. You need three cases, one for ==, one for <, one for > (the actions seem correct, though, just the conditions aren't)

Use while (left <= right) to loop as long as the search interval has elements. No need for break;.

You compare value value to index middle, but you have to compare value value to middle value values[middle]. You need three cases, one for ==, one for <, one for > (the actions seem correct, though, just the conditions aren't)

You're trying to implement a selection sort. To do so, you have to store the index, not the value.

Your inner loop should start at index i+1, not 0, you don't want to compare to elements you have already sorted.

Make sure to compare only index to index or value to value. So it becomes

int min = i;

and

if (values[min] > values[i2])
{
    min = i2;
}

The swap then has to happen between values[min] and values[i].

The swap should be inside the outer loop, but after the inner loop, and definitely not in the if (values[min] > values[i2]) area.

Likewise, the return should be after the outer loop, not within, otherwise you would return after first swap.

Source Link
Blauelf
  • 21k
  • 2
  • 13
  • 22

Use while (left <= right) to loop as long as the search interval has elements. No need for break;.

You compare value value to index middle, but you have to compare value value to middle value values[middle]. You need three cases, one for ==, one for <, one for > (the actions seem correct, though, just the conditions aren't)