What’s the role of the statement marked below through a comment in the code fragment to implement Selection sort?
int temp, min;
for (i = 0; i <= count - 2; i++) {
min = i;
for (int j = i + 1; j <= count - 1; j++) {
if (arr[min] > arr[j]) {
if (arr[i] == arr[min]) { //What's the significance of this statement?
temp = arr[min];
arr[min] = arr[j];
arr[j] = temp;
}
}
}
}
What’s the significance of that statement? Will there ever be an input for which this if condition actually matter?
As you have defined min = i in the outer loop, the condition is always satisfied and thus, you can optimize your code by removing the condition.
In selection sort, you can also make more optimisations. Instead of swapping values each time a new minimum is found, you can find the position of the true minimum, say a[pos] and then swap it with the a[i].