Bubble sort
Bubble sorting is an exchange sort, whose basic idea is to compare the keywords of adjacent records between two pairs, and swap them if they are in reverse order until there are no records in reverse order.It repeatedly visits the columns to be sorted, compares two elements at a time, and exchanges them if they are in the wrong order.The work of visiting a number of columns is repeated until no exchanges are needed, that is, the columns have been sorted.The name of this algorithm comes from the fact that smaller elements "float" slowly to the top of a number of columns through an exchange.
function bubbleSort( arr ){
for( var i = 0 ; i < arr.length - 1 ; i++ ){
var flag = true;
for( var j = 0 ; j < arr.length - i - 1 ; j++ ){
if( arr[ j ] > arr[ j + 1 ] ){
[ arr[ j ] , arr[ j + 1 ] ] = [ arr[ j + 1 ] , arr[ j ] ]
flag = false;
}
}
if(flag){
return arr;
}
}
}
Insert Sort
The basic operation of insertion sort is to insert a record into an ordered table that has been ordered so as to get a new ordered table with an increase of 1 record number.
The sorting process is roughly as follows:
Starting with the first element, the element can be thought to have been 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 a position where the sorted element is less than or equal to the new element;
After inserting a new element into the location;
Repeat steps 2~5
function insertSort( arr ){
for( var i = 0 ; i < arr.length ; 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;
}
Binary insertion sort
Starting with the first element, the element can be thought to have been sorted;
Remove the next element and dichotomize the ordered sequence of elements to find the position of the first greater number.
After inserting a new element into the location;
Repeat the above two steps
function binaryInsertSort( arr ){
for( var i = 0 ; 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;
}
Select Sort
Selective sorting is to select the record with the smallest keyword from n-i-1 records by comparing n-i subkeywords and exchange it with the first record.Selection-sort is a simple and intuitive sorting algorithm.How it works: first find the smallest (large) element in the unsorted sequence, store it at the beginning of the sorted sequence, then continue to find the smallest (large) element from the remaining unsorted elements, and then place it at the end of the sorted sequence.And so on until all the 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;
}
Quick Sort
(1) In a dataset, select an element as the pivot.
(2) All elements less than the "base" are moved to the left of the "base"; all elements greater than the "base" are moved to the right of the "base".
(3) Repeat the first and second steps for the two subsets to the left and right of the datum until only one element remains in all the subsets.
function quickSort( arr ){
//If the length of the array is less than or equal to 1, return directly without judgment
if( arr.length <= 1 ){
return arr;
}
var pivotIndex = Math.floor( arr.length / 2 );//Baseline
var pivot = arr.splice( pivotIndex , 1 )[0];//The splice(index,1) function returns the number that was deleted from the array, taking the value of the base point
var left = [];//Stores an array smaller than the base point
var right = [];//Stores an array larger than the base point
//Traverse the array to make a judgment about the allocation
for( var i = 0 ; i < arr.length ; i++ ){
if( arr[ i ] < pivot ){
left.push( arr[ i ] );//Array to the left smaller than the base point
}else{
right.push( arr[ i ] );//Array larger than datum on the right
}
}
//Recursively perform the above operations on the left and right arrays until the length of the array is <=1;
return quickSort( left ).concat( [ pivot ] , quickSort( right ) );
}