_javascript Techniques for sorting algorithm code in JavaScript

Source: Internet
Author: User
The data items that are sorted by are called "Sort Codes", which is also the key code for the data element. To make it easier to find, you usually want the data tables in your computer to be sorted by key code. such as ordered table binary lookup, search efficiency is high. Also, the process of constructing binary sort trees, B-trees, and plus + trees is a sort process. If the key code is the primary key, then for any ordered sequence, the results obtained after sorting are unique, and if the key code is the secondary key, the sorting result may not be unique, because the data elements with the same key code, which are in the sort result, are not kept in the position relationship between them.
For any sequence of data elements, use a sort method to sort it by key code: If the position relationship between the same key elements, the sort is consistent before and after the order, it is said that the sorting method is stable, but not consistent sorting method is called unstable.
The sorting is divided into two categories: inner and outer sort.
Inner sort: A sort process that is stored completely in memory, suitable for a less large sequence of elements.
Sorting: Refers to the ordering process also needs to access the external memory, large enough sequence of elements, because it can not fully put into memory, can only use the outside sort.

Now paste 3 sort algorithms for JavaScript implementations.

The first is the simplest, the individual will be the bubble sort. No more, just paste the code.
Copy Code code as follows:

/** @name Bubble Sort
* @lastmodify 2010/07/13
* @desc Comparison sort
Degree of Complexity O (n*n)
*/
function Bubblesort (list) {
var len = list.length;
var cl,temp;
while (len--) {
CL = list.length;
while (cl--) {
if (List[cl]>list[len] && cl < len) {
temp = List[len];
List[len] = List[cl];
LIST[CL] = temp;
}
}
}
return list;
}

Then the most common quick sort, interviews are basically asked.
Copy Code code as follows:

/** @name Quick Sort
* @lastmodify 2010/07/14
* @desc Comparison sort
Worst run time O (n*n);
Best run time O (NLOGN)
*/
function QuickSort (list) {
var i = 0;
var j = list.length;
var len = j;
var left;
var right;
var k = Findk (i, j);
if (k!= 0) {
var leftarr = [];
var rightarr = [];
var Midarr = [List[k]];
while (len--) {
if (len!= k) {
if (List[len] > List[k]) {
Rightarr.push (List[len]);
}
else{
Leftarr.push (List[len]);
}
}
}
left = QuickSort (Leftarr);
right = QuickSort (Rightarr);
List = Left.concat (Midarr). Concat (right);
}
return list;
}

function Findk (i,j) {
Find its middle position by default
Return Math.floor ((i + j)/2);
}

The main idea of fast ordering is to divide the method into 2 pieces, which will reduce the complexity of the order. The skillful use of recursion is also the subtlety of quick sorting. In the last example, first use the FINDK function to find the "reference element", the other elements are compared to the element in turn, all larger than the one in a set, smaller than the other set, and then sorted two sets. The efficiency of fast sorting depends mainly on the implementation of the FINDK function and the order degree of the elements to be sorted. Therefore, fast ordering is an unstable sort algorithm.

But the quick sort is still a comparison based sort algorithm. One feature of all comparison based sorting algorithms is that, regardless of how they are optimized, the average sorting time for a set of elements always increases with the number of elements in the set. Rather than the comparison of the sort to overcome this shortcoming, they try to let the sorting time complexity tends to a number of independent stability values. One of the more representative is the bucket sort. Let's look at its JavaScript implementation first.
Copy Code code as follows:

/** @name Bucket Sort
* @author LeBron
* @lastmodify 2010/07/15
* @desc non-comparison sort
*/
function Bucketsort (list) {
var len = list.length;
var range = Findmax (list);
var result = [],
Count = [];
var i,j;
for (i = 0; i < range; i++) {
Count.push (0);
}

for (j = 0; J < Len; J + +) {
count[list[j]]++;
Result.push (0);
}
for (i = 1; i < range; i++) {
Count[i] = Count[i-1] + count[i];
}
for (j = len-1 J >= 0; j--) {
RESULT[COUNT[LIST[J]]] = list[j];
count[list[j]]--;
}
return result;
}

function Findmax (list) {
return MAX;
}

As you can see, in the bucket sort implementation, a Findmax function is still used to determine the range of a large array, which is directly replaced by a constant max. First initializes a large array of count, which is the length max. When the value in the sorted set is placed in the corresponding position, such as having an element value of 24, the 24th digit of Count is labeled 1, and the result array is +1 length. The position of the element with flag 1 in the count array is then computed as the rank marked 1 in the entire count array. In this case, the value of the nth element in the count array should be the position of the sort, and n this is the value that corresponds to this position after this sort. So, finally, one by one of the key values in the count array are mapped upside down into the result array.
The bucket sort cleverly utilizes the idea that if an element is the nth in a set, it should rank N, without caring whether the previous or the latter is larger or smaller than it (without comparison). Obviously, in practice, the value of the elements of the sorted set is likely to be much larger than the number of elements in the set, so it is necessary to allocate an array of the corresponding large space. As a result, the common scenario for bucket sorting is on the outside sort.

Interested students, you can test the next 3 kinds of sorting in different orders of magnitude time consuming.
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.