Some common sorting algorithms (C version)

Source: Internet
Author: User

1. Direct Insert sort (stable sort)

Simply put, the sequence is divided into ordered sequence and unordered sequence. Each trip is sorted by inserting the first element of the unordered sequence into an ordered sequence. R[1 I-1] <-R[I...N], each time take r[i] inserted into r[1 ... i-1].

The steps are as follows:

1> the insertion position of r[i] found in r[1 ... i-1] K (0<k<i)

2> will r[k ... i-1] All move back one bit, the K position is inserted r[i]

Improved version:

1> R[i] from right to left in r[1 ... i-1], r[j] >r[i], then r[j] move back one (j = i-1 start)

2> if R[J] <=r[i], the insertion position of j+1 for R[i]

1#include <stdio.h>2 voidInsert_sort (intA[],intlen)3 {4     intI=0, j=0, temp=0;5      for(i=1; i<len;i++)6     {7temp =A[i];8          for(j=i-1; j>=0&&temp<a[j];j--)9         {Tena[j+1]=A[j]; One         } Aa[j+1]=temp; -     } - } the  - voidPrint_array (intA[],intlen) - { -      for(intI=0; i<len;i++) +     { -printf"a[%d] =%d\n", I, A[i]); +     } Aprintf"\ n"); at } -  -  - intMain () - { -  inta[]={1,2,7,8,Ten,4, Wu, the, at} ; inprintf"before sort\n"); -Print_array (A,sizeof(a)/sizeof(a[0])); toInsert_sort (A,sizeof(a)/sizeof(a[0])); +printf"After sort\n"); -Print_array (A,sizeof(a)/sizeof(a[0])); the  return 0; *}

2. Bubble sort (Stable sort)

Bubble sort is also called bubble sort, as the name implies, is every trip, from left to right, 22 comparison, big (small) back, the last lightest bubble to the last position R[i], for the maximum or minimum value, and then the next trip, select the second big to r[i-1], to this, to the last r[1], so all ordered. (Can be incremented by decreasing)

1#include <stdio.h>2 3 /*... a[n], starting from a[0] min.,*/4 voidBUBBLE_SORT_S2B (intA[],intlen)5 {6     inti,j,temp;7      for(i=0; i<len-1; i++)8     {9          for(j=0; j<len-i-1; j + +)Ten         { One             if(a[j+1]<A[j]) A             { -temp=A[j]; -a[j]=a[j+1]; thea[j+1]=temp; -             } -         } -     } + } - voidBubble_sort_b2s (intA[],intlen) + { A     inti,j,temp; at      for(i=0; i<len-1; i++) -     { -          for(j=0; j<len-i-1; j + +) -         { -             if(a[j+1]>A[j]) -             { intemp=A[j]; -a[j]=a[j+1]; toa[j+1]=temp; +             } -         } the     } * } $ Panax Notoginseng  - voidPrint_array (intA[],intlen) the { +      for(intI=0; i<len;i++) A     { theprintf"a[%d] =%d\n", I, A[i]); +     } -printf"\ n"); $ } $  -  - intMain () the { -  inta[]={1,2,7,8,Ten,4, Wu, the, at} ;Wuyiprintf"before sort\n"); thePrint_array (A,sizeof(a)/sizeof(a[0])); -Bubble_sort_b2s (A,sizeof(a)/sizeof(a[0])); Wuprintf"After sort\n"); -Print_array (A,sizeof(a)/sizeof(a[0])); About  return 0; $}

Some common sorting algorithms (C version)

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.