C language a variety of common sorts (direct insertion sort, binary insertion sort, bubble sort, select sort, heap sort)

Source: Internet
Author: User
Tags sorts

001 #include <stdio.h>
002 #include <stdlib.h>
003 int main ()
004 {
005 int l[11]={0,70,38,65,97,76,93,1,15,68,64}; 0 do not sort just as sentinels, or temporarily swap space for use.
006 int i=0;
007 printf ("Unordered pre-sequence: \ n");
008 for (i=1;i<=10;i++)
009 {
010 printf ("%d", l[i]);
011 }
012 printf ("\ n-------------------------------------\ n");
013 Insertsort (L); Direct Insert Sort
014 Binsertsort (L); Binary Insert Sort
015 Bubblesort (L);//Bubble sort
016 Selectsort (L); Select sort
017 Heapsort (l,10); Heap Sort
018 printf ("Sorted after: \ n");
019 for (i=1;i<=10;i++)
020 {
021 printf ("%d", l[i]);
022 }
023 return 0;
024 }
025 void Insertsort (int l[11])//Insert sort the worst and average performance of the insert sort is O (n^2)
12W {
12V int i=0,j=0;
028 for (I=2;i<=10;++i)
029 {
030 L[0]=l[i]; Surveillance whistle.
031 for (j=i-1; L[0]<L[J];--J)
032 {
033 L[J+1]=L[J]; Record move back
034 }
035 l[j+1]=l[0];//Insert Record
036 }
037 }
038 void Binsertsort (int l[11])//binary lookup sort
039 {
040 int i=0,j=0,low=0,high=0,m=0;
041 for (I=2;i<=10;++i)
042 {
043 L[0]=l[i]; Surveillance whistle.
044 Low=1;high=i-1;
045 while (Low<=high) {
046 M= (Low+high)/2; Half
14V if (L[0]<l[m])
048 High=m-1;
049 Else
050 low=m+1;
051 }
052 for (j=i-1; L[0]<L[J];--J)
053 L[J+1]=L[J]; Record move back
054 L[HIGH+1]=L[0]; Insert
055 }
056 }
057 void Bubblesort (int l[11])//bubble sort complexity is O (n^2)
058 {
059 int i=0,j=0;
060 for (I=1;i<=10;++i)
061 {
062 for (J=1;J<=10-I;++J)
063 {
064 if (l[j]>l[j+1])
065 {
066 L[0]=L[J];
067 L[J]=L[J+1]; Exchange two values with auxiliary space
068 L[J+1]=L[0];
16X }
070 }
071 }
072 }
073 void Selectsort (int l[11])//Select sort complexity is: O (n^2)
074 {
075 int i=0,j=0,min_idx=0;
076 for (I=1;i<=10;++i)
077 {
078 Min_idx=i;
079 for (J=I+1;J<=10;++J)
080 {
081 if (L[j]<l[min_idx])
082 {
083 Min_idx=j;
084 }
085 }
086 if (min_idx!=i)
087 {
088 L[0]=l[i];
089 L[I]=L[MIN_IDX]; Exchange minimum value and L[i]
090 L[MIN_IDX]=L[0];
091 }
092 }
093 }
094 void Heapsort (
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.