Common sorting algorithms

Source: Internet
Author: User

1. Insert Sort

The insertion sort works by creating an ordered sequence, for unsorted data, after the sorted data has been scanned from behind, and then inserted after the corresponding position has been found.

① begins with the first element, which is defaulted to an ordered sequence.

② from the next unsorted data, from backward to forward in the sorted sequence

③ if the element is smaller than the ordered element, continue scanning forward

④ repeats ③ until the element is found to be greater than or equal to the position of the sorted element

⑤ Insert this element

⑥ Repeat ②

Code:

void Insertsort (int *a,int size) {assert (a); for (int i = 1; i < size; i++) {int index = I;int end = index-1;         The last element of the sorted sequence is subscript int temp = A[index];  Save values for unsorted data while (end >= 0 && temp < A[end]) {a[end + 1] = A[end];//When the sorted data is less than the sorted series data, move the sorted data back one end--; Continue to scan forward}a[end + 1] = temp; The unsorted data is found to be greater than or equal to the sorted sequence, or if the entire sorted sequence does not have a number less than the unsorted data}}

The insertion sort is highly efficient when operations are performed on almost sequenced data. But the insertion sort is generally inefficient, and only one bit of data can be moved at a time.

2. Hill sort

The hill sort is more optimized for the insertion sort.

The idea is:

The records are grouped by step gap, and the direct insertion sorting method is used to sort each group of records. As the stride size decreases, the grouped groups contain more and more records, and when the step value decreases to 1 o'clock, the entire data becomes a group that forms an ordered set of records, and the sort is completed.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/9F/wKiom1cFvSeSne0iAACCTYALOk8540.jpg "title=" QQ picture 20160407094830.jpg "Width=" "height=" 314 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:600PX;HEIGHT:314PX; " alt= "Wkiom1cfvsesne0iaacctyalok8540.jpg"/>

void Shellsort (int *a, int size) {assert (a); int gap = Size;while (Gap > 1) {gap = GAP/3 + 1;for (int i = gap; I < Si Ze i++)//For example, when gap=4, do not let it separate 4 sets of insertion sort, but a more ingenious method, let I start from gap each add 1, so that 4 groups can be inserted into the sort, one walk {int index = I;int temp = A[index];int end = Index-gap;while (end >= 0 && Temp<a[end]) {a[end + gap] = A[end];end = Gap;} A[end + gap] = temp;}}}

3. Heap Sequencing

The idea of heap sorting is to build the largest heap if it is sorted in ascending order, and vice versa to build the smallest heap.

The process of building a heap is from the last non-leaf node until it is aligned with the node. Let's say we're going to sort in ascending order. That is, when you align down, turn the small swap to the parent node.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/A0/wKiom1cFwGfhLmrBAAAxn4Fa1M4828.jpg "title=" 666.jpg "alt=" wkiom1cfwgfhlmrbaaaxn4fa1m4828.jpg "/>

Void adjustdown (int *a,int size,int parent)    //downward alignment {int child =  parent * 2 + 1;           // The left child's subscript to childwhile  (child < size)         // Ensure downward alignment until child is out of range {if  (child + 1 < size&&a[child + 1] >  a[child])   //when the right child > Left child, the child changes to right children subscript {child++;} if  (A[parent] < a[child]) {swap (A[parent],a[child]);p arent = child;                child = parent *  2 + 1;       }else{break;}}} Void heapsort (int *a, int size) {assert (a);for  (int i =  (size - &NBSP;2)  / 2; i >= 0; i--) {Adjustdown (a,size,i);          //start with a non-leaf node labeled 4, and align down}for  (int i = size - 1; i  > 0; i--) {swap (a[0],a[i]);   //swaps the first and last element in the largest heap, at which point the last element is the largest adjustdown (a,i,0); Build the remaining elements to the maximum heap}}


Common sorting algorithms

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.