The implementation of sorting algorithm
My JS level is slag, so I use Java and C as a way to write JavaScript sorting algorithm.
And here I do not speak algorithm principle, just code implementation, there may be bugs, you are welcome blog comments Guide.
Insert Sort
The algorithm description of insertion sort (insertion-sort) is a simple and intuitive sort algorithm. It works by building an ordered sequence, scanning the sorted sequence for unsorted data, and finding the location and inserting it. Insert sorting on implementation, usually using in-place sorting (that is, just use the extra space of O (1) to sort), and in the process of backward scanning, you need to repeatedly move the sorted elements backwards and forwards, providing insert space for the newest elements.
The implementation code is as follows:
function Insertsort (arr) {
if (!arr) return;
var len = arr.length;
if (len = = 0 | | | len = = 1) return;
for (var i = 1, len = Arr.length i < len; i + +) {
var stand = arr[i];
for (var j = i-1 J >= 0; J-) {
if (Arr[j] > Stand) {
arr[j + 1] = Arr[j];
} else {
arr[j + 1 ] = stand;
break;
}
}} return arr;
}
Time complexity: O (n^2)
Of course, the algorithm is optimized, for example, to change the location algorithm of search substitution to two-point lookup.
Bubble Sort
The classic sort algorithm, mentioned bubble sort I was heartache. Bachelor's time must be the improvement of the bubble sorting algorithm, the results after the completion of the thesis can not complete the bubble sort algorithm, so embarrassing.
if (!arr) return;
var len = arr.length;
if (len = = 0 | | | len = = 1) return;
for (var i = 0; i < len. i + +) {for
(var j = 0; J < len-i-1; j + +) {
if (Arr[j] > arr[j + 1]) {
var tmp = arr[j + 1];
Arr[j + 1] = Arr[j];
ARR[J] = tmp;
}}} return arr;
}
Time complexity: O (n^2)
Quick Sort
Very classic sorting algorithm, the sorting process of the main I is divided into three steps:
- Pick an element from a sequence called a "benchmark" (pivot);
- Reorder the series, all elements smaller than the base value placed in front of the datum, all elements are larger than the base value behind the datum (the same number can be on either side). After the partition exits, the datum is positioned in the middle of the series. This is called partition (partition) operation;
- recursively (recursive) sorts the substrings that are less than the datum elements and those that are larger than the datum values.
The implementation code is as follows:
function QuickSort (arr, bt, ed) {
if (BT < ed) {
var pivot = findpartition (arr, BT, ed);
QuickSort (arr, BT, pivot-1);
QuickSort (arr, pivot + 1, ed);
}
function Findpartition (arr, bt, ed) {
var stand = arr[bt];
while (BT < ed) {while
(BT < ed && Arr[ed] >= stand) {
ed-;
}
if (BT < ed) {
ARR[BT + +] = arr[ed];
}
while (BT < Ed && ARR[BT] <= stand) {
BT + +;
}
if (BT < ed) {
arr[ed--] = ARR[BT];
}
}
ARR[BT] = stand;
return BT;
}
The complexity of Time is: O (NLOGN).
Merge Sort
is also a very classic sorting algorithm, I was learning JS opportunity to review the classic sorting algorithm. The idea of merging sort can refer to my blog: merge sort. I only write JS implementation here.
function MergeSort (arr, bt, ed) {
if (BT < ed) {
var mid = bt + parseint ((ED-BT)/2);
MergeSort (arr, BT, mid);
MergeSort (arr, mid + 1, ed);
Mergearray (arr, BT, Mid, ed);
}
function Mergearray (arr, BT, Mid, ed) {
var mArr = [];
var i = bt, j = mid + 1;
while (I <= mid && J <= ed) {
if (Arr[i] <= arr[j]) {
Marr.push (arr[i++));
} else {
m Arr.push (arr[j + +])
;
}
if (I <= mid) {
MARR = Marr.concat (Arr.slice (i, mid + 1));
if (J <= ed) {
MARR = Marr.concat (Arr.slice (J, ed + 1));
for (var h = 0; h < marr.length H + +) {
ARR[BT + h] = Marr[h];
}
Write merge sort of time there is also a small episode: JS can not automatically take the whole, and later used the parseint method, feeling Meng Meng big.