[2022-12-26 ๐Ÿ‘ฃ TIL ]

Burkeyยท2022๋…„ 12์›” 26์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
29/157

์˜ค๋Š˜์€ selection sort(์„ ํƒ ์ •๋ ฌ)์„ ์ฝ”๋“œ๋กœ ์งœ ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

function selectSort(arr) {
  for(let i = 0; i < arr.length; i++){
    let temp = arr[i];
    let index = i;
    for (let j = i + 1; j < arr.length - 1; j++){
      if(temp > arr[j]){
        console.log(arr[i], arr[j])
        temp = arr[j]
        index = j
      }
    }
    arr[index] = arr[i];
    arr[i] = temp;
  }
  return arr;
}

console.log(selectSort([4,3,1,2,6])) //[1,2,3,4,6]
profile
์Šคํƒฏ ์˜ฌ๋ฆฌ๋Š” ์ค‘

0๊ฐœ์˜ ๋Œ“๊ธ€