Implementation of Common sorting algorithms based on JS

I. Bubble Sorting 1. principle. compare two adjacent elements to be sorted from the beginning. If the preceding element is greater than the following element, the positions of the two elements are swapped. in this way, after the first element of the sequence is traversed to n-1 elements, the largest element is "sunk" to the last position of the sequence (n-1 position, n is the number of elements to be sorted.) c. exclude the last element (n = N-1) of the sorting, and continue to repeat the previous two steps of d. when (n = N-1) = 0, the sorting is completed. the specific implementation takes the sequence to be sorted as an example: Bubble Sorting is completed for the first time, and the maximum value is 7 to the end. Then, sort the sequence except the last element (7) by bubble. The specific implementation is as follows:/*** Bubble Sorting ** @ param {Array} arr-integer Array * @ returns {Array} ret-sorted Array */function bubbleSort (arr) {var n = arr. length, I = 0, temp; while (-- n) {while (I <n) {// if the previous number is greater than the next one, if (arr [I]> arr [I + 1]) {temp = arr [I]; arr [I] = arr [I + 1]; arr [I + 1] = temp;} I ++;} // after each bubble is complete, reset I = 0;} return arr ;} // test ('bubble-sort ', function () {var arr1 = [6, 2, 4, 3]; var arr2 = [28, 13, 10]; var arr3 = [,]; propEqual (bubbleSort (arr1), [,]); propEqual (bubbleSort (arr2 ), [, 28]); propEqual (bubbleSort (arr3), [,]);}); 2: Select sort 1. principle. first, find the minimum element in the sequence to be sorted and store it in the sequence of storage. Delete this element B from the sequence to be sorted. continue to find the smallest element in the sequence that has never been sorted, and then step a contains c. and so on until the number of elements in the sequence to be sorted is 0. specific implementation: Select sort/*** select sort * @ param {Array} arr * @ returns {Array} ret */function selectionSort (arr) {var ret = [], min, I; while (arr. length) {// TODO should calculate the minimum value in the Array Using min = Math. min. apply (null, arr); ret. push (min); // Delete the arr element from the array to be sorted. splice (arr. indexOf (min), 1);} return ret;} // qunittest ('selection-sort ', function () {var Arr1 = [,]; var arr2 = [,]; propEqual (selectionSort (arr1), [,]); propEqual (selectionSort (arr2), [1, 2, 4, 5, 6]);}); 3: insert sort 1. sorting Principle:. the elements are sorted starting from 0th elements in the sequence to be sorted. This element can be regarded as B. extracts the next element and traverses the sorted element sequence from the back to the left. if the sorted element is greater than the new one, move the element to the next position. repeat Step c until a sorted element is found. The element is not greater than the new element, or the element is located at the starting position of an ordered sequence. insert a new element to the end of this element. repeat Step B ~ E. The number of elements in the direct reception queue is 0. 2. specific implementation of insert Sorting/*** insert sorting * @ param {Array} arr * @ returns {Array} ret */function insertionSort (arr) {// starts from 1, because the sequence of an element is always ordered for (var I = 1, j; I <arr. length; I ++) {j = I; // Save the element to be sorted v = arr [j]; // If the element in the sequence is greater than the element to be inserted, then, the elements in the sequence move one position backward // move one position backward to overwrite the elements to be sorted, but we have the elements to be sorted, so the elements to be sorted will not be lost // At the same time, it also sets aside a location to insert the elements to be sorted // until a sorted element is found, which is not greater than the elements to be sorted, insert the elements to be sorted here. // If it is traversed to the starting position of the ordered sequence, there is no element not greater than the element to be sorted, insert the elements to be sorted to the beginning of the sorted sequence while (arr [J-1]> v) {arr [j] = arr [J-1]; j --; if (j = 0) {break;} arr [j] = v;} return arr;} // qunittest ('insertion-sort ', function () {var arr1 = [,]; var arr2 = [,]; propEqual (insertionSort (arr1), [,]); propEqual (insertionSort (arr2), [1, 2, 4, 5, 6]) ;}); 4: Hill sorting 1. sorting Principle:. set a spacing d to group the sequence to be sorted. sort groups by insert. c. Change d and group d again. use Insert sort for the preceding group again. e. repeat the preceding steps until d is 1 and sort the last insert order to obtain the sorted sequence 2. specific implementation of the hill sorting 1 Hill sorting 2 the hill sorting process involves selecting a set of spacing sequences, called the hill increment. The secret of hill increment will be studied later. /*** Hill sorting ** @ param {Array} arr * @ returns {Array} */function shellSort (arr) {// obtain a hill incremental var N = arr dynamically. length; var h = 1; var I, j, v; // generate the first value of the hill incremental sequence while (h <N/3) {h = 3 * h + 1; // ①} // USE insert sort for the group, and change the increment value of hill until it is 1 and perform the last insert sort to obtain the sequence. // The First insert sort is obtained, they can all be arranged into an ordered sequence // they can be inserted and sorted without stopping. In fact, they reduce the number of times elements move in the sorting process. while (h> = 1) {for (I = 1; I <arr. length; I ++) {j = I; v = arr [j]; while (j> 0 & arr [j-1 ]> V) {arr [j] = arr [j-1]; j --;} arr [j] = v;} h = (h-1)/3; // you can guarantee from ① That} return arr;} // qunittest ('Shell-sort ', function () {var arr1 =,, 12, 45]; var arr2 = [,]; propEqual (shellSort (arr1), [,]); propEqual (shellSort (arr2), [1, 2, 4, 5, 6]);}); 5. merge Order 1. sorting principle: Merge Sorting mainly consists of two steps:. grouping sorting sequences is divided into two small sequences by means of the binary method, and recursion continues until the sequence length is 1 (the sequence with the length of 1 is ordered) B. integration Combine the sorted array into an ordered series, and finally obtain the sorting result 2. specific implementation of Merge Sorting 1 Merge Sorting 2 // Merge Sorting function mergetSort (arr) {if (arr. length = 1) {return arr;} var leftArr = arr. slice (0, Math. floor (arr. length/2); var rightArr = arr. slice (leftArr. length); // recursive return merge (mergetSort (leftArr), mergetSort (rightArr); // merged ordered sequence function merge (arrLeft, arrRight) {var indexLeft = 0, indexRight = 0, sl = arrLeft. length, sr = arrRight. length, Ret = []; while (true) {if (indexLeft <sl & indexRight <sr) {if (arrLeft [indexLeft] <arrRight [indexRight]) {ret. push (arrLeft [indexLeft]); indexLeft ++;} else {ret. push (arrRight [indexRight]); indexRight ++ ;}} else {if (indexLeft <indexRight) {ret = ret. concat (arrLeft. slice (indexLeft);} else {ret = ret. concat (arrRight. slice (indexRight);} break;} return ret;} // qunittest ('merge- Sort ', function () {var arr1 = [, 13, 10]; var arr2 = [,]; propEqual (mergetSort (arr1, 13, 19, 28]); propEqual (mergetSort (arr2), [,]);}); 6: Fast sorting 1. sorting principle. pick out an element from the sequence, called "benchmark", B. in the re-sorting sequence, all elements smaller than the benchmark value are placed before the benchmark, and all elements greater than the benchmark value are placed after the benchmark c. sort the new sequence by the method above. End recursion when the sequence length is 1 or 0. specific implementation of quick sorting // quick sorting function quickSort (arr) {var variable = arr [0]; var I = 1; var leftArr = [], rightArr = []; if (arr. length = 0) {return [];} if (arr. length = 1) {return arr ;}for (; I <arr. length; I ++) {if (arr [I] <strong) {leftArr. push (arr [I]);} else {rightArr. push (arr [I]);} return quickSort (leftArr ). concat (cipher, quickSort (rightArr);} // qunittest ('quick-sort ', functi On () {var arr1 = [,]; var arr2 = [, 10]; var arr3 = [, 6]; propEqual (quickSort (arr1), [2, 3, 4, 6]); propEqual (quickSort (arr2), [4, 10, 13, 19, 28]); propEqual (quickSort (arr3 ), [1, 2, 3, 4, 5, 6, 7]);}); summary: This article describes common sorting algorithms (bubble, select, insert, Hill, merge, and fast sort) I implemented it based on JS and practiced the algorithm to improve programming thinking. For example, calculate the maximum value of an array. We know that we can: var max = Math. min. applu (null, arr) But how does the browser implement it? Or if the browser does not provide this method, how can we do it ourselves? In fact, the minimum value can be obtained after a bubble to the left: function getMinValue (arr) {var I = arr. length, j; j = I; while (-- j) {if (arr [j-1]> arr [j]) {arr [j-1] = [arr [j-1], arr [j]; arr [j] = arr [j-1] [0]; arr [j-1] = arr [j-1] [1] ;}/// return the minimum return arr [0];}