每冒泡一次有序度加1,逆序度減1數組
const bubbleSort = (arr) => { for(let i = 0; i < arr.length; i++){ let isChange = false; for(let j = 0; j < arr.length - i - 1; j++){ if(arr[j] > arr[j+1]){ [arr[j],arr[j + 1]] = [arr[j + 1],arr[j]] isChange = true } } if(!isChange)break } console.log(test) } const test = [4, 5, 6, 3, 2, 1] bubbleSort(test)
無序數組中取一個要插入的元素和有序數組中要插入的元素進行比較,找到要插入的位置ui
const insertionSort = (arr) => { if(arr.length <= 1)return for(let i = 1; i < arr.length; i++){ let insertVal = arr[i] let j = i - 1 for(j; j >= 0; j--){ if(insertVal < arr[j]){ arr[j + 1] = arr[j] }else{ break } } arr[j + 1] = insertVal } console.log(arr) } const testSort = [4, 1, 6, 3, 2, 1] insertionSort(testSort)
每次按順序找到最小的數排在有序組裏,有序組裏的數據再也不比較指針
const selectionSort = (arr) => { if (arr.length <= 1) return for(let i = 0; i < arr.length - 1; i++){ let minIndex = i for(let j = i + 1; j < arr.length; j++){ if(arr[minIndex] > arr[j]){ minIndex = j } } [arr[i],arr[minIndex]] = [arr[minIndex],arr[i]] } console.log(arr) } const testSelect = [4, 8, 6, 3, 2, 1, 0, 12] selectionSort(testSelect)
const mergeSort = (arr) => { if(arr.length <= 1)return arr let mid = Math.floor(arr.length/2) let left = arr.slice(0,mid) let right = arr.slice(mid) return merge(mergeSort(left),mergeSort(right)) } function merge(l,r){ let res = [] let li = 0; let ri = 0; while(li < l.length && ri < r.length){ if(l[li] < r[ri]){ res.push(l[li++]) }else{ res.push(r[ri++]) } } while(li < l.length){ res.push(l[li++]) } while(ri < r.length){ res.push(r[ri++]) } return res }
const heapSort = (arr) => { if(arr.length <= 0) return arr let heapSize = arr.length heapBuild(arr,heapSize) while(heapSize > 1){ heapSize -- [arr[0],arr[heapSize]] = [arr[heapSize],arr[0]] heapIfy(arr,heapSize,0) } return arr } function heapBuild(arr,size){ for(let i = Math.floor(size/2-1); i >= 0; i --){ heapIfy(arr,size,i) } } function heapIfy(arr,size,i){ let left = i * 2 + 1 let right = i * 2 + 2 let largest = i if(left < size && arr[left] > arr[largest]){ largest = left } if(right < size && arr[right] > arr[largest]){ largest = right } if(largest !== i){ [arr[i],arr[largest]] = [arr[largest],arr[i]] heapIfy(arr,size,largest) } } const test = [3,5,1,6,4,7,2] console.log(heapSort(test))
const quickSort = (arr) => { quick(arr,0,arr.length -1) console.log(arr) } const quick = (arr,left,right) => { let index if(arr.length > 1){ index = partition(arr,left,right) if(left < index - 1){ quick(arr,left,index - 1) } if(right > index){ quick(arr,index,right) } } } function partition(arr,left,right){ let pivot = arr[Math.floor((left + right)/2)] let i = left let j = right while(i <= j){ while(arr[i] < pivot){ i++ } while(arr[j] > pivot){ j-- } if(i <= j){ [arr[i],arr[j]] = [arr[j],arr[i]] i++ j-- } } return i } const test = [3,5,1,6,4,7,2] quickSort(test)