JavaScript Quick Sort

Source: Internet
Author: User

Use the quick sort in the book "JavaScript Data Structures and algorithms" and add your own understanding.

This algorithm has been tested faster than the built-in sort! And the Nanyi of the fast platoon is more like a merge sort, although the writing is much simpler, but the performance is very poor, the array is too big will explode stack.

//principle: Fast sorting also uses the method of splitting, dividing the original array into smaller arrays (but it does not split them apart like a merge sort)//1. Select an intermediate item from the array as the main element//2. Create two pointers, one on the left pointing to the first item in the array, the right one to the last item in the array. Move the left pointer until we find an element that is larger than the primary, and then move the right pointer until you find an element that is smaller than the primary, and then swap them, repeating the process until the left pointer exceeds the right pointer. This process causes the values that are smaller than the primary to be ranked before the primary, and the values larger than the primary are ranked after the main element. This step is called partitioning the operation. //3. Next, the algorithm repeats the previous two steps for the divided decimal group (a sub-array of smaller values than the main element, and a sub-array of values larger than the principal) until the array is fully sorted. Const QuickSort = (arr) = = {    //sorting algorithm. Each time you receive the entire ARR, only left and right are changing. Each sort is simply a sort of the number between left and rightConst QUICK = (arr, left, right) = ={Let index; //index of the segmented array        if(Arr.length > 1) {            //Core algorithm: The two sides of the pointer toward the middle, from the left to find a large number than the main element, the right to find a smaller number than the main element, and then exchange two numberLet mid = Math.floor ((left + right)/2),//Select an intermediary as the main elementMidnum =Arr[mid]; L= left,//Initialize two pointersR =Right ;            Console.log (arr, L, R, Mid, Midnum); //The left pointer is smaller than the right pointer to execute             while(L <=r) {//search from the left to find a number larger than the main element                 while(Arr[l] <midnum) {L++; }                //Search from the right, find a number smaller than the main element                 while(Arr[r] >midnum) {R--; }                //if the index of a number larger than the primary is less than the index of the number smaller than the main element, then the two numbers are exchanged                if(L <=r) {[arr[l], Arr[r]]=[Arr[r], arr[l]]; //exchange complete, move pointerl++; R--; }} Index= L;//when the above execution is complete, the left pointer to the left of the number is a small number, the number on the right is a larger number. We use the left pointer as the index of the segmented array            //the left index is less than the split index, and the left index in the array is sorted in the Split index section            if(Left < Index-1) {Quick (arr, left, index-1); }            //the right index is greater than the split index, and the split index to the right index part of the array is sorted            if(Index <Right )            {Quick (arr, index, right); }        }    }    //call quick to sort, because it is to sort the entire array, so pass in 0 and Arr.length-1Quick (arr, 0, Arr.length-1);} Let arr= []; for(Let i = 0; i < ten; i++) {Arr.push (Math.floor (Math.random ()* 100));} Console.time (); QuickSort (arr);//Arr.sort (arr);Console.timeend ();

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.