定义数组,然后编程排序 数组排序的几种方法!

排序:这是冒泡法的程序:
#include<stdio.h>
void sort(int array[],int size)
{
int i,j,temp;

for(i=0;i<size-1;i++)
        for(j=i+1;j<size;j++)
               if(array[i]>array[j])
               {
                      temp=array[i];
                      array[i]=array[j];
                      array[j]=temp;
               }
}
void main()
{
int i;
int a[10]={1,33,78,34,787,213,132,35,32,21};
sort(a,10);
for(i=0;i<10;i++)
        printf("m",a[i]);
}

这是选择法的程序:
#include<stdio.h>
void sort(int array[],int size)
{
int i,j,k,temp;
for(i=0;i<size-1;i++)
{
        k=i;

        for(j=i+1;j<size;j++)
               if(array[k]>array[j])
                      k=j;
        temp=array[k];
        array[k]=array[i];
        array[i]=temp;
}
}
void main()
{
int a[]={12,43,54,23,32,65,87,2,34,54};
int i;
sort(a,10);
for(i=0;i<10;i++)
        printf("%d ",a[i]);
}
这是一个快速排序的程序:
#include<stdio.h>
void quick_sort(int array[],int first,int last)//first,last分别为数组下标的范围;
{
int temp,low,high,list_separator;
low=first;
high=last;

    list_separator=array[(first+last)/2];//中间数;
do{
        while(array[low]<list_separator)//中间数与前半部分比较;
               low++;
        while(array[high]>list_separator)// 中间数与后半部分比较;
               high--;
        if(low<=high)//前半部分与后半部分交换;
        {
               temp=array[low];
               array[low++]=array[high];
               array[high--]=temp;
        }
}while(low<=high);
if(first<high)
        quick_sort(array,first,high);//利用递归的办法,实行循环;
if(low<last)
        quick_sort(array,low,last);//利用递归的办法,实行循环;
}
void main()
{
int a[9]={12,23,34,65,93,32,21,9,8};
quick_sort(a,0,8);
for(int i=0;i<9;i++)
        printf("%d ",a[i]);
printf(" ");
}
这是一个希尔排序的程序:
#include<stdio.h>
void shell_sort(int array[],int size)
{
int temp,gap,i,flag;
gap=size/2;
do{
        do{
               flag=0;


               for(i=0;i<size-gap;i++)
                      if(array[i]>array[i+gap])
                      {
                             temp=array[i];
                             array[i]=array[i+gap];
                             array[i+gap]=temp;
                          flag=1;
                      }
        }while(flag);
}while(gap=gap/2);//赋值完后,gap的值}
void main()
{
int array[]={1,2,32,43,64,7654,321,42,23,97,56,32,78,45,32},i;
shell_sort(array,15);
for(i=0;i<15;i++)
        printf("d",array[i]);
}

 

 数组排序方法很多,有直接插入排序、冒泡排序、快速排序、直接选择排序,下面来详细介绍这四种基本的排序方法及其实现。

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

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

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

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

程序1:直接插入法实现对数组的排序!
#include<stdio.h>
#include<conio.h>

int main()
{
        void InsertSort(int a[],int);
        int a[7]={8,10,2,3,1,7,13};
        int i;
        InsertSort(a,7);
        for(i=0;i<7;i++)
           printf("M",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<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:快速排序法实现对数组的排序!
#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:直接选择法实现对数组的排序!

#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;
                  }
            }
}

各种排序方法的综合比较:

一、时间性能

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

二、排序方法的稳定性能

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

posted @ 2014-03-19 14:47  義丨往昔灬miller  阅读(3027)  评论(0编辑  收藏  举报