各种数组排序方法
1、问题描述
数组排序(即按某种特定的顺序排列数据,如升序或降序)是最重要的计算应用之一,银行用帐号对所有的支票进行能够排序,并根据排序结果准备月底的财务报告,学校学生成绩管理系统用数组排序的方法将考试成绩从高到低进行排名,数组排序方法很多,有直接插入排序、冒泡排序、快速排序、直接选择排序,下面来详细介绍这四种基本的排序方法及其实现。
2、方法总结
1)直接插入排序:数据表A中每个元素距其最终位置不远,数据表A按关键字值基本有序,可用此方法排序较快。
2)冒泡排序法:将较小的值“上浮”到数组顶部,而较大值“下沉”到数组底部,这种排序技术要比较好几趟,每一趟要比较连续的数组元素对,如果某对数值是按升序排序的(或者这两个值相等),那就保持原样,如果某对数组是按降序排列的,就要交换它们的值。
3)快速排序法:快速排序是对冒泡排序的一种改进。它的基本思想是:通过一躺排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按次方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
4)直接选择排序法:直接选择排序的作法是:第一趟扫描所有数据,选择其中最小的一个与第一个数据互换;第二趟从第二个数据开始向后扫描,选择最小的与第二个数据互换;依次进行下去,进行了(n-1)趟扫描以后就完成了整个排序过程。它比起冒泡排序有一个优点就是不用不断的交换。
3、算法实现
1)直接插入法实现
- #include
- #include
- 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)冒泡法实现
- #include
- #include
- 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)快速排序法实现
- #include
- #include
- #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)直接选择法实现
- #include
- #include
- 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.快速排序是不稳定的排序方法。
还没有评论,来说两句吧...