Insertion sort
The basic operation of insertion sorting is to insert a record into an ordered table that has been sorted, so as to get a new ordered table with the number of records increasing by 1.
The sorting process is roughly as follows:
Starting with the first element, the element can be considered sorted.
Remove the next element and scan backwards and forwards in the sorted element sequence.
If the element (sorted) is larger than the new element, move the element to the next location.
Repeat step 3 until you find that the sorted element is less than or equal to the new element.
After inserting the new element into the location;
Repeat steps 2 to 5.
/**
* Insertion sort algorithm
* @param {Array} arr Arrays to be sorted
* @return {Array} Arrays sorted from small to large
*/
function insertSort(arr){
var len = arr.length;
for (var i = 1; i < len; i++) {
var key = arr[i];
var j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
return arr;
}
algorithm Analysis
Best case: Input arrays are arranged in ascending order. T(n) = O(n)
Worst case: Input arrays are arranged in descending order. T(n) = O(n2)
Average: T(n) = O(n2)
Bipartite insertion sort
Starting with the first element, the element can be considered sorted.
Take out the next element and dichotomize the sorted element sequence to find the location of the first number larger than it.
After inserting the new element into the location;
Repeat these two steps
/**
* Bipartite insertion sort
* @param {array} arr Arrays to be sorted
* @return {array} Sort the array of regrets
*/
function binaryInsertSort(arr){
for (var i = 1; i < arr.length; i++) {
var key = arr[i], left = 0, right = i - 1;
while (left <= right) {
var middle = parseInt((left + right) / 2);
if (key < arr[middle]) {
right = middle - 1;
} else {
left = middle + 1;
}
}
for (var j = i - 1; j >= left; j--) {
arr[j + 1] = arr[j];
}
arr[left] = key;
}
return arr;
}
algorithm analysis
Best case: T(n) = O(nlogn)
Worst case: T(n) = O(n2)
Average: T(n) = O(n2)
Selection sort
Selective sorting is to select the record with the smallest keyword from the n-i-1 record by comparing the n-i subkeywords, and exchange it with the i-th record. Selection-sort is a simple and intuitive sorting algorithm. Its working principle: first, find the smallest (big) element in the unordered sequence, store it at the beginning of the ordered sequence, then continue to find the smallest (big) element from the remaining unordered elements, and then put it at the end of the ordered sequence. By analogy, until all elements are sorted.
function selectSort(arr){
for(var i = 0; i < arr.length - 1; i++){
var min = arr[i];
for(var j = i + 1; j < arr.length - 1; j++){
if(min > arr[j]){
var temp = min;
min = arr[j];
arr[j] = temp;
}
}
arr[i] = min;
}
return arr;
}
algorithm analysis
Best case: T(n) = O(n2)
Worst case: T(n) = O(n2)
Average: T(n) = O(n2)
Bubble sort
Bubble sorting is a kind of exchange sorting. Its basic idea is to compare the keywords of adjacent records between two pairs and exchange them if they are in reverse order until there is no reverse order record. It repeatedly visits the columns to be sorted, compares two elements at a time, and swaps them if their order is wrong. The work of visiting a sequence is repeated until no exchange is needed, that is to say, the sequence has been sorted. The algorithm's name comes from the fact that smaller elements float slowly to the top of the sequence by swapping.
function bubbleSort(arr) {
var len = arr.length;
for (var i = 0; i < len - 1; i++) {
var n = 0;
for (var j = 0; j < len - i ; j++) {
if(arr[j] < arr[j-1]){
n++;
console.log(n);
var temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
if( n < 1){
break;
}
}
return arr;
}
Best case: T(n) = O(n)
Worst case: T(n) = O(n2)
Average: T(n) = O(n2)
Quick sort
(1) In the data set, select an element as a pivot.
(2) All elements smaller than the "benchmark" are moved to the left of the "benchmark"; all elements larger than the "benchmark" are moved to the right of the "benchmark".
(3) Repeat the first and second steps for the two subsets on the left and right sides of the benchmark until only one element remains in all subsets.
function quickSort(arr){
if (arr.length <= 1){return arr};
var pivotIndex = Math.floor(arr.length / 2);
var pivot = arr.splice(pivotIndex,1)[0];
var left = [];
var right = [];
for (var i = 0; i < arr.length; i++){
if(arr[i] < pivot) {
left.push(arr[i]);
}else{
right.push(arr[i]);
}
}
return quickSort(left).concat([pivot],quickSort(right));
}
algorithm analysis
Best case: T(n) = O(nlogn)
Worst case: T(n) = O(n2)
Average case: T(n) = O(nlogn)
Shell Sort
The essence of Hill sorting is grouping insertion sorting, which is also called reduced incremental sorting. The basic idea of this method is to divide the whole sequence of elements to be arranged into several sub-sequences (composed of elements separated by an'increment') for direct insertion sort, then reduce the increment in turn and sort again. When the elements in this sequence are basically ordered (the increment is small enough), then insert the whole element into a direct sequence. Because the efficiency of direct insertion sort is very high when the elements are basically ordered (close to the best case), the time efficiency of Hill sort is greatly improved.
Implementation of Hill Sorting Algorithms
function shallSort(array) {
var increment = array.length;
var i
var temp; //Temporary storage
var count = 0;
do {
//Set increment
increment = Math.floor(increment / 3) + 1;
for (i = increment ; i < array.length; i++) {
console.log(increment);
if (array[i] < array[i - increment]) {
temp = array[i];
for (var j = i - increment; j >= 0 && temp < array[j]; j -= increment) {
array[j + increment] = array[j];
}
array[j + increment] = temp;
}
}
}
while (increment > 1)
return array;
}