Space complexity [space]: O(1), only exchange elements in the original array space.
Sorting method [Space]: inner sorting
Stability [Space]: Unstable (such as [5 a ,8,5 b ,2,9], after the first round of selection and exchange, it is [2,8,5 b ,5 a ,9], two 5s are in order Changed).
The best case [time]: O(n), which occurs when the array itself is ordered, and each element (except the first element) is inserted once more than once.
Worst case [time]: O(n 2 ), which occurs when the array itself is disordered.
Space complexity [space]: O(1), only exchange elements in the original array space.
Sorting method [Space]: inner sorting
Stability [Space]: Stable
Four: Hill sort
Motion picture understanding
Code
functionshellSort(arr){
let gap = Math.floor(arr.length/2);while(gap>=1){
for(let i=gap;i<arr.length;i++){
shellSortHelp(arr,i,gap);}
gap = Math.floor(gap/2);}}// 在当前增量为gap的分组情况下,把索引为i的元素插入至正确位置。functionshellSortHelp(arr,i,gap){
let insert_index = i;let insert_ele = arr[i];// 找到插入位置(间隔为gap)for(let j=i-gap;j>=0;j=j-gap){
// 交换的不是相邻索引处的元素,所以不稳定if(insert_ele<arr[j]){
insert_index = j;}else{
break}}// 腾出插入位置for(let z=i-gap;z>=insert_index;z=z-gap){
arr[z+gap]= arr[z];}
arr[insert_index]= insert_ele;}
Analysis of Algorithms
Average time complexity [time]: O(nlog2 n)
Best case [time]: O(nlog2 n)
Worst case [time]: O(nlog2 n)
Space complexity [space]: O(1), only exchange elements in the original array space.
Sorting method [Space]: inner sorting
Stability [Space]: Unstable (such as [5 a , 5 b , 2, 9], after the first packet exchange is [2 , 5 b , 5 a , 9], the second packet sequence remains unchanged, two The order of 5 changed)
Five: merge sort
Motion picture understanding
Code
functionmergeSort(arr,left,right){
if(left<right){
// 1.分治分组时,索引中值二分let center = Math.floor((left+right)/2)// 左分mergeSort(arr,left,center)// 右分mergeSort(arr,center+1,right)// 左右合一mergeSortHelp(arr,left,right,center)}}functionmergeSortHelp(arr,left,right,center){
let pointer_left = left
let pointer_right = center+1let arr_copy = arr.slice()// 借用到拷贝数组,空间复杂度O(n)for(let i=left;i<=right;i++){
if(pointer_left===center+1){
for(let j=i;j<=right;j++){
arr[j]= arr_copy[pointer_right++]}break}elseif(pointer_right===right+1){
for(let j=i;j<=right;j++){
arr[j]= arr_copy[pointer_left++]}break}// 2.合并比较时,先判断取左边数组元素,结合1判断 归并排序稳定elseif(arr_copy[pointer_left]<=arr_copy[pointer_right]){
arr[i]= arr_copy[pointer_left++]}else{
arr[i]= arr_copy[pointer_right++]}}}
Analysis of Algorithms
Average time complexity [time]: O(nlogn)
Best case [time]: O(nlogn)
Worst case [time]: O(nlogn)
Space complexity [Space]: O(n), copy array will be used.
Stability [Space]: Unstable (the median value of index logic is divided into two)
Seven: Heap sort
Motion picture understanding
Code
// 1.堆排序let len =0// heapSort和heapAdjust方法共享。functionheapSort(arr){
buildMaxHelp(arr)for(let i=arr.length-1;i>0;i--){
swap(arr,0,i)
len--heapAdjust(arr,0)}}// 2.构建大顶堆functionbuildMaxHelp(arr){
len = arr.length
// 堆数组:索引为[0,Math.floor(arr.length)-1]的为非叶子结点。for(let i=Math.floor(arr.length/2)-1;i>=0;i--){
heapAdjust(arr,i)}}// 3.堆调整:调整为以arr[i]为堆顶的堆为大顶堆functionheapAdjust(arr,i){
let left =2*i+1let right =2*i+2let largest = i// 最大元素的索引指针if(right<len && arr[right]>arr[largest]){
// 排序已提出的元素不参与构建堆,此处len不能为arr.length
largest = right
}if(left<len && arr[left]>arr[largest]){
largest = left
}if(largest!=i){
swap(arr,i,largest)heapAdjust(arr,largest)// 堆变化后,递归调整子堆}}// 4.元素交换functionswap(arr,i,j){
let temp = arr[i]
arr[i]= arr[j]
arr[j]= temp
}
Tencent written test questions: unordered and non-repeated numbers, take out the K-th largest number (take out the k-th smallest number with a small top pile).
Average time complexity [time]: O(n+k). (N=arr.length,k=bucket.length)
Best case [time]: O(n+k)
Worst case [time]: O(n+k)
Space complexity [space]: O(k)
Sorting method [space]: outer sorting
Stability [Space]: Stable (counting sorting is not based on element comparison, and stability here does not refer to the above unoptimized counting sorting code).