I was practicing writing this code and couldn't figure out why it wasn't sorting the ID's. I compared it to what I had written before and saved to a text file and couldn't find any difference, except for the numbers I was using for the ID's. I tried using the numbers that I had used previously, and it worked. It doesn't make any sense. This is the set of ID's that work: 313, 453, 502, 101, 892, 475, 792, 912, 343, 633. I'm using MinGW btw.
Code:
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int ctr, tempID, left, right, didSwap;
float tempBal;
// It won't sort these ID's properly, but when I swap these ID's with the ones above, it sorts those properly.
int custID[10] = {123, 456, 789, 987, 654, 321, 741, 852, 963, 147};
float custBal[10] = {0.00, 98.74, 102.63, 54.26, 326.89, 0.00, 19.95, 220.63, 56.66, 150.55};
for (left = 0; left < 9; left++) {
didSwap = 0;
for (right = left; right < 10; right++) {
if (custID[right] < custID[left]) {
tempID = custID[right];
tempBal = custBal[right];
custID[right] = custID[left];
custBal[right] = custBal[left];
custID[left] = tempID;
custBal[left] = tempBal;
didSwap = 1;
}
}
if (didSwap == 0) {
break;
}
}
for (ctr = 0; ctr < 10; ctr++) {
printf("%d, ", custID[ctr]);
}
return 0;
}