JS commonly used array element sorting algorithm (bubble Insert Hill) (1/2)

Source: Internet
Author: User

Let's take a look at some of the sorting methods commonly used in web effects, we mainly include the bubble Insert Hill sorting method, mainly for array operations.

----------some sort algorithms

//JS sorts using sort
Systemsort:function (Array) {
Return Array.Sort (function (A, b) {
return a-b;
});
},

Bubble sort

Bubblesort:function (Array) {
var i = 0, Len = array.length,
J, D;
for (; i<len; i++) {
For (j=0 j<len; j + +) {
if (Array[i] < array[j]) {
d = array[j];
ARRAY[J] = Array[i];
Array[i] = D;
}
}
}
return array;
},

Quick Sort

Quicksort:function (Array) {
var array = [8,4,6,2,7,9,3,5,74,5];
var array = [0,1,2,44,4,324,5,65,6,6,34,4,5,6,2,43,5,6,62,43,5,1,4,51,56,76,7,7,2,1,45,4,6,7];
var i = 0;
var j = array.length-1;
var sort = function (I, j) {

End condition
if (i = = j) {return};

var key = Array[i];
var tempi = i; Record start position
var tempj = j; Record End Position

while (J > i) {
J <<--------------look forward
if (Array[j] >= key) {
j--;
}else{
Array[i] = Array[j]
i++------------>> Look Back
while (J > ++i) {
if (Array[i] > key) {
ARRAY[J] = Array[i];
Break
}
}
}
}

If the first key to remove is the smallest number
if (tempi = = i) {
Sort (++i, TEMPJ);
return;
}

Last vacancy left to key
Array[i] = key;

Recursion
Sort (tempi, i);
Sort (j, TEMPJ);
}

Sort (i, j);

return array;
},

Home 1 2 last page

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.