# I am trying make a swap in Implement Selection Sort

Tell us what’s happening:
I think that I got the swapping, but I do not know how to set them in the array again

``````
function selectionSort(array) {
let minNumber=0;
let temp=[]
for(let i=0;i<array.length;i++){
minNumber=Math.min(...array)//I find the min Number in the array
if(minNumber!=array[i]){//If they are different
temp[0]=array[i]//I set the index in temp
temp[1]=minNumber//I set the min number in the index
}
array=array.slice(i)// I make the same in every cycle

}
}

selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
``````
``````   **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36`.

Challenge: Implement Selection Sort

What is your goal in this line?

1 Like

I want to repeat the process every time I cut the beginning of the array.

Repeat what process?

1 Like

I want to repeat this, find the min number and swap it with indexNum. But the swapping is wrong.

``````function selectionSort(array) {

for(let i=0;i<array.length-1;i++){

for(let j=i+1;j<array.length;j++){

if(array[i]>array[j]){

let temp=array[i]

array[i]=array[j]

array[j]=temp

}

}

}

return array

console.log(array)

}

selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
``````

I have seen my errors. Thanks for your time, Mr Dawson.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.