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