I'm trying to learn code by making 'easy' exercises. I am trying to make a search algorithm using selection sort. When I follow the code inside my head it makes perfect sense, but when I run it, it does not order anything. For the array I am using an integer only array, it consist of random numbers and is of random length.
int currentMin;
int currentMinIndex = 0;
int temp;
for(int i=0;i<array.length-1;i++){
currentMin = array[i];
for(int j=i+1;j<array.length-1;j++){
if(array[j]<currentMin){
currentMinIndex = j;
currentMin = array[j];
}
}
temp = array[currentMinIndex]; //I am aware this could be currentMin
array[currentMinIndex] = array[i];
array[i] = temp;
}
I hope someone will spot my mistake and tell me. (If you have other 'easy' exercises I could do, creativity is appreciated, however this post must stay on topic)
Edit: I just noticed that in some weird way when the array is of large length it sorts but the last one. (array length vary because they are random)
array.length(remove the-1)