0.1 Algorithm classification

0.1 Algorithm classification

Ten common sorting algorithms can be divided into two categories:

Comparative sorting: The relative order of elements is determined by comparison. Because its time complexity cannot exceed O(nlogn), it is also called nonlinear time comparison sorting.
Non-comparative sorting: The relative order between elements is not determined by comparison. It can break through the lower bound of time based on comparative sorting and run in linear time, so it is also called linear-time non-comparative sorting.

0.2 Algorithm complexity

0.3 Related concepts

Stable: If a is originally in front of b, and a=b, a is still in front of b after sorting.
Unstable: If a is originally in front of b, and a=b, a may appear behind b after sorting.
Time complexity: the total number of operations on sorted data. Reflects the law of the number of operations when n changes.
Space complexity: refers to
the measurement of the storage space required when the algorithm is executed in the computer . It is also a function of the data size n.

1. Bubble Sort (Bubble Sort)

Bubble sort is a simple sorting algorithm. It has repeatedly visited the sequence to be sorted, comparing two elements at a time, and swapping them if they are in the wrong order. The work of visiting the sequence is repeated until no more exchanges are needed, which means that the sequence has been sorted. The origin of the name of this algorithm is because the smaller the element will slowly "float" to the top of the sequence through exchange.

1.1 Algorithm description

Compare adjacent elements. If the first one is greater than the second, swap the two;
do the same work for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end, so that the last element should be the largest Count;
repeat the above steps for all elements except the last one;
repeat steps 1~3 until the sorting is completed.

1.2 Animation demo

1.3 Code implementation

function bubbleSort(arr) { var len = arr.length; for (var i = 0; i <len-1; i++) { for (var j = 0; j <len-1-i; j++) { if (arr [j]> arr[j+1]) {// Pairwise comparison of adjacent elements var temp = arr[j+1]; // Element exchange arr[j+1] = arr[j]; arr[j] = temp; } } } return arr; } 2. Selection Sort (Selection Sort) Selection-sort is a simple and intuitive sorting algorithm. Its working principle: first find the smallest (large) element in the unsorted sequence, store it at the beginning of the sorted sequence, and then continue to find the smallest (large) element from the remaining unsorted elements, and then put it in the sorted sequence At the end. And so on, until all elements are sorted.













2.1 Algorithm description
Direct selection and sorting of n records can get ordered results through n-1 times of direct selection and sorting. The specific algorithm is described as follows:

Initial state: the disordered area is R[1...n], the ordered area is empty; when
the i-th sorting (i=1,2,3...n-1) starts, the current ordered area and disordered area are respectively R[1...i-1] and R(i...n). This sorting trip selects the record R[k] with the smallest key from the current disordered area, and exchanges it with the first record R of the disordered area to make R[1...i] and R[i+1... n) Become a new ordered area with the number of records increased by one and a new disordered area with the number of records decreased by one respectively; the
n-1 pass ends, and the array is ordered.
2.2 Moving picture demonstration

2.3 Code implementation
function selectionSort(arr) { var len = arr.length; var minIndex, temp; for (var i = 0; i <len-1; i++) { minIndex = i; for (var j = i + 1; j <len; j++) { if (arr[j] <arr[minIndex]) {// find the smallest number minIndex = j; // save the index of the smallest number } } temp = arr[i]; arr[i ] = arr[minIndex]; arr[minIndex] = temp; } return arr; } 2.4 Algorithm analysis is one of the most stable sorting algorithms, because no matter what data enters, it is O(n2) time complexity, so it is used When it comes to it, the smaller the data size, the better. The only advantage may be that it does not take up additional memory space. In theory, selective sorting may also be the sorting method most people think of.
















3. Insertion Sort (Insertion Sort)
Insertion-Sort algorithm description is a simple and intuitive sorting algorithm. It works by constructing an ordered sequence. For unsorted data, scan from back to front in the sorted sequence, find the corresponding position and insert it.

3.1 Algorithm description
Generally speaking, insertion sort is implemented on an array using in-place. The specific algorithm is described as follows:

Starting from the first element, the element can be considered as sorted;
take out the next element and scan from back to forward in the sequence of sorted elements;
if the element (sorted) is greater than the new element, move the element to the bottom One position;
repeat step 3 until you find the position of the sorted element that is less than or equal to the new element;
insert the new element into this position;
repeat steps 2 to 5.
3.2 Moving picture demonstration

3.2 Code implementation
function insertionSort(arr) { var len = arr.length; var preIndex, current; for (var i = 1; i <len; i++) { preIndex = i-1; current = arr[i]; while ( preIndex >= 0 && arr[preIndex]> current) { arr[preIndex + 1] = arr[preIndex]; preIndex–; } arr[preIndex + 1] = current; } return arr; } 3.4 Algorithm analysis and insertion sort in implementation In the above, in-place sorting is usually used (that is, sorting that only needs O(1) extra space), so in the process of scanning from back to forward, it is necessary to repeatedly shift the sorted elements back to the newest element Provide insert space.














4. Shell Sort (Shell Sort)
Shell invented in 1959, the first breakthrough O(n2) sorting algorithm, is an improved version of simple insertion sort. The difference between it and insertion sort is that it will compare the farther elements first. Hill sorting is also called reduced incremental sorting.

4.1 Algorithm description
First divide the entire sequence of records to be sorted into several sub-sequences for direct insertion sorting, the specific algorithm description:

Select an incremental sequence t1, t2,..., tk, where ti>tj, tk=1;
according to the number of incremental sequences k, the sequence is sorted by k times;
each pass is sorted, according to the corresponding increment ti, waiting The sorting sequence is divided into several sub-sequences of length m, and each sub-table is directly inserted and sorted. When only the increment factor is 1, the entire sequence is treated as a table, and the length of the table is the length of the entire sequence.
4.2 Animation demo

4.3 Code implementation
function shellSort(arr) { var len = arr.length; for (var gap = Math.floor(len / 2); gap> 0; gap = Math.floor(gap / 2)) { // Note: This is different from the animation demonstration. The animation is executed in groups. The actual operation is that multiple groups are executed alternately for (var i = gap; i <len; i++) { var j = i; var current = arr[i]; while (j-gap >= 0 && current <arr[j-gap]) { arr[j] = arr[j-gap]; j = j-gap; } arr[j] = current; } } return arr; } 4.4 Algorithm analysis The core of Hill sorting lies in the setting of interval sequence. The interval sequence can be set in advance, or the interval sequence can be dynamically defined. The algorithm for dynamically defining the interval sequence was proposed by Robert Sedgewick, the co-author of "Algorithm (4th Edition)".
















5. Merge Sort (Merge Sort) Merge Sort
is an effective sorting algorithm based on merge operations. This algorithm is a very typical application of Divide and Conquer. Combine existing ordered subsequences to obtain a completely ordered sequence; that is, first make each subsequence in order, and then make the subsequences in order. If two ordered lists are merged into one ordered list, it is called 2-way merge.

5.1 Algorithm description
Divide the input sequence of length n into two subsequences of length n/2;
use merge sorting on these two subsequences respectively; merge
the two sorted subsequences into a final sorted sequence.
5.2 Motion Picture Demonstration

5.3 代码实现
function mergeSort(arr) {
var len = arr.length;
if (len < 2) {
return arr;
}
var middle = Math.floor(len / 2),
left = arr.slice(0, middle),
right = arr.slice(middle);
return merge(mergeSort(left), mergeSort(right));
}

function merge(left, right) {
var result = [];

while (left.length>0 && right.length>0) {
    if (left[0] <= right[0]) {
        result.push(left.shift());
    } else {
        result.push(right.shift());
    }
}

while (left.length)
    result.push(left.shift());

while (right.length)
    result.push(right.shift());

return result;

}
5.4 Algorithm analysis
Merge sorting is a stable sorting method. Like selection sort, the performance of merge sort is not affected by the input data, but it performs much better than selection sort because it is always O(nlogn) time complexity. The price is the need for additional memory space.

6.
The basic idea of Quick Sort : separate the records to be sorted into two independent parts through a sorting, and the keywords of one part of the records are smaller than the keywords of the other part. Some records continue to be sorted to achieve the order of the entire sequence.

6.1 Algorithm description
Quick sort uses divide and conquer to divide a string (list) into two sub-lists (sub-lists). The specific algorithm is described as follows:

Pick an element from the sequence and call it a "pivot";
reorder the sequence so that all elements smaller than the reference value are placed in front of the reference, and all elements larger than the reference value are placed behind the reference (the same number). You can go to either side). After this partition exits, the benchmark is in the middle of the sequence. This is called a partition operation;
recursively sort the sub-arrays of elements smaller than the reference value and the sub-arrays of elements greater than the reference value.
6.2 Motion Picture Demonstration

6.3 代码实现
function quickSort(arr, left, right) {
var len = arr.length,
partitionIndex,
left = typeof left != ‘number’ ? 0 : left,
right = typeof right != ‘number’ ? len - 1 : right;

if (left < right) {
    partitionIndex = partition(arr, left, right);
    quickSort(arr, left, partitionIndex-1);
    quickSort(arr, partitionIndex+1, right);
}
return arr;

}

function partition(arr, left ,right) { // 分区操作
var pivot = left, // 设定基准值(pivot)
index = pivot + 1;
for (var i = index; i <= right; i++) {
if (arr[i] < arr[pivot]) {
swap(arr, i, index);
index++;
}
}
swap(arr, pivot, index - 1);
return index-1;
}

function swap(arr, i, j) { var temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } 7. Heap Sort (Heap Sort ) Yes Refers to a sorting algorithm designed by using the heap data structure. Stacking is a structure that approximates a complete binary tree, and it also satisfies the nature of stacking: that is, the key or index of the child node is always less than (or greater than) its parent node.





7.1 Algorithm description
Construct the initial sequence of keywords to be sorted (R1, R2...Rn) into a large top heap, which is the initial disordered area;
swap the top element R[1] with the last element R[n] in this case to obtain new disordered regions (R1, R2, ...... Rn- 1) and the new ordered regions (Rn of), satisfying R [1,2 ... n-1] <= R [n];
Since After the exchange, the new top of the heap R[1] may violate the nature of the heap, so the current disordered area (R1, R2,...Rn-1) needs to be adjusted to a new heap, and then R[1] and the disordered area must be adjusted again The last element is exchanged, and a new disordered area (R1, R2...Rn-2) and a new ordered area (Rn-1, Rn) are obtained. This process is repeated until the number of elements in the ordered area is n-1, and the entire sorting process is completed.
7.2 Animation demo

7.3 Code implementation
var len; // Because multiple functions declared require data length, set len ​​as a global variable

function buildMaxHeap(arr) { // 建立大顶堆
len = arr.length;
for (var i = Math.floor(len/2); i >= 0; i–) {
heapify(arr, i);
}
}

function heapify(arr, i) { // 堆调整
var left = 2 * i + 1,
right = 2 * i + 2,
largest = i;

if (left < len && arr[left] > arr[largest]) {
    largest = left;
}

if (right < len && arr[right] > arr[largest]) {
    largest = right;
}

if (largest != i) {
    swap(arr, i, largest);
    heapify(arr, largest);
}

}

function swap(arr, i, j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

function heapSort(arr) {
buildMaxHeap(arr);

for (var i = arr.length - 1; i > 0; i--) {
    swap(arr, 0, i);
    len--;
    heapify(arr, 0);
}
return arr;

}
8. Counting Sort
is not a sorting algorithm based on comparison. Its core is to convert input data values ​​into keys and store them in an additional array space. As a sort of linear time complexity, the count sort requires that the input data must be an integer with a certain range.

8.1 Algorithm description
Find the largest and smallest elements in the array to be sorted;
count the number of occurrences of each element with the value i in the array and store it in the ith item of the array C;
accumulate all the counts (from the first in C) Start with an element, add each item to the previous item);
Fill the target array backward: place each element i in the C(i) item of the new array, and subtract C(i) every time you put an element Go to 1.
8.2 Motion Picture Demonstration

8.3 代码实现
function countingSort(arr, maxValue) {
var bucket = new Array(maxValue + 1),
sortedIndex = 0;
arrLen = arr.length,
bucketLen = maxValue + 1;

for (var i = 0; i < arrLen; i++) {
    if (!bucket[arr[i]]) {
        bucket[arr[i]] = 0;
    }
    bucket[arr[i]]++;
}

for (var j = 0; j < bucketLen; j++) {
    while(bucket[j] > 0) {
        arr[sortedIndex++] = j;
        bucket[j]--;
    }
}

return arr;

}
8.4 Algorithm analysis
Counting sorting is a stable sorting algorithm. When the input elements are n integers between 0 and k, the time complexity is O(n+k) and the space complexity is also O(n+k), and its sorting speed is faster than any comparison sorting algorithm. When k is not very large and the sequence is relatively concentrated, counting sorting is a very effective sorting algorithm.

9. Bucket Sort
is an upgraded version of counting sort. It uses the mapping relationship of functions, and the key to efficiency lies in the determination of the mapping function. The working principle of Bucket sort: Assuming that the input data is uniformly distributed, the data is divided into a limited number of buckets, and each bucket is sorted separately (it is possible to use another sorting algorithm or continue to use it in a recursive manner Bucket sorting is performed).

9.1 Algorithm description
Set a quantitative array as an empty bucket;
traverse the input data, and put the data one by one into the corresponding bucket;
sort each bucket that
is not empty ; never sort the buckets that are not empty The data is spliced ​​together.
9.2 Picture demonstration

9.3 代码实现
function bucketSort(arr, bucketSize) {
if (arr.length === 0) {
return arr;
}

var i;
var minValue = arr[0];
var maxValue = arr[0];
for (i = 1; i < arr.length; i++) {
  if (arr[i] < minValue) {
      minValue = arr[i];                // 输入数据的最小值
  } else if (arr[i] > maxValue) {
      maxValue = arr[i];                // 输入数据的最大值
  }
}

// 桶的初始化
var DEFAULT_BUCKET_SIZE = 5;            // 设置桶的默认数量为5
bucketSize = bucketSize || DEFAULT_BUCKET_SIZE;
var bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1;  
var buckets = new Array(bucketCount);
for (i = 0; i < buckets.length; i++) {
    buckets[i] = [];
}

// 利用映射函数将数据分配到各个桶中
for (i = 0; i < arr.length; i++) {
    buckets[Math.floor((arr[i] - minValue) / bucketSize)].push(arr[i]);
}

arr.length = 0;
for (i = 0; i < buckets.length; i++) {
    insertionSort(buckets[i]);                      // 对每个桶进行排序,这里使用了插入排序
    for (var j = 0; j < buckets[i].length; j++) {
        arr.push(buckets[i][j]);                     
    }
}

return arr;

}
9.4 Algorithm analysis
Bucket sorting is best used in linear time O(n). The time complexity of bucket sorting depends on the time complexity of sorting the data between each bucket, because the time complexity of other parts is O (n). Obviously, the smaller the buckets are divided, the less data between each bucket, and the less time it takes to sort. But the corresponding space consumption will increase.

10. Radix Sort (Radix Sort)
Radix sort is sorted according to the low order, then collected; then sorted according to the high order, and then collected; and so on, until the highest order. Sometimes some attributes have a priority order, first sort by low priority, and then sort by high priority. The final order is that the high priority is first, and the low priority with the same high priority is first.

10.1 Algorithm description
Get the maximum number in the array and get the number of digits;
arr is the original array, and each bit is taken from the lowest bit to form the radix array;
radix is ​​counted and sorted (using the characteristics of counting sorting for small range numbers);
10.2 Motion Picture Demo

10.3 代码实现
var counter = [];
function radixSort(arr, maxDigit) {
var mod = 10;
var dev = 1;
for (var i = 0; i < maxDigit; i++, dev *= 10, mod = 10) {
for(var j = 0; j < arr.length; j++) {
var bucket = parseInt((arr[j] % mod) / dev);
if(counter[bucket]==null) {
counter[bucket] = [];
}
counter[bucket].push(arr[j]);
}
var pos = 0;
for(var j = 0; j < counter.length; j++) {
var value = null;
if(counter[j]!=null) {
while ((value = counter[j].shift()) != null) {
arr[pos++] = value;
}
}
}
}
return arr;
}
10.4 Algorithm analysis
Cardinality sorting is based on sorting separately and collecting separately, so it is stable. However, the performance of radix sort is slightly worse than that of bucket sort. Each bucket assignment of a key requires O(n) time complexity, and obtaining a new key sequence after assignment requires O(n) time complexity. If the data to be sorted can be divided into d keywords, the time complexity of the radix sort will be O(d
2n), of course, d is much smaller than n, so it is basically linear.

The space complexity of radix sort is O(n+k), where k is the number of buckets. Generally speaking, n>>k, so the extra space needs about n.

Guess you like

Origin blog.csdn.net/GodfatherTye/article/details/109123061