排序 比较

// 直接插入排序.cpp : 定义控制台应用程序的入口点。
//
 
 
#include <fstream>
#include <iostream>
using namespace std;
#include<string>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <malloc.h>
 
 
#define   CLOCKS_PER_SEC ((clock_t)1000)
int Max;
//产生随机数头文件
struct  su
{
    float  s;
    float  v;
    float p;
};
 
 
 
 
void change(float  *r,float W[],int Max)
{
  for(long  i=1;i<=Max;i++)
    {
    W[i]=r[i];
    }
}
//插入排序 2 种(直接和希尔)
void insertSort (float  r[] ,float  n)//直接插入排序
{   long  j;
    for (long  i=2;i<=n;i++)
    {
        r[0]=r[i];
        j=i-1;
        while (r[0]<r[j])
        {
         r[j+1]=r[j];
         j=j-1;
        }
        r[j+1]=r[0];
    }
    
};
 
void shell(float  r[],float  n)//希尔
{
    for(long  d=n/2;d>=1;d/=2)
    {
        for(long  i=d+1;i<=n;i++){
            r[0]=r[i];
            long  j=i-d;
            while (j>0&&r[0]<r[j])
            {
                r[j+d]=r[j];
                j-=d;
 
            }
             r[j+d]=r[0];
        }
    }
}
//交换排序
//起泡排序
void buddle (float  r[],float  n)
{
    float  bound ,exchange ,t;
    exchange =n;
    while (exchange)
    {
        bound=exchange;
        exchange=0;
        for(long  i=1;i<bound;i++)
        {
           if (r[i]>r[i+1])
            {
                t=r[i];
                r[i]=r[i+1];
                r[i+1]=t;
              exchange=i;
           }
 
        }
 
    }
}
//快速排序
float  Partition (float  r[],long  first,long  end)
{
    float  t;
    while (first<end)
    {
        while (first<end&&r[first]<=r[end])end--;
        if(first<end)
        {
                t=r[first];
                r[first]=r[end];
                r[end]=t;
               first++;
        }
        while (first<end&&r[first]<=r[end])first++;
        if(first<end)
        {
                t=r[first];
                r[first]=r[end];
                r[end]=t;
                   end--;
        }
    }
        return first;
 
 
}
void QuickSort (float   r[ ], float  first, float  end )
{
    float  pivotpos;
   if(first<end){
    pivotpos = Partition (r, first, end );    
    QuickSort (r,first,pivotpos-1);       
    QuickSort (r, pivotpos+1, end );  
    
      }
}
 
void sift ( float  r[ ], float  k, float  m )
{
  long   i=k,t;   
  long  j=2*i;  
  float  temp=r[i];
    while (j<=m )   
    {
        if (j<m && r[j]<r[j+1]) j++;
        if (r[i]>r[j]) break;  
        else {
             t=r[i];
             r[i]=r[j];
             r[j]=t;
              i=j;  
              j=2*i;
        }
     }
 
}
 
void  HeapSort ( float   r[], long  n)
{
    float  t;
    for (float  i=n/2; i>=1; i--)
    {  sift(r, i, n) ;  
    /*for(float  i=1;i<=Max;i++)
    {
    cout <<W[i]<<"  ";
    }
    cout<<endl;*/
    }
    for (long  i=1; i<n; i++ )
    {
        t=r[1];
        r[1]=r[n-i+1];  
         r[n-i+1]=t;
       sift(r, 1, n-i);
    }
}
void  selectSort (float   r[],float  n){
    long  index;
    float t;
    for (long  i=1; i<n; i++)  
    {   
        index=i;         
        for (long  j=i+1; j<=n; j++)  
           if  (r[j]<r[index])  index=j;
        if (index!=i)    
        {t=r[i];
        r[i]=r[index];  
        r[index]=t;  
        }
    }
}  
 
int  main(int argc,char *argv[])
{
    
    int count=0,y=0;
       
     string str;
  float  q;
    srand(time(NULL));
   clock_t start, end;
    bool repeat;
     for (int u=0;u<argc;u++){
        Max= atoi(argv[u]);
        float  W[Max+1];
    
     fstream file("data",ios::out);
    float* p = (float  *) malloc ( sizeof(float) * (Max+1) );
    //float p[Max+1];
    cout<<"随机生成一系列无序数........"<<endl;
    cout<<"进行中......."<<endl;
    
    int tmp;
          for(int i=0;i<=Max;i++)
                p[i]=i;
                for(int j=1;j<Max;j++){
                tmp=rand()%(Max-j)+j+1;
                swap(p[j],p[tmp]);
                }
                for(int i=1;i<=Max;i++){
                 file<<p[i]<<"\t";  
                    count++;  
                     if(count%10==0)  
                       {  
                           file<<++y<<endl;
                        }  
                }
        cout<<endl<<"几种排序比较"<<endl;
        change(p,W,Max);
        start = clock();

    insertSort(W,Max);
    /*for(float  i=1;i<=Max;i++)
    {
    cout <<p[i]<<endl;
    }
    for(float  i=1;i<=Max;i++)
    {
    cout <<W[i]<<endl;
    }*/

    end =clock();
    //printf("The above segment is executed in %lf seconds.\n",elapsed_time(VIRTUAL));
    cout<<"直接插入 Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<"  秒"<<endl;
    change(p,W,Max);
     start = clock();
     shell(W,Max);
      end=clock();

     cout<<"希尔 Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<"  秒"<<endl;
    
     /*change(p);
     start = clock();
      buddle (W,Max);
       end=clock();
   cout<<"冒泡 Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<"  秒"<<endl;*/
         
     change(p,W,Max);
       start = clock();
             QuickSort (W, 1, Max );
               end=clock();
             cout<<"快速 Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<"  秒"<<endl;
            
             change(p,W,Max);
         start = clock();
HeapSort (W,Max);
  end=clock();
cout<<"堆排序 Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<"  秒"<<endl;
 change(p,W,Max);
  start = clock();
     selectSort (W,Max);
     end=clock();
cout<<"选择排序 Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<"  秒"<<endl;

     free (p);
    file.close();
    }

    return 0;
}
 
posted @ 2012-11-05 21:54  MFT  阅读(208)  评论(0编辑  收藏  举报