Tutorial on finding bug in C selection sort implementation
Input parameters (variables):
ais a list of elements and will be modifiednis the length ofaand should be left intact and hence a constant
Local variables, meaning local to scope of function:
current, could be limited to the relevant loop, since it is a loop counterj, could be limited to the relevant loop, since it is a loop counterlowestindex, temporary variable, could be limited to relevant looptemp, temporary variable, could be limited to relevant loop scope, since it is truly temporary
void sort (
int a[],
int n) {
int current, j, lowestindex, temp;
for (current = 0; current < n-1; current++) {
lowestindex = current;
for (j = current+1; j < n; j++) {
if (a[j] < a[current]) {
lowestindex = j;
}
}
if (lowestindex != current) {
temp = a[current];
a[current] = a[lowestindex];
a[lowestindex] = temp;
}
}
}