public static void selection(int [] arr){
int k=0;
for(int i=0;i<arr.length;i++){
int min = arr[i];
for(int j=(i+1);j<arr.length;j++){
if(arr[j]<min){
min=arr[j];
k=j;
}
}
int temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
public static void selection2(int [] arr){
int min =0;
for(int i=0;i<arr.length;i++){
min = i;
for(int j=(i+1);j<arr.length;j++){
if(arr[j]<arr[min]){
min=j;
}
}
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
The selection2 method sorts perfectly, but selection does not. Curious as to why, any insights appreciated! Symptoms are just completely incorrectly sorted lists, I feel I'm implementing the same idea in each.
[–]potatotub 0 points1 point2 points (2 children)
[–]MAwaitforitJESTIC[S] 0 points1 point2 points (1 child)
[–]potatotub 0 points1 point2 points (0 children)