Commonly used sorting, quick sorting, merge sorting algorithm explanation

Article directory

There are many kinds of sorting algorithms, and the top ten sorts that are often heard are: bubble sorting, selection sorting, insertion sorting, quick sorting, merge sorting, heap sorting, Hill sorting, counting sorting, radix sorting, and bucket sorting.

Only two commonly used algorithms are introduced here.

Sort by:

  1. quick sort
  2. merge sort

You might be wondering when to use which:

Quick sort usage scenarios : The average time complexity of quick sort is O(n log n), and in the worst case it is O(n²), which is not very stable, but the advantage is that it does not take up extra space, and quick sort is an in-place sort. In practice, quicksort usually outperforms other algorithms with a time complexity of O(n log n),

Merge sort usage scenario : the time complexity is O(n log n). Relatively stable for better or worse. The advantage is that it is stable, but requires additional O(n) space.


quick sort

Idea: Select a datum element, place all elements smaller than the datum to its left, and place all elements larger than the datum to its right. This operation is called "partitioning". Then do the same for the two child elements to the left and right of the pivot, respectively.

Now start the basic steps of quick sorting:

1: data array

insert image description here
2: Select a benchmark

insert image description here

3: Set the left and right borders

insert image description here
4: If the right boundary is less than the reference value, swap the values ​​of the left and right boundaries, then the left boundary i++, then the right boundary j++ (if the loop is not over). otherwise only j++

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

insert image description here
insert image description here
insert image description here

insert image description here
insert image description here

insert image description here

If you don't understand, please read the content of the fourth title carefully: If the right boundary is less than the reference value, then swap the values ​​of the left boundary and the right boundary, then the left boundary i++, then the right boundary j++ (if the loop is not over). otherwise only j++

5: After the loop ends, swap the values ​​​​of the left boundary and the base

insert image description here

6: According to the benchmark, intercept the value of two parts

insert image description here
7: Perform steps 1-6 for the left and right parts respectively

insert image description here

JavaScript implementation code:

function myQuickSort(array, left = 0, right = array.length - 1) {
    
    
    if (left < right) {
    
    
        // 获取分区后枢轴的位置
        let pivotIndex = pivotIndexMeth(array, left, right)
        // 递归调用
        myQuickSort(array, left, pivotIndex - 1)
        // 递归调用
        myQuickSort(array, pivotIndex + 1, right)
    }
    return array
}

function pivotIndexMeth(array, left, right) {
    
    
    // 使用最右边的元素作为枢轴
    let pivot = array[right]
    // 从左边开始
    let i = left;
    // 遍历数组
    for (let j = left; j < right; j++) {
    
    
        // 如果元素小于枢轴,就交换它和当前的“i”元素
        if (array[j] < pivot) {
    
    
            [array[i], array[j]] = [array[j], array[i]]
            i++
        }
    }
    // 最后,将枢轴放置在正确的位置
    [array[i], array[right]] = [array[right], array[i]]
    // 返回枢轴的位置
    return i;
}

console.log(myQuickSort([7, 2, 3, 1, 6, 8, 4, 5]))

merge sort

Idea: Divide a large list into two smaller sublists, then sort the two sublists separately, and finally merge the two sorted sublists into a complete sorted list.

Now start the basic steps of merge sorting:

1: data array

insert image description here

2: Find the center point and divide the data

insert image description here
3: Continue to divide the divided data

insert image description here

4: Continue to divide the divided data

insert image description here
5: After splitting into an array of unit length, perform size comparison sorting

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

JavaScript implementation code:

function myMergeSort(array) {
    
    
    // 获取数组长度
    let len = array.length;
    // 如果数组长度小于2,返回
    if (len < 2) {
    
    
        return array;
    }
    // 获取中间值
    let middle = Math.floor(len / 2)
    // 获取左边的数组
    let left = array.slice(0, middle)
    // 获取右边的数组
    let right = array.slice(middle)
    // 递归调用
    return merge(myMergeSort(left), myMergeSort(right))
}

function merge(left, right) {
    
    
    // 定义一个数组
    let result = [];
    // 如果左边和右边的数组都有值
    while (left.length && right.length) {
    
    
        // 如果左边的第一个值小于右边的第一个值
        if (left[0] < right[0]) {
    
    
            // 将左边的第一个值放入数组
            result.push(left.shift())
        } else {
    
    
            // 将右边的第一个值放入数组
            result.push(right.shift())
        }
    }
    // 如果左边的数组有值
    while (left.length) {
    
    
        result.push(left.shift())
    }
    // 如果右边的数组有值
    while (right.length) {
    
    
        result.push(right.shift())
    }
    return result;
}

console.log(myMergeSort([7, 2, 3, 1, 6, 8, 4, 5]))

If the explanation is wrong or needs to be supplemented, thank you for correcting.

Guess you like

Origin blog.csdn.net/qq_41974199/article/details/131647559