The Selection Sort coded below does not sort correctly. The numbers 7,66,5,40,33,99 are entered into an array called nums[ ]. When the program is run they do not sort from low to high? Certain sequences will sort, such as 10,9,8,7,6,5 but not all random sequences of six integers.
I have reviewed the code many time but can not solve the problem.
Can some one help me out?
Bruce
int temp = 0;
int[] nums = {7,66,5,40,33,99};
void setup() {
size(1000,1000);
background(255);
textSize(50);
fill(255,0,0);
for(int i = 0; i < nums.length; i++){
text("["+i+"]",100 + 100 * i, 60);
text(nums[i],100 + 100 * i, 120);
}
}
void draw(){
fill(0,0,255);
for (int i = 0; i < nums.length-1; i++) {
// Find the minimum element in unsorted array
int min = i;
for (int j = i+1; j < nums.length; j++) {
if (nums[j] < nums[min]) {
min = j;
// Swap the found minimum element with the first
// element
int temp = nums[min];
nums[min] = nums[i];
nums[i] = temp;
}
}
}
fill(255,0,0);
for(int i = 0; i < nums.length; i++){
text(nums[i], 200+100*i, 780);
text("["+i+"]",200 + 100 * i, 700);
}
noLoop();
}