[Data Structure (ii)] C + + simple implementation of seven kinds of sorting algorithms

Source: Internet
Author: User

One. Bubble sort (Bubble sort)

Basic idea: 22 Compare the keywords of adjacent records, if reversed, until there is no reverse order.

 //  bubble sort   void  Bubblesort (int  *p, int   length) { for  (int< /span> i = 0 ; I < Length-1 ; I++ for  (int  J =length-1 ; J>=i;j-- if  (P[j-1 ] > P[j]) {swap (p[j -1  ], p[j]); }        }    }}

The order before sorting is: 9 1 5 8 3 7 4 6 2

When I=1, the exchange situation is as follows:

Two. Simple select sort (Easy Selection sort)

Select the smallest record from the n-i+1 record and exchange it with the first (1≤i≤n) record by comparing the n-1 keywords.

//Simple Selection SortingvoidSimselsort (int*p,intlength) {    intI, J, Min;  for(i =0; I < length-1; i++) {min= i;//record the subscript of the minimum value, initialized to I         for(j=i+1; j<=length-1; j + +)        {            if(P[j] <p[min]) min= J;//by continuously comparing, get the lowest value subscript} swap (P[i], p[min]); }}

Three. Direct Insert sort

Insert a record directly into an ordered table that is already sorted, thus obtaining a new, sequential table with a 1 increase in record count.

//Direct Insert SortvoidStrinsersort (int*p,intlength) {    intI, J;  for(i =1; I <length; i++)    {        if(p[i]<p[i-1])        {          inttmp; TMP=P[i];  for(j = i-1; P[J] > tmp; j--) P[j+1] =P[j]; P[j+1] =tmp; }    }}

Four. Hill sort (Shell sort)

Hill sort is to group records by a certain increment of the subscript, sorting each group using the direct insertion sorting algorithm; As the increments gradually decrease, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the entire file is divided into a group, the algorithm terminates.

voidShellsort (int*p,intlength) {    intI, J; intincrement =length;  Do{Increment= Increment/3+1;  for(i = increment; I <= length-1; i++)        {              if(P[i] < p[i-Increment]) {                inttmp; TMP=P[i];  for(j = i-increment; J >=0&& tmp < P[J]; J-=increment) p[j+ Increment] =P[j]; P[j+ Increment] =tmp; }            }    }  while(Increment >1);}

Five. Heap sort (heap sort)

1. Definition of a heap

A heap is a complete binary tree with the following properties: The value of each node is greater than or equal to the value of its left and right child nodes, called the Big Top heap, or the value of each node is less than or equal to the value of its left and right child nodes, called the small top heap.

2. Heap Sequencing

The basic idea of heap sequencing (using heaps, such as a large top heap): To construct a sequence of orders to be sorted into a large top heap. At this point, the maximum value of the entire sequence is the root node of the heap top. Remove it (in fact, swap it with the end element of the heap array, at which point the element at the end is the maximum), and then reconstruct the remaining n-1 sequence into a heap, which gives the minor value of the n elements. With this repeated execution, an ordered sequence can be obtained.


1. How to build a heap from an unordered sequence?

2. How do I adjust the remaining elements to become a new heap after the top element of the output heap?

//constructing the largest heapvoidMaxheapfixdown (int*p,intIintlength) {    intj =2* i +1; inttemp =P[i];  while(j<length) {        if(j +1<length && P[j]<p[j +1])            ++J; if(temp>P[j]) Break; Else{P[i]=P[j]; I=J; J=2* i +1; }} P[i]=temp;}//Heap SortvoidHeapsort (int*p,intlength) {     for(inti = length/2-1; I >=0; i--) {Maxheapfixdown (P, I, length); }     for(inti = length-1; I >=1; i--) {swap (p[i], p[0]); Maxheapfixdown (P,0, i); cout<<"the value of I:"<< I <<"Sort by:"; Ergodic (P,9); }}

Six. Merge sort (Merging sort)

Merge sort is a sort method that is realized by merging thoughts. Principle: Assuming that the initial sequence contains n records, it can be regarded as n ordered sub-sequence, each subsequence length is 1, and then 22 merges, to get [N/2] length 2 or 1 ordered sub-sequence; again 22 merge ...., so repeat, until a sequence of length n is ordered, called 2-way merge sort.

Seven. Fast sorting (Quick sort)

The basic idea of fast sorting is: to split the pending records into two separate parts by a sort of sequence, in which some of the recorded keywords are smaller than those of the other records, then the two parts of the records can be sorted separately in order to achieve the order.

//Quick SortvoidQuickSort (int*p,intLintR) {    if(l<r) {inti = l, j = r, x =P[l];  while(I <j) { while(I < J && P[j] >= x)//find the first number less than x from right to leftj--; if(I <j) P[i++] =P[j];  while(I < J && p[i]< X)//find the first number greater than or equal to x from left to righti++; if(I <j) P[j--] =P[i]; } P[i]=x; QuickSort (P, L, I-1);//Recursive invocationQuickSort (p, i +1, R); }}


Reference: "Big liar data Structure"

[Data Structure (ii)] C + + simple implementation of seven kinds of sorting algorithms

Related Article

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.