Fast sorting of rows and merge JS--

JS sorting algorithm of fast and merge row

Quick Sort

Principle: selecting a key (generally the first element), the array is divided into two regions the entire area of ​​the left key or less, all the right is greater than the key in this way is then dividing each area into two areas... The whole process can be implemented recursively, in order to achieve an orderly whole data

  • Time complexity: O (n * log (n))
  • The worst time complexity: O (n ^ 2)
    • Worst: The source array is ascending (descending), need to be laid descending (ascending)
  • Sort unstable
  • Characteristics: the array block, and the left area is smaller than the right (ascending)
  • Unstable reasons: switching element is directly across the element exchange, the exchange may take place adjacent to the location of the same element
  • Performance: The best method of quick sort

Example process:

image.png

function quickSort(ary) {
    let n = ary.length;
        function sort(ary, start, end) {
            if(end <= start) return;
            let i = start,
                j = end,
                key = ary[start]; // 设置第一个元素为key
            while(true) {
                // 从左向右找到大于key的元素位置(大于key循环停止, i就是该元素位置)
                while(ary[++i] < key) {
                    // 到达末尾退出循环
                    if(i === end) break;
                }
                // 从右向左找到小于key的元素位置
                while(ary[--j] > key) {
                    // 到达头部退出循环
                    if(j === start) break;
                }
                // 如果 i和j相交, 直接退出循环
                if(i>=j) break;
                // 交换左右两边元素
                let temp = ary[i];
                ary[i] = ary[j];
                ary[j] = temp;
            }
            // 交换key和最后一个小于key值的元素(就是arr[j])
            let temp = ary[start];
            ary[start] = ary[j];
            ary[j] = temp;
            sort(ary, start, j);
            sort(ary, j+1, end);
        }
    sort(ary, 0, n);
    return ary;
}

Demonstration effect:

Quick Sort .gif

Merge sort

How it works: First array continue to fold into left and right two small array, and then sort the array and merge small

  • Time complexity: O (n * log (n))
  • Stable sorting algorithm
    • Stability reasons: ordering is interchangeable between the two two-element value, a position adjacent to the same element will not be changed
  • Performance: second only to quickly discharge (if using a recursive method, memory condition may occur when handling huge data), faster than the stable discharge, whenever the time complexity is O (n * long (n));
  • Features: Array will continue to be equal to half a tie and then sort-merge between the first cell and orderly, then a large interval interval interval.
  • Optimization: TimeSort Sort

Example process:
Example process of FIG.

    function mergeSort(items) {
        if (items.length == 1) {
            return items;
        }
        //将数组对半平分为左右两个数组
        var middle = Math.floor(items.length / 2),
            left = items.slice(0, middle),
            right = items.slice(middle);

        function merge(left, right) {
            var result = [];
            // 通过这个循环来排序
            while (left.length > 0 && right.length > 0) {
                if (left[0] < right[0]) {
                    /*shift()方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。*/
                    result.push(left.shift());
                } else {
                    result.push(right.shift());
                }
            }
            //合并两个数组
            return result.concat(left).concat(right);
        }

    // 递归调用
    return merge(mergeSort(left), mergeSort(right));
}

Sample effects

Merge sort .gif

gif Source: sorting algorithm - scatter Visualization

Guess you like

Origin www.cnblogs.com/luoshuifushen/p/12542020.html