JavaScript Quick Sort

Source: Internet
Author: User

Overview

The quick platoon is looking for a base to compare
Put him in front of him bigger than him, put him behind him.
Then repeat this method for both the front and back parts to sort

Plain and easy to understand version
let list = [2, 3, 1, 9, 5, 6, 4, 7, 8];let quick =  function(arr) {    if (arr.length < 1) {        return arr;    }    let base = arr[0], left = [], right = [];    for (let i = 1; i < arr.length; i++) {        if (arr[i] < base) {            left.push(arr[i])        } else {            right.push(arr[i])        }    }    return arguments.callee(left).concat(base, quick(right))}let result = quick(list);console.log(result)

This method is relatively simple, directly create an array, put a smaller than the cardinality of the previous array, large put back array. Then using an array merge is the sorted array. But repeatedly creating many array objects, plus recursion, is a serious waste of performance.

Abstract version
let quickSort = function(arr, startIndex, endIndex) {    if (startIndex > endIndex) {        return;    }    let left = startIndex, right = endIndex, base = list[startIndex];    while (left !== right) {        while (right > left && arr[right] >= base) {            right--;        }        while (left < right && arr[left] <= base) {            left++;        }        if (left < right) {            let temp = arr[left];            arr[left] = arr[right];            arr[right] = temp;        }    }    arr[startIndex] = arr[right];    arr[right] = base;    arguments.callee(arr, startIndex, left - 1);    arguments.callee(arr, left + 1, endIndex)}quickSort(list, 0, list.length - 1);console.log(list)
  • This is not the way to create array collations, but to modify the original array directly, avoiding unnecessary creation of array objects. The downside is that it doesn't seem so clear.
  • First disadvantage a base for comparison (not necessarily the first number)
  • From the end of the array to find a smaller number than the base, to find a stop, at this point to right the right of the first number smaller than the base.
  • From the left of the array to find a larger number than the base, find one to stop, this time left point to the left of the first larger than the number of cardinality.
  • Swap these two numbers, this guarantees the cardinality on both sides, the left side is small, the right side is large.
  • When right with left meet, stop. Because at this time left traversed are smaller than the base, right traversed by the larger than the base, there is no need to look down.
  • rightthe number that allows the pointer to stop is definitely a smaller number than the base, because the cardinality is the first, so the cardinality is to be swapped with a smaller number of positions.
  • Note: left when you stop pointing to a number larger than the base, it is useless to do this number, because the cardinality is already in the first, the left number pointing is definitely behind it, is already the correct situation. If we get the cardinality is a value in the middle, then we have to judge the position of the pointer and the value of the size and then determine where to swap. Since the outer loop ends at the position where right we need to be, we should start from right to left to find small. If we start from left to right and left point to a number that is larger than the base, right left then we meet again, but this point is a number larger than the base, which is obviously not what we need. Starting from the right start is not the same, the right lock is smaller than the base, left hitting right the point of the number is smaller than the base number.
  • In other words: first from the right to find the reason is to find a smaller than the base with the base transposition, the realization of small in front of the cardinality. But this does not guarantee that the base is smaller than his. So also need to find out from the front larger than the base, found on and has found a small number of interchange, can not find is right and left meet, the end of the cycle, the base and right point to the number of transposition.
  • Finally, repeat the above steps for the numbers on both sides of the base.

JavaScript Quick Sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.