Common sort algorithm implementation (direct insert sort, bubble sort, quick sort, build large selection sort)

Source: Internet
Author: User

Common sort algorithm implementations (direct insert sort, bubble sort, quick sort, build big selection Sort), and several next time write on.

#include   "stdio.h" #include   "stdlib.h" #include   "malloc.h" #include  <iostream>using  namespace std; #define &NBSP;N&NBSP;10VOID&NBSP;SHOWARR (int a[]); Void insertsort (Int a []);     //directly into sort Void bubblesort (int a[]);     //bubble sort void  quicksort (int a[],int ,int);     //Quick sort Void simpleselectionsort (int  a[]);     //Simple Selection sort (select one with first interchange at a time) Int main () {    int a[n] ={3,1,14,5,2,56,67,54,77,34};    showarr (a);//    insertsort (a);     //Direct Insert Sort//    bubblesort (a);     //bubble Sort      //quicksort (a,0,n-1);     //Quick Sort     //cout<< " This is the result of the direct insert Sort "<<endl;    //showarr (a);    //cout<<endl;     simpleselectiOnsort (a);     system ("pause");     return 0;} Print List Void showarr (Int a[n]) {    for (int i=0;i<n;i++)          cout<<a[i]<< " ";     cout<<endl;} Direct Insert sort Void insertsort (Int a[n]) {    int tmp;    int  j;    for (int i=1;i<n;i++)         if (A[i]<a[i-1])         {             tmp=a[i];    //this value to be present in TMP              for (j=i-1;tmp<a[j];j--)                  a[j+1]=a[j];    //Front override value              a[j+1]=tmp;        }    cout< < "This is the result of the direct insertion sort" <<endl;    showarr (a);     cout<<endl;} Bubble sort Void bubblesort (Int a[n]) {    int tmp;    for (int  i=0;i<n;i++)         for (int j=0;j<n-i-1;j++)          {             if (a[j]>a[j+1])             {                 tmp=a[j];                 a[j]=a[j+1];                 a[j+1]=tmp;             }        }    cout <<endl<< "This is the result of the bubbling Sort" <<endl;    showarr (a);     cout <<endl;} Quick Sort Void quicksort (int a[],int left,int right) {    if (left<right)     {        int i=left,j=right,tmp=a[i];// Each time with the first element as a base point, small in front, large in the back         while (i<j)          {            while (i<j &NBSP;&AMP;&AMP;&NBSP;A[J]&GT;TMP)                  j--;            if (I&LT;J)                 a[i++]=a[j];              while (I&LT;J&NBSP;&AMP;&AMP;&NBSP;A[I]&LT;=TMP)                 i++;             if (I&LT;J)                  a[j--]=a[i];         }        a[i]=tmp;    // Complete the first quick order and put it in its final position         quicksort (a,left,i-1);         //before the same method recursively quickly sort         quicksort (a,i+1, right);         //followed by the same method recursive quick sort     }}//simple select sort void  simpleselectionsort (int a[]) {    int min;    int  tmp;    //Temp Variable &Nbsp;   for (int i=0;i<n-1;i++)     {         min=i;    //first tentative min for the beginning of that element          for (int j=i+1;j<n;j++)             if (A[j]<a[min])                  min=j;    //find the smallest value          tmp=a[min];         //exchange with the first element         a[ min]=a[i];        a[i]=tmp;    }     cout<<endl<< "This is the result of the selection sort" <<endl;    showarr (a);     cout<<endl;}

Operation Result:

3 1 14 5 2 56 67 54 77 34 This is the result of selecting Sort 1 2 3 5 14 34 54 56 67 77 Press any key to continue ...


Common sort algorithm implementation (direct insert sort, bubble sort, quick sort, build large selection 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.