各种数组排序方法

来源:互联网 发布:windows 10 iso bt 编辑:程序博客网 时间:2024/04/28 02:07

1、问题描述

    数组排序(即按某种特定的顺序排列数据,如升序或降序)是最重要的计算应用之一,银行用帐号对所有的支票进行能够排序,并根据排序结果准备月底的财务报告,学校学生成绩管理系统用数组排序的方法将考试成绩从高到低进行排名,数组排序方法很多,有直接插入排序、冒泡排序、快速排序、直接选择排序,下面来详细介绍这四种基本的排序方法及其实现

2、方法总结

   1)直接插入排序:数据表A中每个元素距其最终位置不远,数据表A按关键字值基本有序,可用此方法排序较快。

   2)冒泡排序法:将较小的值“上浮”到数组顶部,而较大值“下沉”到数组底部,这种排序技术要比较好几趟,每一趟要比较连续的数组元素对,如果某对数值是按升序排序的(或者这两个值相等),那就保持原样,如果某对数组是按降序排列的,就要交换它们的值。

   3)快速排序法:快速排序是对冒泡排序的一种改进。它的基本思想是:通过一躺排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按次方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。

   4)直接选择排序法:直接选择排序的作法是:第一趟扫描所有数据,选择其中最小的一个与第一个数据互换;第二趟从第二个数据开始向后扫描,选择最小的与第二个数据互换;依次进行下去,进行了(n-1)趟扫描以后就完成了整个排序过程。它比起冒泡排序有一个优点就是不用不断的交换。

3、算法实现

    1)直接插入法实现

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3.   
  4. int main()  
  5. {  
  6.         void InsertSort(int [],int);  
  7.         int a[7]={8,10,2,3,1,7,13};  
  8.         int i;  
  9.         InsertSort(a,7);  
  10.         for(i=0;i<7;i++)  
  11.            printf("%4d",a[i]);  
  12.         getch();  
  13. }  
  14. void InsertSort(int a[],int count)  
  15. {  
  16.         int i,j,temp;  
  17.         for(i=1;i<count;i++)     
  18.         {  
  19.            temp=a[i];  
  20.            j=i-1;  
  21.            while(a[j]>temp && j>=0)  
  22.            {  
  23.              a[j+1]=a[j];  
  24.               j--;  
  25.            }  
  26.            if(j!=(i-1))       
  27.              a[j+1]=temp;  
  28.          }  
  29. }  

   2)冒泡法实现

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. int main()  
  4. {  
  5.          void BubbleSort(int []);  
  6.          int a[10];  
  7.          int i,j,temp;  
  8.          printf("Input tem integer numbers for a[10]:");  
  9.          for(i=0;i<10;i++)  
  10.             scanf("%d,",&a[i]);  
  11.          printf("\n");  
  12.          BubbleSort(a);  
  13.          printf("The sorted array is:\n");  
  14.             for(j=0;j<10;j++)  
  15.                  printf("%d,",a[j]);  
  16.          printf("\n\n");  
  17.          getch();  
  18. }  
  19.   
  20. void BubbleSort(int array[])  
  21. {  
  22.          int i,j,temp;  
  23.            for(j=0;j<9;j++)  
  24.               for(i=0;i<9-j;i++)  
  25.                  if(array[i]>array[i+1])  
  26.                   {  
  27.                       temp=array[i];  
  28.                       array[i]=array[i+1];  
  29.                       array[i+1]=temp;  
  30.                    }  
  31. }  

    3)快速排序法实现

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. #define Max 8  
  4.   
  5. int main()  
  6. {  
  7.          void QuickSort(int a[],int p,int r);  
  8.          int a[]={2,8,7,1,3,5,6,4};  
  9.          QuickSort(a,1,Max);  
  10.          printf(" The sorted array is :");  
  11.             for(int i=0;i<Max;i++)  
  12.                printf("%d,",a[i]);  
  13.          printf("\n");  
  14.          getch();  
  15. }  
  16.   
  17. void QuickSort(int a[],int p,int r)  
  18. {  
  19.          int Partition(int a[],int p,int r);  
  20.          if(p<r)  
  21.          {  
  22.             int q=Partition(a,p,r);  
  23.             QuickSort(a,p,q-1);  
  24.             QuickSort(a,q+1,r);  
  25.          }  
  26. }  
  27.   
  28. int Partition(int a[],int p,int r)  
  29. {  
  30.          int i=p-1;  
  31.          int x=a[r-1];  
  32.             for(int j=p;j<r;j++)  
  33.             {  
  34.                if(a[j-1]<=x)  
  35.                 {  
  36.                    i=i+1;  
  37.                    int temp;  
  38.                    temp=a[j-1];  
  39.                    a[j-1]=a[i-1];  
  40.                    a[i-1]=temp;  
  41.                  }  
  42.              }  
  43.          int temp;  
  44.          temp=a[i];  
  45.          a[i]=a[r-1];  
  46.          a[r-1]=temp;  
  47.          return i+1;  
  48. }  

    4)直接选择法实现

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3.   
  4. int main()  
  5. {  
  6.          void ChooseSort(int []);  
  7.          int i,j,a[10];  
  8.          printf("Input ten integer numbers for a[10]: ");  
  9.             for(i=0;i<10;i++)  
  10.                scanf("%d,",&a[i]);  
  11.          printf("\n");  
  12.          ChooseSort(a);  
  13.          printf("The sorted array is:\n");  
  14.             for(j=0;j<10;j++)  
  15.                printf("%d,",a[j]);  
  16.          printf("\n\n");  
  17.          getch();  
  18. }  
  19.   
  20. void ChooseSort(int array[])  
  21. {  
  22.          int j,temp,*p1,*p2;  
  23.          for(p1=array;p1<array+9;p1++)  
  24.             {   
  25.               j++;  
  26.               for(p2=array+j;p2<=array+9;p2++)  
  27.                 if(*p2<*p1)  
  28.                   {  
  29.                      temp=*p2;  
  30.                      *p2=*p1;  
  31.                      *p1=temp;  
  32.                   }  
  33.             }  
  34. }  

4、各种方法比较

    1)时间性能比较

    按平均的时间性能来分,四种类排序方法时间复杂度分别为:
    直接插入排序法:O(n^2)

    冒泡排序法:O(n^2)
    快速排序法:O(nlogn)

    直接选择排序法:O(n^2)
    时间复杂度为O(n^2)的有:直接插入排序、起泡排序和简单选择排序,其中以直接插入为最好,特别是对那些对关键字近似有序的记录序列尤为如此;当待排记录序列按关键字顺序有序时,直接插入排序和起泡排序能达到O(n)的时间复杂度;而对于快速排序而言,这是最不好的情况,此时的时间性能蜕化为O(n2),因此是应该尽量避免的情况。

    2)排序方法的稳定性能比较

    1.稳定的排序方法指的是,对于两个关键字相等的记录,它们在序列中的相对位置,在排序之前和经过排序之后,没有改变。
    2.当对多关键字的记录序列进行LSD方法排序时,必须采用稳定的排序方法。
    3.对于不稳定的排序方法,只要能举出一个实例说明即可。
    4.快速排序是不稳定的排序方法。

0 0
原创粉丝点击