Js Sorting Algorithm Implementation (Summary), js Sorting Algorithm Summary
As follows:
// ---------- Some sorting algorithms var Sort ={} Sort. prototype = {// use sort to 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 = [4,324,]; // var array =, 5, 1,]; 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; // the start position of the record var tempj = j; // The end position of the record while (j> I) {// j <-------------- search forward if (array [j]> = key) {j --;} else {array [I] = array [j] // I ++ ------- ----- >> Search backward for while (j> ++ I) {if (array [I]> key) {array [j] = array [I]; break ;}}}} // if the first retrieved key is the smallest number if (tempi = I) {Sort (++ I, tempj); return ;} // leave the last empty space to key array [I] = key; // recursive Sort (tempi, I); Sort (j, tempj);} Sort (I, j ); return array;}, // insert sort insertSort: function (array) {// http://baike.baidu.com/image/d57e99942da24e5dd21b7080 // http://baike.baidu.com/view/396887.htm // var array = [4,324,]; var I = 1, j, temp, key, len = array. length; for (; I <len; I ++) {temp = j = I; key = array [j]; while (-- j>-1) {if (array [j]> key) {array [j + 1] = array [j];} else {break ;}} array [j + 1] = key ;} return array;}, // sort by hill // Jun. array. shellSort (Jun. array. df (10000); shellSort: function (array) {// http://zh.wikipedia.or G/zh/% E5 % B8 % 8C % E5 % B0 % 94% E6 % 8E % 92% E5 % BA % 8F // var array = [, 94, 65,23, 45,27, 1750,701,301,132, 25, 39,10]; // var tempArr = [, 57, 23, 10, 4, 1]; // reverse () on the Wiki, we can see that this optimal small step array var tempArr = [1031612713,217 378076, 45806244,965 1787, 2034035,428 481, 90358,190 01, 4025,836,182, 34, 9, 1] // select var I = 0 for the step size of the large array; var tempArrtempArrLength = tempArr. length; var len = array. lengt H; var len2 = parseInt (len/2); for (; I <tempArrLength; I ++) {if (tempArr [I]> len2) {continue ;} tempSort (tempArr [I]);} // sort a step function tempSort (temp) {// console. var I = 0, j = 0, f, tem, key; var tempLen = len % temp> 0? ParseInt (len/temp) + 1: len/temp; for (; I <temp; I ++) {// The for (j = 1; /* j <tempLen & */temp * j + I <len; j ++) {// cyclically round each row in each column. tem = f = temp * j + I; key = array [f]; while (tem-= temp)> = 0) {// search for if (array [tem]> key in sequence) {array [tem + temp] = array [tem];} else {break ;}} array [tem + temp] = key ;}} return array ;}}
The implementation (Summary) of various sorting algorithms in this js section is all the content shared by the editor. I hope to give you a reference and support for the help house.