Overview of various sorting algorithms

Quick Sort (exchange paradigm)

Quick sort code basically back again on it, pay attention to> = and <= personal opinion but may also be without time out.

// 一趟快排就是找到第一个元素的位置使得左边比它小右边比它大
int _quicksort(int *a, int low, int high) {
    int mid_value = a[low];
    while(low < high) {
        while(low < high && a[high] >= mid_value) high--;
        a[low] = a[high];
        while(low < high && a[low] <= mid_value) low++;
        a[high] = a[low];
    }
    a[low] = mid_value;
    return low;
}

void quicksort(int *a, int low, int high) {
    if (low < high) {
        int mid = _quicksort(a, low, high);
        quicksort(a, low, mid-1);
        quicksort(a, mid+1, high);
    }
}

Bubble Sort (exchange paradigm)

void bubblesort(int *a, int n) {
    for(int i=0; i<n-1; i++) {
        int flag = 0;
        for(int j=n-1; j>i; j--) {
            if (a[j-1] > a[j]) {
                int tmp = a[j-1];
                a[j-1] = a[j];
                a[j] = tmp;
                flag = 1;
            }
        }
        if (!flag) return;
    }
}

Direct insertion sort (insertion Paradigm)

Not posted relatively simple code, the second element of the array is from a start of next iteration, a value is found to be inserted after the entire preceding ordered array.

Hill sorting (insert paradigm)

The question does not need to write the code, for example a trip in increments of 3 sorting sub-processes:

第0步 15 9 7 8 20 -1 4
第1步 15     8       4  ->  4      8       5
第2步    9     20       ->    9      20   
第3步      7      -1    ->      -1       7
(每一步都用直接插入排序获取)
第一趟得到: 4 9 -1 8 20 7 5

On this basis, Hill sorting follow the following procedure:

  • From increments of n / 2 starts, incremental times by halving the above operations until a final increment of one end of that trip.

Select sorting (selection paradigm)

Very simple, if from small to large, starting with index 0 to the end, every step of the selection on the current index position behind minimal.

Heapsort (choose paradigm)

Push sort the array seen as a complete binary tree, the root mean any parent heap Fengyun binary tree is always greater than the child node.
Complete binary tree is that each element of the array in order to fill a complete binary tree.
Built large root heap process:

  1. From the last non-leaf node, from the bottom upward adjustment of each non-leaf nodes (right to left from bottom to top)
  2. Adjustment: If the current node is greater than the need to adjust two sons. Otherwise, find the greatest value to the son with their exchange, and then adjust the son until the leaf nodes.
    Heap sort process (after completing construction of the heap):
void heapsort(int *a, int n) {
    build_maxheap(a, n);
    for(int i=n-1; i>0; i--) {
        swap(a[0], a[i]); // 从后往前迭代,每次将堆顶输出到当前位置, 排序后为从小到大
        adjust(a, i-1); 
    }
}

Merge sort

Merge algorithm is quite important, we recommended back a bit, its function is to merge two already ordered array, the output order.

void merge(int *a, int low, int mid, int high, int *buffer) {
    // a[low:mid] 与 a[mid+1:high] 各自有序, 现在要输出一个有序的a[low:high]
    int i, j, k;
    for (k=low; k<=high; k++) buffer[k] = a[k]; //拷贝
    for (i=low, j=mid+1, k=low; k<=mid && j<=high; k++) {
        // i,j为两个有序数组的扫描指针, k为当前输出指针
        if (buffer[i] <= buffer[j]) a[k] = buffer[i++];
        else a[k] = buffer[j++];
    }
    while(i<=mid)  a[k++] = buffer[i++];
    while(j<=high) a[k++] = buffer[j++];
}

int *buffer = new int[n] // 需要开一个和输入一样大的数组
void mergesort(int *a, int low, int high) {
    if (low<high) {
        int mid = (low+high)/2;
        mergesort(a, low, mid);
        mergesort(a, mid+1, high);
        merge(a, low, mid, high, buffer); //归并放最后,这个算法是自底向上的
    }
}

Radix Sort

Decimal group, the lower priority radix sorting process:

  1. The establishment of 10 queues
  2. According to enter all the last (10%), placed in queues (for example 123 placed on the tail queue No. 3)
  3. 0-10 the respective output values ​​from the queue
  4. And then placed in a queue according to the penultimate position (similar to the operation 2)
  5. 0-10 the respective output values ​​from the queue
  6. It continues until the end of the highest-bit operating

Sorting Algorithm Characteristics Table

algorithm The complexity of the best Average complexity Worst complexity Space complexity stability
Direct insertion sort O (n) O (n ^ 2) O (n ^ 2) O (1) Y
Bubble Sort O (n) O (n ^ 2) O (n ^ 2) O (1) Y
Selection Sort O (n ^ 2) O (n ^ 2) O (n ^ 2) O (1)
Shell sort O (1)
Quick Sort O (nlogn) O (nlogn) O (n ^ 2) O (logn)
Heapsort O (nlogn) O (nlogn) O (nlogn) O (1)
2-way merge sort O (nlogn) O (nlogn) O (nlogn) O (n) Y
Radix Sort O(d(n+r)) O(d(n+r)) O(d(n+r)) (R) Y

Guess you like

Origin www.cnblogs.com/xytpai/p/12609662.html