数组排序方法及C实现的总结

      

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

int main()
{
        void InsertSort(int [],int);
        int a[7]={8,10,2,3,1,7,13};
        int i;
        InsertSort(a,7);
        for(i=0;i<7;i++)
           printf("%4d",a[i]);
        getch();
}
void InsertSort(int a[],int count)
{
        int i,j,temp;
        for(i=1;i<count;i++)   
        {
           temp=a[i];
           j=i-1;
           while(a[j]>temp && j>=0)
           {
             a[j+1]=a[j];
              j--;
           }
           if(j!=(i-1))     
             a[j+1]=temp;
         }
}

   2)冒泡法实现

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

void BubbleSort(int array[])
{
         int i,j,temp;
           for(j=0;j<9;j++)
              for(i=0;i<9-j;i++)
                 if(array[i]>array[i+1])
                  {
                      temp=array[i];
                      array[i]=array[i+1];
                      array[i+1]=temp;
                   }
}

    3)快速排序法实现

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

int main()
{
         void QuickSort(int a[],int p,int r);
         int a[]={2,8,7,1,3,5,6,4};
         QuickSort(a,1,Max);
         printf(" The sorted array is :");
            for(int i=0;i<Max;i++)
               printf("%d,",a[i]);
         printf("\n");
         getch();
}

void QuickSort(int a[],int p,int r)
{
         int Partition(int a[],int p,int r);
         if(p<r)
         {
            int q=Partition(a,p,r);
            QuickSort(a,p,q-1);
            QuickSort(a,q+1,r);
         }
}

int Partition(int a[],int p,int r)
{
         int i=p-1;
         int x=a[r-1];
            for(int j=p;j<r;j++)
            {
               if(a[j-1]<=x)
                {
                   i=i+1;
                   int temp;
                   temp=a[j-1];
                   a[j-1]=a[i-1];
                   a[i-1]=temp;
                 }
             }
         int temp;
         temp=a[i];
         a[i]=a[r-1];
         a[r-1]=temp;
         return i+1;
}

    4)直接选择法实现

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

int main()
{
         void ChooseSort(int []);
         int i,j,a[10];
         printf("Input ten integer numbers for a[10]: ");
            for(i=0;i<10;i++)
               scanf("%d,",&a[i]);
         printf("\n");
         ChooseSort(a);
         printf("The sorted array is:\n");
            for(j=0;j<10;j++)
               printf("%d,",a[j]);
         printf("\n\n");
         getch();
}

void ChooseSort(int array[])
{
         int j,temp,*p1,*p2;
         for(p1=array;p1<array+9;p1++)
            { 
              j++;
              for(p2=array+j;p2<=array+9;p2++)
                if(*p2<*p1)
                  {
                     temp=*p2;
                     *p2=*p1;
                     *p1=temp;
                  }
            }
}

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值