Sort summary one (common eight sorts)

Source: Internet
Author: User

1 Bubble Sort:

voidBubble (int*a,intN)//The simple implementation of the bubbling algorithm { for(intI=0; i<n-1; i++)    {         for(intj=0; j<n-i-1; j + +)        {            if(a[j]>a[j+1])            {                inttemp=A[j]; A[J]=a[j+1]; A[j+1]=temp; }        }    }}

Improved bubble sort 1, add marker bit

voidBubble_1 (int*a,intN//improved bubbling algorithm to increase marker bit{    BOOLpos=false;  for(intI=0; i<n-1; i++) {pos=true;  for(intj=0; j<n-i-1; j + +)        {            if(a[j]>a[j+1])            {                inttemp=A[j]; A[J]=a[j+1]; A[j+1]=temp; POS=false; }        }        if(POS)return; }}

2 Select Sort:

voidSelect (int*a,intN//Simple Selection Sorting{    intmin;  for(intj=0; j<n-1; j + +) {min=J;  for(inti=j+1; i<n;i++)        {        if(a[min]>a[i]) min=i; }        inttemp=A[min]; A[min]=A[j]; A[J]=temp; }}

Improved bi-directional sorting

voidSelect_1 (int*a,intN//Bi- directional sorting{    intMin,max;  for(intj=0;j< (n1)/2; j + +) {min=J; Max=n-j-1;  for(inti=j+1; i<n-j;i++)        {            if(a[min]>a[i]) min=i; if(a[max]<A[i]) Max=i; }        inttemp=A[min]; A[min]=A[j]; A[J]=temp; Temp=A[max]; A[max]=a[n-j-1]; A[n-j-1]=temp; }}
3 Insert Sort

voidInsert (int*a,intN//Direct Insertion Algorithm{    intTemp,count; if(n<2)return;  for(intI=1; i<n;i++) {Temp=A[i]; Count=i-1;  while(count>=0&&A[count]>temp) {A[count+1]=A[count]; Count--; } A[count+1]=temp; }}
4 Hill Sort (Improved insert sort):

voidShellsort (int*a,intN//Insert sort with step selection{     for(intdiv=n/2;d iv>=1;d iv/=2)    {         for(inti=div;i<n;i++)        {             for(intj=i;j-div>=0&&a[j]<a[j-div]&&j>=0; j-=Div)/*The main point here, rather than the insertion sort, is a bubble sort with a step selection, not that it is wrong, just not good intuitive understanding. */            {                inttemp=A[j]; A[J]=a[j-Div]; A[j-div]=temp; }        }            }}
voidShellsort_1 (int*a,intN//Use the Insert sort model for intuitive writing. {     for(intdiv=n/2;d iv>=1;d iv/=2)    {         for(inti=div;i<n;i++)        {            intTemp,count; Temp=A[i]; Count=i-Div;  while(count>=0&&A[count]>temp) {A[count+div]=A[count]; Count-=Div; } A[count+div]=temp; }    }}

Variable steps can be written separately as a function

voidShellinsert (int*a,intNintDiv) {         for(inti=div;i<n;i++)        {            intTemp,count; Temp=A[i]; Count=i-Div;  while(count>=0&&A[count]>temp) {A[count+div]=A[count]; Count-=Div; } A[count+div]=temp; }}
5 Merge Sort:

The merge sort is the first contact in the introduction to the algorithm, and is also the most basic algorithm for understanding recursion, with O (NLOGN) time complexity and O (n) space complexity.

http://blog.csdn.net/hguisu/article/details/7776068

Sort summary one (common eight sorts)

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.