What can you learn?
① Time complexity ② Space complexity ③ What is algorithm stability ④ Ideas and code implementation of five classic algorithms
Learning sorting algorithm (implemented by js code)
Sorting algorithms are very common in work and learning. Today, the main js implements bubble sort, insertion sort, selection sort, merge sort, and quick sort.
Note:
① Space complexity generally refers to extra space complexity.
② Algorithm stability: Assume that there are multiple records with the same key in the sequence of records to be sorted. If sorted, the relative order of these records remains unchanged. The algorithm is said to be stable.
Bubble Sort
Average time complexity: O(n2) Space complexity O(1) Very stable
function bubbleSort(arr){
for (let i = 0; i < arr.length; i++) {
for (let j = arr.length; j > i; j--) {
if(arr[j]<arr[j-1]){
//两值互换
arr[j]=arr[j]^arr[j-1]
arr[j-1]=arr[j-1]^arr[j]
arr[j]=arr[j]^arr[j-1]
}
}
}
return arr
}
Self-feeling: simple understanding, high time complexity
Insertion sort
Average time complexity: O(n2) Space complexity O(1) Very stable
function insertSort(arr){
for (let index = 1; i < arr.length; index++) {
for (let i = index-1; i >=0&&arr[i]>arr[i+1]; i--) {
arr[i] = arr[i]^arr[j]
arr[j] = arr[i]^arr[j]
arr[i] = arr[i]^arr[j]
}
}
return arr
}
Self-feeling: simple understanding, high complexity, you can refer to the process of playing cards sorting
Select sort
Average time complexity: O(n2) Space complexity O(1) Unstable
function selectSort(arr){
for (let i= 0; i < arr.length-1; i++) {
let min = i
for(let j = i+1;j<arr.length;j++){
if(arr[j]<arr[min]){
min = j
}
}
arr[i] = arr[i]^arr[min]
arr[min]= arr[min]^arr[i]
arr[i] = arr[i]^arr[min]
}
return arr
}
Self-feeling: Every time you complete a cycle, you will find a relative minimum
Merge sort
Average time complexity: O(nlog2n) Space complexity O(n) is very stable
function mergeSort(arr,l,r){
if(l == r){
return [arr[l]]
}
let mid = parseInt((l+r)/2)
let arrleft = mergeSort(arr,l,mid)
let arrRight = mergeSort(arr,mid+1,r)
return merge(arrleft,arrRight)
}
function merge(arrleft,arrRight){
let i = 0
let j = 0
let temp =[]
while(i<arrleft.length||j<arrRight.length){
if(arrleft[i]<arrRight[j]||j>=arrRight.length){
temp.push(arrleft[i])
i++
}else if(arrleft[i]>=arrRight[j]||i>=arrleft.length){
temp.push(arrRight[j])
j++
}
}
return temp
}
Self-feeling: first divide the array continuously, divide it to the smallest unit, and then merge (sort when merging). Use "dual pointers" to add to the new array by comparison.
Quick sort
Average time complexity: O(nlog2n) Space complexity O(nlog2n) unstable
function quickSort(arr,l,r){
if(l==r){
return;
}
let i = l
let j = r
let key = i
while(i<j){
while(i<j&&arr[i]<arr[key]){
i++
}
while(i<j&&arr[j]>=arr[key]){
j--
}
if(i!=j){
arr[i] = arr[i]^arr[j]
arr[j] = arr[j]^arr[i]
arr[i] = arr[i]^arr[j]
i++;
j--
}
}
quickSort(arr,l,i)
quickSort(arr,j+1,r)
}
//简单测试
let arr1 = [8,2,6,3,4,7,9]
quickSort(arr1,0,arr1.length-1)
console.log(arr1)
Self-feeling: Quick sort is based on a certain value of the array, sorting, and then recursively to the smallest unit, the order will come out naturally
Code download address
Have to do it yourself
thank
If you feel helpful to your study work, please share it with those who need it, or like and encourage it, thank you for
your support. You can add a favorite, and I will continue to update it. . .