Ten classic sorting Algorithms +sort sort

Source: Internet
Author: User
Tags prev

This article turns from: The ten classic sorting algorithm, which has the dynamic Diagram + code detailed, this article simple introduction + personal understanding. Sorting algorithms

Classic algorithmic problems are also frequently asked questions during the interview process. The sorting algorithm is simply categorized as follows:

The time complexity of these sorting algorithms is as follows:

where n represents the size of the data, K represents the number of buckets, In-place represents no additional space, and out-place represents the need for additional space.

Bubble sort (Bubble sort)

The simplest way to sort. Compares two elements at a time, and swaps them if they are in the wrong order. The entire sequence is accessed repeatedly until no elements need to be exchanged.

Algorithm description
    • Compares the adjacent elements. If the order is wrong, exchange it;
    • Iterate through the sequence, repeat step one, the largest element is at the far right after one traversal;
    • Repeat step two, each time you repeat, you can move an element to the appropriate position (it is lined up) until all the elements are lined up;
Algorithm analysis

Best case:\ (t (n) = O (n) \), worst case:\ (t (n) = O (n^2) \), average condition:\ (t (n) = O (n^2) \).

Reference Code
void bubbleSort(vector<int> &nums) {    int n = nums.size();    for(int i = 0; i < n; i++) {        for(int j = 0; j < n-1-i; j++) {            if(nums[j] > nums[j+1]) {                // 元素交换                 nums[j] = nums[j]^nums[j+1];                nums[j+1] = nums[j]^nums[j+1];                nums[j] = nums[j]^nums[j+1];            }        }    }}
Select sort (Selection sort)

One of the most stable sorting methods, regardless of the time complexity is \ (O (n^2) \), no additional space is required. Simple and intuitive, each time you find the smallest (maximum) element in the unsorted, place it in the appropriate position and perform the (n-1) order.

Algorithm description
    • Traverse unordered sequence, find the smallest (maximum) element, exchange the element with the corresponding position;
    • Repeat step One (n-1) times, put all the elements in the corresponding position, that is, the sorting is complete;
Algorithm analysis

Best case:\ (t (n) = O (n^2) \), worst case:\ (t (n) = O (n^2) \), average condition:\ (t (n) = O (n^2) \).

Reference Code
void selectSort(vector<int> &nums) {    int n = nums.size(), minIndex;    for(int i = 0; i < n-1; i++) {        minIndex = i;        for(int j = i+1; j < n; j++) {            if(nums[j] < nums[minIndex])//寻找最小元素                 minIndex = j;        }        if(i == minIndex) continue;//相同位置元素不可异或交换        // 元素交换         nums[i] = nums[i]^nums[minIndex];        nums[minIndex] = nums[i]^nums[minIndex];        nums[i] = nums[i]^nums[minIndex];    }}
Insert sort (Insertion sort)

It is also an easy-to-understand sorting algorithm that does not require additional space. By constructing an ordered sequence, for unsorted elements, in the sorted sequence from backward to forward scan, find the corresponding position and insert. The insertion sort is implemented on an implementation, usually in the order of In-place (that is, the ordering of extra space using only O (1), so that in the backward-forward scanning process, the ordered elements need to be moved back and forth gradually, providing the insertion space for the newest elements.

Algorithm description

In general, the insertion sort is implemented using In-place on the array. The steps are as follows:

    • The first element is considered to be sorted;
    • Remove the next element and scan from the back forward in the sequence;
    • If the sorted element is greater than the new element, move the element to the next position;
    • Repeat step 3 until you find the sorted element ≤ the position of the new element, inserting the new element into the position;
    • Repeat steps until all are well sequenced.
Algorithm analysis

Best case:\ (t (n) = O (n) \), worst case:\ (t (n) = O (n^2) \), average condition:\ (t (n) = O (n^2) \).

Reference Code
void insertSort(vector<int> &nums) {    int n = nums.size(), prev, num;    for(int i = 0; i < n; i++) {        prev = i-1;//有序序列尾部         num = nums[i];//当前元素         while(prev>=0 && nums[prev]>num) {            nums[prev+1] = nums[prev];//后移            prev--;         }        nums[prev+1] = num;    }}
Hill sort

The first breakthrough O (n^2) sorting algorithm is an improved version of the simple insert sort. It differs from the insertion sort in that it takes precedence over elements that are farther apart. Hill sort is also known as narrowing the incremental sort.

The core of the hill sort is the setting of the interval sequence. You can set the interval sequence in advance, or you can define the interval sequence dynamically. The algorithm for dynamically defining the interval sequence is proposed by Robert Sedgewick, co-author of the algorithm (4th edition).

Algorithm description
    • Define the increment sequence: {T1,T2,...,TK}, which generally has t1>t2>...>tk=1;
    • According to the number of increment series K, the sequence is sorted by K-trip;
    • Each order, according to the corresponding increment ti, the waiting sequence is divided into several sub-sequences of length m, and the sequence of each subsequence is directly inserted. Only the increment factor is 1 o'clock, the entire sequence is treated as a sequence, and the sequence length is the length of the entire sequence.
Examples Show

The above algorithm description may not be very understood, for example. Pairs with {5, 2, 4, 1, 5, 9, 7, 8, 9, 0} sequences, first order, Delta T1=4 (custom), sequence is divided into {5, 5, 9},{2,9,0},{4,7},{1,8}, respectively, and the sequence is inserted into the order, the series becomes {5,0,4,1, 5,2,7,8, 9 , 9}; the second sort, increment t2=2, the sequence is divided into {5,4,5,7,9},{0,1,2,8,9}, it is inserted into the sort, the sequence becomes {4, 0, 5, 1, 5, 2, 7, 8, 9,9}, Third Order, increment t3=1, sequence { 4,0,5,1,5,2,7,8,9,9}, inserts it into the sort order and becomes {0,1,2,4,5,5,7,8,9,9}.

Algorithm analysis

The time complexity of the hill sort is related to its increment sequence, which involves a mathematical unsolved problem, but in some sequences the complexity can be considered O (n^1.3); the lower bound of the hill sort time complexity is n*log^2 N.

Reference Code
void shellSort(vector<int> &nums) {    int n = nums.size();    int gap, i, j;        for(gap = n/2; gap > 0; gap /= 2) {        //插入排序简洁写法         for(i = gap; i < n; i++) {            int num = nums[i];            for(j = i-gap; j>=0 && nums[j]>num; j-=gap)                nums[j+gap] = nums[j];            nums[j+gap] = num;        }    }}
Merge sort

As with select Sort, the performance of the merge sort is not affected by the input data, but the performance is much better than the selection sort, and the time complexity is always O (n log n). The cost is that additional memory space is required.

Merge sort is an efficient sorting algorithm based on the merging operation. This algorithm is a very typical application of the partition method (Divide and Conquer). Merge sort is a stable sort method. The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If you combine two ordered tables into an ordered table, it is called a 2-way merge.

Merge sort has many applications, such as solving the problem of reverse order, just need to add a line of code in the process of merging sorting.

Algorithm description
    • The input sequence of length n is divided into two sub-sequences of length n/2;
    • The two sub-sequences were sorted by merging;
    • Merges two sorted sub-sequences into a final sort sequence. Compliance

The merging process requires additional space, using a new array, comparing two sub-sequences, continuously adding the smaller elements to the new array, and finally updating the new array to the original sequence.

Algorithm analysis

Best case:\ (t (n) = O (Nlogn) \), worst case:\ (t (n) = O (Nlogn) \), average condition:\ (t (n) = O (Nlogn) \).

The spatial complexity is \ (O (n) \).

Reference Code
void Merge(vector<int> &nums, int first, int med, int last) {    int i = first, j = med+1;        vector<int> temp(nums.size());//额外空间     int cur=0;//当前位置    while(i<=med && j<=last) {        if(nums[i] <= nums[j])            temp[cur++] = nums[i++];        else            temp[cur++] = nums[j++];    }    while(i <= med)        temp[cur++] = nums[i++];    while(j <= last)        temp[cur++] = nums[j++];            for(int m = 0; m < cur; m++)//更新数组         nums[first++] = temp[m];}void mergeSort(vector<int> &nums, int first, int last) {    if(first < last) {        int med = first+(last-first)/2;        mergeSort(nums, first, med);        mergeSort(nums, med+1, last);        Merge(nums, first, med, last);    }}
Quick Sort

Basic idea: Select a sort datum to sort through, divide all the elements into two parts (greater than the baseline and less than the baseline), and quickly sort the two parts here.

The quick sort can be used to solve the big k problem, because after each order, you can pin an element.

Algorithm description

Quick sort use the divide-and-conquer method to divide a sequence into two sub-sequences. The specific algorithm is described as follows:

    • Select a sort datum (pivot) from the sequence;
    • The sequence is sorted, all the smaller than the base value is placed before the datum, all the larger than the base value of the pendulum behind the datum, the sequence is divided into two sub-sequences of left and right. called partition operation (partition);
    • Recursive, quickly sort the left and right two sub-sequences.
Algorithm analysis

Best case:\ (t (n) = O (Nlogn) \), worst case:\ (t (n) = O (n2) \), average condition:\ (t (n) = O (Nlogn) \).

Reference Code
void quickSort(vector<int> &nums, int left, int right) {    if(left<right) {        int l=left, r=right;        int pivot = nums[left];//判断标准值         while(l<r) {            while(l<r && nums[r]>=nums[l])//一定记住要加等于号,在下面加也行                r--;            swap(nums[l], nums[r]);                        while(l<r && nums[l]<nums[r])//在这里加等于号也行,但必须有一个加                l++;            swap(nums[l], nums[r]);        }        nums[l]=pivot;                quickSort(nums, l+1, right);        quickSort(nums, left, l-1);    }}
Heap sort (heap sort)

Heap ordering (heapsort) refers to a sort algorithm designed using the data structure of the heap. A heap is a structure that approximates a complete binary tree and satisfies the properties of the heap at the same time: that is, the key value or index of the child node is always less than (or greater than) its parent node.

Algorithm description
    • The initial order-to-sort keyword sequence (r1,r2....rn) is constructed into a large top heap, which is the initial unordered area;
    • Swap the top element of the heap r[1] with the last element R[n] to get a new unordered area (R1,R2,...... RN-1) and the new ordered area (Rn), and satisfies the r[1,2...n-1]<=r[n];
    • Because the new heap top r[1] may violate the nature of the heap, it requires a current unordered zone (R1,R2,...... RN-1) adjusts to the new heap, then swaps the r[1] with the last element of the unordered zone, resulting in a new unordered area (r1,r2....rn-2) and a new ordered area (RN-1,RN). This process is repeated until the number of elements in the ordered area is n-1, and the entire sorting process is complete.

may seem a bit complex, in the reference link in this article, there is a dynamic diagram interpretation, it may be good to understand some.

Algorithm analysis

Best case:\ (t (n) = O (Nlogn) \), worst case:\ (t (n) = O (Nlogn) \), average condition:\ (t (n) = O (Nlogn) \).

Reference Code
int len;void heapify(vector<int> &nums, int i) {    int left = 2*i+1;    int right = 2*i+2;    int largest = i;        if(left<len && nums[left] > nums[largest])        largest = left;    if(right<len && nums[right] > nums[largest])        largest = right;            if(largest != i) {        swap(nums[i], nums[largest]);        heapify(nums, largest);    }}void buildMaxHeap(vector<int> &nums) {    len = nums.size();    for(int i = len/2; i>=0; i--)        heapify(nums, i);}void heapSort(vector<int> &nums) {    buildMaxHeap(nums);        for(int i = nums.size()-1; i>0; i--) {        swap(nums[0], nums[i]);        len--;        heapify(nums, 0);    }}
Count sort (counting sort)

The core of a count sort is to convert the input data value into a key stored in an additional array space. As a sort of linear time complexity, counting ordering requires that the input data be an integer with a definite range.

The Count sort (counting sort) is a stable sorting algorithm. The count sort uses an extra array of C, where the I element is the number of elements in the array A to be sorted with the value equal to I. The elements in a are then ranked in the correct position according to the array C. It can only sort integers. This is actually the basic use of map.

The count sort is too restrictive to require an integer to determine the range. The actual problem can not be used at all, but in some special scenarios may be used.

Algorithm description
    • Find the largest and smallest elements in the array to be sorted;
    • The number of occurrences of each element in the statistic array that is I, in the array C;
    • Summation of all counts (starting with the first element in C, each item and the previous one);
    • Reverse-Populate the target array: Place each element I in the C (i) of the new array, minus 1 for each element that is placed.
Algorithm analysis

When the input element is an integer of n 0 to K, its run time is \ (O (n + k) \). The count sort is not a comparison sort, and the sort is faster than any comparison sort algorithm. Because the length of the array C used to count depends on the range of data in the array to be sorted (equal to the difference between the maximum and minimum values of the array to be sorted plus 1), this makes the count sort for arrays with a large data range, which requires a lot of time and memory.

Best case:\ (t (n) = O (n+k) \), worst case:\ (t (n) = O (n+k) \), average condition:\ (t (n) = O (n+k) \).

Reference Code
Summarize

Bubble sort is the basis, select sort and insert sort to understand, merge sort and quick sort to memorize the principle and write code.

Count sort, bucket sort, cardinal sort are not comparison sorts, can be classified as one class.

Ten classic sorting algorithms +sort sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.