資料結構C語言幾種排序

來源:互聯網
上載者:User

標籤:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define SIZE 30000
typedef int Elemtype;
typedef struct
{
    Elemtype key;
}SQ;
void Shellsort(Elemtype a[],int n,int d[],int number)
{
    int i,j,k,m,span;
    SQ temp;
    for(m=0;m<number;m++)           /*總共要進行number輪排序*/
    {
        span=d[m];               
        for(k=0;k<span;k++)         /*每一輪排序中,全部元素被分為span 個小組*/  
        {
            for(i=k; i+span<n;i+=span)
            {
                temp.key=a[i+span];
                j=i;
                while(j>-1&&temp.key<a[j])
                {
                    a[j+span]=a[j];
                    j-=span;
                }
                a[j+span]=temp.key;
            }
        }
    }
}
void Insertsort(Elemtype a[],int n)
{
    int i,j;
    SQ temp;
    for(i=0;i<n-1;i++)
    {
        temp.key=a[i+1];
        j=i;
        while(j>-1&&temp.key<a[j])
        {
            a[j+1]=a[j];
            j--;
        }
        a[j+1]=temp.key;
    }
}
void Quicksort(Elemtype a[],int low,int high)    /*low,high分別標記待排序元素的起始位置和終止位置*/
{
    int i,j;
    SQ temp;
    i=low,j=high;
    temp.key=a[low];   /*以第一個元素為標準元素*/
    while(i<j)
    {
        while(i<j&&temp.key<=a[j])         /*數組右端掃描*/
            j--;
            if(i<j)
            {
                a[i]=a[j];
                i++;
            }
        
       while(i<j&&a[i]<temp.key)
           i++;
          if(i<j)
          {
              a[j]=a[i];
              j--;
          }
       
       
    }
    a[i]=temp.key;
    if(low<i)  Quicksort(a,low,i-1);         /*對左端集合進行遞迴*/
    if(i<high) Quicksort(a,j+1,high);        /*對右端集合進行遞迴*/   
}
void product(Elemtype a[])
{   int i;
       for(i=0;i<30000;i++)
    {    a[i]=rand();
        printf("%10d",a[i]);
        if((i+1)%6==0) printf("\n");
    }
}
int main()
{   Elemtype a[SIZE],b[5]={2500,500,100,20,1};
    clock_t   start,end;              /*typedef long clock_t;*/
    start=clock();/* srand()設定隨機種子,time()函數的傳回值是作為srand函數的參數的!*/
    product(a);
    Quicksort(a,0,SIZE-1);
    end=clock();
    printf("\n%lf\n",(double)(end-start)/CLOCKS_PER_SEC);  /*常量CLOCKS_PER_SEC,它用來表示一秒鐘會有多少個時鐘計時單元*/
    start=clock();
    product(a);
    Insertsort(a,10000);
    end=clock();   printf("\n%lf\n",(double)(end-start)/CLOCKS_PER_SEC);
    start=clock();
    product(a);
    Shellsort(a,10000,b,5);
    end=clock();   printf("\n%lf\n",(double)(end-start)/CLOCKS_PER_SEC);
    return 0;    
}

資料結構C語言幾種排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.