各种数组排序方法

秒速五厘米 2022-08-05 11:28 241阅读 0赞

1、问题描述

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

2、方法总结

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

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

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

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

3、算法实现

1)直接插入法实现

  1. #include
  2. #include
  3. int main()
  4. {
  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. }
  13. void InsertSort(int a[],int count)
  14. {
  15. int i,j,temp;
  16. for(i=1;i<count;i++)
  17. {
  18. temp=a[i];
  19. j=i-1;
  20. while(a[j]>temp && j>=0)
  21. {
  22. a[j+1]=a[j];
  23. j—;
  24. }
  25. if(j!=(i-1))
  26. a[j+1]=temp;
  27. }
  28. }

2)冒泡法实现

  1. #include
  2. #include
  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. void BubbleSort(int array[])
  20. {
  21. int i,j,temp;
  22. for(j=0;j<9;j++)
  23. for(i=0;i<9-j;i++)
  24. if(array[i]>array[i+1])
  25. {
  26. temp=array[i];
  27. array[i]=array[i+1];
  28. array[i+1]=temp;
  29. }
  30. }

3)快速排序法实现

  1. #include
  2. #include
  3. #define Max 8
  4. int main()
  5. {
  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. {
  17. int Partition(int a[],int p,int r);
  18. if(p<r)
  19. {
  20. int q=Partition(a,p,r);
  21. QuickSort(a,p,q-1);
  22. QuickSort(a,q+1,r);
  23. }
  24. }
  25. int Partition(int a[],int p,int r)
  26. {
  27. int i=p-1;
  28. int x=a[r-1];
  29. for(int j=p;j<r;j++)
  30. {
  31. if(a[j-1]<=x)
  32. {
  33. i=i+1;
  34. int temp;
  35. temp=a[j-1];
  36. a[j-1]=a[i-1];
  37. a[i-1]=temp;
  38. }
  39. }
  40. int temp;
  41. temp=a[i];
  42. a[i]=a[r-1];
  43. a[r-1]=temp;
  44. return i+1;
  45. }

4)直接选择法实现

  1. #include
  2. #include
  3. int main()
  4. {
  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. {
  20. int j,temp,*p1,*p2;
  21. for(p1=array;p1<array+9;p1++)
  22. {
  23. j++;
  24. for(p2=array+j;p2<=array+9;p2++)
  25. if(*p2<*p1)
  26. {
  27. temp=*p2;
  28. *p2=*p1;
  29. *p1=temp;
  30. }
  31. }
  32. }

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 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JS 数组排序方法 - sortFun

    在前端开发中,经常需要对数组进行排序操作。在 JavaScript 中,数组的排序方法是 `sort()`,但是它并不支持根据指定的字段进行排序。因此,我们需要自己实现一个根据

    相关 各种数组排序方法

    1、问题描述 数组排序(即按某种特定的顺序排列数据,如升序或降序)是最重要的计算应用之一,银行用帐号对所有的支票进行能够排序,并根据排序结果准备月底的财务报告,学校学生

    相关 数组排序方法sort()

    在默认情况下,sort()方法按升序排序数组项------即最小的值位于最前面,最大的值排在最后面。为了实现排序,sort()方法会调用toString()转型方法,然后比较得