JavaScript implements a fast sorting method for the in-place idea, including javascriptin-place

Source: Internet
Author: User

JavaScript implements a fast sorting method for the in-place idea, including javascriptin-place

Fast sorting, also known as division and exchange sorting. A Fast Sorting Algorithm Based on the divide and conquer method.

This article focuses on using javascript to quickly sort in-place ideas

Divide and conquer law:

In computer science, divide and conquer is an important algorithm paradigm based on multiple branch recursion. The literal explanation is "divide and conquer". It refers to dividing a complicated problem into two or more identical or similar subproblems until the subproblems can be solved directly, the solution of the original problem is the merge of the solutions of the subproblem. (From Wikipedia)

Idea of fast sorting

Specify an element in the array as the ruler. Put an element larger than it after it and put it smaller than it before the element. Repeat until all the elements are arranged in the forward order.

Quick sorting is divided into three steps:

Benchmark selection: select an element in the data structure as the benchmark)

Partition: based on the size of the reference element value, the partition is unordered. All data smaller than the reference element is placed in one interval, and all data greater than the reference element is placed in another interval. After the partition operation is complete, the position of the benchmark element is the position it should be in after the final sorting.

Recursion: for the two unordered intervals first divided, the algorithms in step 1 and Step 2 are recursively called until only one element is left in all unordered intervals.

Now let's talk about the Common Implementation Method (not using the in-situ algorithm)

Function quickSort (arr) {if (arr. length <= 1) return; // obtain the baseline of the digits closest to the center of the array. The values of the odd and even numbers are different, but you are not impressed. Of course, you can select the first one, or the last number is used as the benchmark. Here we do not describe var using tindex = Math. floor (arr. length/2); var partition = arr. splice (tindex, 1) [0]; // left and right intervals, used to store the sorted number var left = []; var right = []; console. log ('benchmark: '+ cursor +'); for (var I = 0; I <arr. length; I ++) {console. log ('subcycle of partition operation '+ (I + 1) + 'subcycle:'); // smaller than the benchmark, placed in the left interval, greater than the benchmark, placed in the right range if (arr [I] <piv Ot) {left. push (arr [I]); console. log ('left: '+ (arr [I])} else {right. push (arr [I]); console. log ('Right: '+ (arr [I])} // use the concat operator to set the left interval, reference, concatenate the right range into a new array // then recursion Step 1 and 2 until all unordered intervals have only one element left and recursively end return quickSort (left ). concat ([activities], quickSort (right);} var arr = [14, 3, 15, 7, 2, 76, 11]; console. log (quickSort (arr);/** when the benchmark is 7, the first partition gets the Left and Right subsets [3, 2,] 7 [14, 15, 76, 11]; * base on 2, sort the subset [3, 2] on the left and obtain [2] 3. The sorting of the Left subset ends. * The right subset is sorted by partition based on the benchmark of 76. The result is [14, 15, 11] 76, 11] The partition is sorted Based on the benchmark of 15. [14, 11] 15 * at this time, the preceding [14, 11] is sorted Based on the benchmark of 11, 11 [14] * All unordered intervals have only one element left and end recursively **/

Through breakpoint debugging, you can get the result.

Disadvantages:

It requires extra storage space of Ω (n), which is as bad as merging and sorting. In the production environment, extra memory space is required, affecting performance.

At the same time, many people think that the above is the real quick sorting. Therefore, it is necessary to recommend the in-place Algorithm for fast sorting.

For more information about in-situ algorithms, see wikipedia. Baidu is similar to Baidu.

In-place

Quick sorting is generally implemented using recursion. The most important part is the partition function, which divides the array into two parts. One part is smaller than the limit and the other part is greater than the limit. I mentioned above about the specific principle

Function quickSort (arr) {// exchange function swap (arr, a, B) {var temp = arr [a]; arr [a] = arr [B]; arr [B] = temp;} // shard function partition (arr, left, right) {/*** I do not know the storage location of the final shard at the beginning, you can first swap the cursor to the back. * here, You can directly define the rightmost element as the benchmark */var cursor = arr [right];/*** when storing elements smaller than the cursor, is next to the previous element. Otherwise, the space may store elements greater than the primary element. * therefore, a storeIndex variable is declared and initialized to left to store elements less than the primary element in sequence. */Var storeIndex = left; for (var I = left; I <right; I ++) {if (arr [I] <strong) {/*** traverses the array, find the element smaller than the limit (the element larger than the limit will be skipped) * place the element obtained during the I cycle to the storeIndex through swap, * and increase the storeIndex by 1, indicates the next location that may be switched */swap (arr, storeIndex, I); storeIndex ++;} // Finally, swap the cursor to the storeIndex, place the reference element to the correct position. swap (arr, right, storeIndex); return storeIndex;} function sort (arr, left, right) {if (left> right) return; var storeIndex = partition (arr, left, right); sort (arr, left, storeIndex-1); sort (arr, storeIndex + 1, right);} sort (arr, storeIndex + 1, right, 0, arr. length-1); return arr;} console. log (quickSort ([8, 4, 90, 8, 34, 67, 1, 26, 17]);

Partition Optimization

Here, careful students may ask whether the performance varies depending on the benchmark. The answer is yes, but because I am working on the front-end, I am not very familiar with algorithms. Therefore, this trap is left to powerful people to fill.

Complexity

Fast sorting is the fastest sorting algorithm, and its time complexity is O (log n)

On average, sort (n log n) times to compare n projects. In the worst case, compare (n2) times.

Https://github.com/LYZ0106/

The above is a quick sorting method for implementing the in-place idea in JavaScript introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner, and I would like to thank you for your support for the help House website!

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.