各种排序算法比较源代码

// testsort.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <time.h>
int cp[2500];
int rplist[2500] ={1097,0,747,1395,24,2433,1208,1050,1106,378,450,...,1146};
int c=0;
void test(char*s,void (*f)(int [],int ),int a[],int n)
{
 for(int i=0;i<n;i++)
 {
  a[i]=rplist[i];
 }
 c=0;
 clock_t t=clock();
 f(a,n);

 printf("%s,count=%d time=%d",s,c,clock()-t);
 for(int i=0;i<25;i++)
 {
  if (i%25 ==0)
   printf("/n");
  printf(" %d",a[i]);

 }
 printf("/n");
}

void shellsort(int A[],int n){
 int gap, i, j;
 for(gap = n/2; gap > 0; gap = gap/2)
  for(i = gap; i < n; i++)
   for(j = i-gap; j >= 0 && A[j] > A[j+gap]; j=j-gap){
    //swap(A[j], A[j+gap]);
    int  temp=A[j];A[j]=A[j+gap];A[j+gap] =temp;
    c++;
   }
}
void bubblesort(int A[],int n)//冒泡排序
{
 int i,j,temp;
 for(i=0;i<n;i++)
  for(j=n-1;j>=i+1;j--)
  {
   if(A[j]<A[j-1])
   {
    temp=A[j];
    A[j]=A[j-1];
    A[j-1]=temp;
    c++;
   }
  }
}
void selectsort(int A[],int n)//选择排序
{
 int i,j,k,temp;
 for(i=0;i<n;i++)
 {
  k=i;
  for(j=i+1;j<=n-1;j++)
  {
   if(A[j]<A[k])
    k=j;
  }
  temp=A[i];
  A[i]=A[k];
  A[k]=temp;
  c++;
 }
}
void insertsort(int A[],int n)//插入排序
{
 int i,j,temp;
 for(i=0;i<n;i++)
 {
  temp=A[i];
  j=i-1;
  while(temp<A[j])
  {
   A[j+1]=A[j];
   c++;
   j--;
  }
  A[j+1]=temp;
 }
}

void sunxysort( int b[],int d )
{
 int p=0;
 for(int i=0 ;i< d-1 ;i++)
  for(int j=i+1; j< d ;j++)
   if (b[i]>b[j] )
   {
    p = b[j]; b[j] = b[i];b[i] = p;
    c++;
   }
}


void quicksort (int a[], int lo, int hi)
{
 //  lo is the lower index, hi is the upper index
 //  of the region of array a that is to be sorted
 int i=lo, j=hi, h;
 int x=a[(lo+hi)/2];

 //  partition
 do
 {
  while (a[i]<x) {i++; }//c++;}
  while (a[j]>x) {j--; }//c++;}
  if (i<=j)
  {
   h=a[i]; a[i]=a[j]; a[j]=h;
   i++; j--;c++;
  }
 } while (i<=j);

 //  recursion
 if (lo<j) quicksort(a, lo, j);
 if (i<hi) quicksort(a, i, hi);
}

void quicksort2 (int a[], int n)
{
 quicksort(a,0,n-1);
}
void sift(int key[],int l,int m)
{
    int i,j,x;
    i=l;
    j=2*i;
    x=key[i];
    while (j<=m)
    {
        c++;
        if(j<m && key[j]<key[j+1]) {j++;c++;c++;}
        if(x<key[j])
        {
            c++;
            key[i]=key[j];//shiftCount++;
            i=j;//shiftCount++;
            j=2*i;
        }
        else j=m+1;
    }
    key[i]=x;//shiftCount++;
}

void heapsortb(int key[],int n)
{
    int i,w;
   
    //compCount=shiftCount=0;
    for (i=n/2;i>=1;i--) {sift(key,i,n);c++;}
    for (i=n;i>=2;i--)
    {
        c++;
        w=key[i];//shiftCount++;
        key[i]=key[1];//shiftCount++;
        key[1]=w;//shiftCount++;
        sift(key,i-1,1);
    }
    return ;
}
void
heapsort(int key[] ,int n ) {
    int i, j;
    int ir = n ;   //-1
    int l = (n >> 1) + 1;
    int rra;

    for (;;) {
    if (l >1) { 
        rra = key[--l-001];
    } else {
        rra = key[ir-001];
        key[ir-001] = key[1-001]; c++;
        if (--ir == 1) {
        key[1-001] = rra;  c++;
        return;
        }
    }
    i = l;
    j = l << 1;
    while (j <= ir) {
        if (j < ir && key[j-001] < key[j+1-001]) { ++j; }
        if (rra < key[j-001]) {
        key[i-001] = key[j-001];   c++;
        j += (i = j);
        } else {
        j = ir + 1;
        }
    }
    key[i-001] = rra;   c++;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
 test("quicksort",  quicksort2,cp,2500);
 test("insertsort",  insertsort,cp,2500);
 test("shellsort", shellsort,cp,2500);
 test("selectsort", selectsort,cp,2500);
 test("bubblesort",bubblesort,cp,2500);
 test("sunxysort",sunxysort,cp,2500);
 test("heapsort",heapsort,cp,2500);
 return 0;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值