zoukankan      html  css  js  c++  java
  • 常见各种排序算法(C语言实现)

    1、冒泡排序:每次把循环过程中最大的数放到最后,循环次数每次减一。

     1 void BubbleSort(int a[],int length)
     2 {
     3     int i,temp,num;
     4     for(num=length-1;num>0;num--)
     5     {
     6         for(i=0;i<num;i++)
     7         {
     8             if(a[i]>a[i+1])
     9             {
    10                 temp=a[i];
    11                 a[i]=a[i+1];
    12                 a[i+1]=temp;
    13             }
    14         }
    15     }
    16 }

    2、选择排序:每次选择循环过程中最小的数放在最前面。

     1 void ChooseSort(int a[],int length)
     2 {
     3     int i,j,minNum,temp;
     4     for(j=0;j<length-1;j++)
     5     {
     6         minNum=j;
     7         for(i=j+1;i<length;i++)
     8         {
     9             if(a[minNum]>a[i])
    10             minNum=i;
    11         }
    12         if(minNum!=j)
    13         {
    14             temp=a[minNum];
    15             a[minNum]=a[j];
    16             a[j]=temp;
    17         }
    18     }
    19 }

    3、插入排序:前面的数已有序,后面的数不断插入,插入过程会伴随着数据位置的移动。

     1 void InsertSort(int a[],int length)
     2 {
     3     int i,j,s;
     4     for(j=0;j<length-1;j++)
     5     {
     6         s=a[j+1];
     7         for(i=j;i>=0;i--)
     8         {
     9             if(s>=a[i])
    10             break;
    11             else a[i+1]=a[i];
    12         }
    13         a[i+1]=s;
    14     }
    15 }

     4、希尔排序:插入排序的一种优化,间隔依次从length/2到1,原理与插入排序一样。

     1 void ShellSort(int a[],int length)
     2 {
     3     int i,j,g,s;
     4     g=length/2;
     5     while(g>=1)
     6     {
     7         for(i=g;i<length;i++)
     8         {
     9             s=a[i];
    10             for(j=i-g;j>=0;j-=g)
    11             {
    12                 if(s>=a[j])
    13                 break;
    14                 else
    15                 a[j+g]=a[j];
    16             }
    17             a[j+g]=s;
    18         }
    19         g/=2;
    20     }
    21 }

     5、递归排序:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 //函数的参数除了指针和数组,只能用它的值,不能随便改变,要新定义一个变量代替。
     4 //递归就是要做3,就得做2,要做2就得做1,做1结束,再做完2,再做完3。
     5 int main()
     6 {
     7     void Merge(int a[],int first,int last);
     8     int a[8]={12,6,3,7,9,4,2,1};
     9     int i=0;
    10     Merge(a,0,7);
    11     for(i=0;i<8;i++)
    12     printf("%3d",a[i]);
    13     return 0;
    14 }
    15 void Merge(int a[],int first,int last)
    16 {
    17     void MergeSort(int a[],int first,int mid,int last);
    18     if(first>=last)
    19     return;
    20     int mid=(first+last)/2;
    21     Merge(a,first,mid);
    22     Merge(a,mid+1,last);
    23     MergeSort(a,first,mid,last);
    24 }
    25 void MergeSort(int a[],int first,int mid,int last)
    26 {
    27     int begin1=first,begin2=mid+1;
    28     int end1=mid,end2=last;
    29     int length=last-first+1;
    30     int b[length];
    31     int i=0;
    32     while(begin1<=end1 && begin2<=end2)
    33     {
    34         if(a[begin1]>a[begin2])
    35         b[i++]=a[begin2++];
    36         else b[i++]=a[begin1++];
    37     }
    38     while(begin2<=end2)
    39     {
    40         b[i++]=a[begin2++];
    41     }
    42     while(begin1<=end1)
    43     {
    44         b[i++]=a[begin1++];
    45     }
    46     for(i=0;i<length;i++)
    47     {
    48         a[first+i]=b[i];
    49     }
    50 }

    6、堆排序:

     1 void heapadjust(int n[],int i,int length)
     2 {
     3     while(2*i+1<length)
     4     {   int temp=n[i];
     5         int a=2*i+1;
     6         if((a+1)<length && n[a]>n[a+1])
     7         a++;
     8         if(n[i]>n[a])
     9         n[i]=n[a];
    10         else break;
    11         n[a]=temp;
    12         i=a;
    13     }
    14 }
    15 
    16 void swap(int *a,int *b)
    17 {
    18     int temp;
    19     temp=*a;
    20     *a=*b;
    21     *b=temp;
    22 }
    23 
    24 void heapbuild(int n[],int length)
    25 {
    26     int i;
    27     for(i=(length/2)-1;i>=0;i--)
    28     {
    29         heapadjust(n,i,length);
    30     }
    31     for(i=length-1;i>0;i--)
    32     {
    33         swap(&n[i],&n[0]);
    34         heapadjust(n,0,i);
    35     }
    36 }

    7、有标志位的冒泡排序:

     1 void NewBubbleSort(int a[],int n)
     2 {
     3     int i=0,j=0,flag=1;
     4     for(i=0;i<n-1;i++)
     5     {
     6         flag=1;
     7         for(j=0;j<n-i-1;j++)
     8         {
     9             if(a[j]>a[j+1])
    10             {
    11                 swap(&a[j],&a[j+1]);
    12                 flag=0;
    13             }
    14         }
    15         if(flag)
    16         break;
    17     }
    18 }

    8、c++堆排序(最大堆、最小堆):

     1 void heapadjust(int n[],int i,int length)
     2 {
     3     while(2*i+1<length)
     4     {   int temp=n[i];
     5         int a=2*i+1;
     6         if((a+1)<length && n[a]>n[a+1])
     7         a++;
     8         if(n[i]>n[a])
     9         n[i]=n[a];
    10         else break;
    11         n[a]=temp;
    12         i=a;
    13     }
    14 }
    15 
    16 void swap(int *a,int *b)
    17 {
    18     int temp;
    19     temp=*a;
    20     *a=*b;
    21     *b=temp;
    22 }
    23 void ShiftUp(int a[],int i,int n)
    24 {
    25     int j=0,p=0;
    26     while(i>0)
    27     {
    28         j=((i&1)==0?i-1:i+1);
    29         p=(i-1)>>1;
    30         if(j<n && a[j]<a[i]) i=j;
    31         if(a[i]<a[p]) swap(&a[i],&a[p]);
    32         i=p;
    33     }
    34 }
    35 void heapbuild(int n[],int length)
    36 {
    37     int c,i=0;
    38     cout<<"最小堆选1,最大堆选2:";
    39     cin>>c;
    40     switch(c)
    41     {
    42         case 2:
    43         for(i=(length/2)-1;i>=0;i--)
    44         {
    45             heapadjust(n,i,length);
    46         }
    47         for(i=length-1;i>0;i--)
    48         {
    49             swap(&n[i],&n[0]);
    50             heapadjust(n,0,i);
    51         }
    52         break;
    53         case 1:
    54         for(i=length-1;i>(length/2)-1;i--)
    55         {
    56             ShiftUp(n,i,length);
    57         }
    58         for(i=length-1;i>0;i--)
    59         {
    60             swap(&n[0],&n[i]);
    61             ShiftUp(n,i-1,i);
    62         }
    63         break;
    64         default: break;
    65     }
    66 }

    哈哈,未完待续……

  • 相关阅读:
    Binary Tree Maximum Path Sum
    4Sum
    Candy
    Simplify Path
    Max Points on a Line
    ZigZag Conversion
    Two Sum
    Effective_STL 学习笔记(十二) 对STL线程安全性的期待现实一些
    Effective_STL 学习笔记(十一) 理解自定义分配器的正确用法
    Effective_STL 学习笔记(九) 在删除选项中仔细选择
  • 原文地址:https://www.cnblogs.com/nannanITeye/p/3000506.html
Copyright © 2011-2022 走看看