QuickSort by JavaScript

Source: Internet
Author: User

1 functionquickSort (array, left, right) {2             //when the Left,right parameter is not given, the default value3             varleft = left | | 0,4right = right | | Array.length-1;5             if(Left >=Right ) {6                 returnArray;7             }8             //index value, each time the first element of an array9             varindex =Array[left];Ten             //Set Sentinel I and J One             vari =Left , Aj =Right ; -             //follow-up treatment when two sentinels do not meet -              while(I! =j) { the                 //Let Sentinel J start running to the left until it finds a small number of peso values. -                  while(Array[j] >= index && i <j) { -j--; -                 } +                 //Let Sentry I run right until I find a large number of peso values -                  while(Array[i] <= index && i <j) { +i++; A                 } at                 //when Sentinel I is still on the left side of Sentinel J, it is exchanged -                 if(I <j) { -                     vart =Array[i]; -Array[i] =Array[j]; -ARRAY[J] =T; -                 } in             } -             //when Sentinel I and J meet, the index value is exchanged with the Sentinel toArray[left] =Array[i]; +Array[i] =index; -             //recursive invocation, then processing the left and right parts of the index value the             returnQuickSort (array, left, i-1); *             returnQuickSort (Array, i + 1, right); $}

These days pondering the next algorithm, for a long time did not make this thing feel their programming ability is degraded, and took out practice, used to write with C, and now learn JS thinking to take this to achieve the next.

QuickSort by JavaScript

Related Article

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.