Hello,
This is a Selection Sort exercise. After swapping the current value with the lower value after each pass, I want the code below to print:
Original array: [2,7,4,1,5,3]
1pass: [1,7,4,2,5,3]
2pass: [1,2,4,7,5,3]
3pass: [1,2,3,7,5,4]
4pass: [1,2,3,4,5,7]
var arr = [2,7,4,1,5,3];
for(var i=0; i<=arr.length-2; i++) {
var imin = i;
for(var j =i+1; j<=arr.length-1; j++) {
if(arr[imin] > arr[j]) {
imin = j;
}
}
var temp = arr[i];
arr[i] = arr[imin];
arr[imin] = temp;
console.log(arr)
}
What I have tried:
I hadd created an empty array called store to put the swap values of the minimum value and the maximum value, but I still cant get my original array of 2,7,4,1,5,3 to print the following after each iteration
1pass: [1,7,4,2,5,3]
2pass: [1,2,4,7,5,3]
3pass: [1,2,3,7,5,4]
4pass: [1,2,3,4,5,7]
....