Skip to content

Commit

Permalink
feat: selection sort
Browse files Browse the repository at this point in the history
  • Loading branch information
andy922200 committed Jan 4, 2024
1 parent 0e4ff06 commit d4f3b48
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app-ch5-12.js
@@ -0,0 +1,23 @@
const unsortedArr = [14,-4,17,6,22,1,-5]

// 選擇排序法,以由小到大排序
// O(n^2)
// 1. 從第一個元素開始,找出最小的元素。最後一個元素會是最大的,所以只要取到倒數第二個元素
// 2. 將最小的元素 Index 給保留下來,因為你得跑完整個陣列才能確定最小元素 Index
// 3. 將最小的元素和第 i 項元素交換位置

function selectionSort(arr){
for(let i=0; i<=arr.length-2;i++){
let minIndex = i
for(let j=i; j<=arr.length-1;j++){
if(arr[j]<arr[minIndex]){
minIndex = j
}
}
[arr[i],arr[minIndex]] = [arr[minIndex],arr[i]]
}

return arr
}

console.log('selectionSort Result:',selectionSort(unsortedArr))

0 comments on commit d4f3b48

Please sign in to comment.