c 排序 总结

插入排序:

#include <stdio.h>
#include
<sys/time.h>
#include
<unistd.h>
#include
<stdlib.h>


void
change_node (
int data[], int head, int start, int end )
{
int j =0;
for(j = end; j > start; j--)
{
data[j]
= data[j-1];
}
data[start]
= head;
return ;
}
/* ----- end of function change_node ----- */

void
insert_data (
int data[], int num)
{
int i =0;
for(i = num -1; i >=0 ; i--)
{
if((data[num] > data[i]))
{
if(i == num -1)
{
break;
}
change_node(data, data[num], i
+1, num);
break;
}
else
{
if(i ==0)
{
change_node(data, data[num],
0, num);
}
}
}
return ;
}
/* ----- end of function insert_data ----- */

void
sort (
int data[], int nums)
{
int i =1;
for(i =1; i < nums ; i++)
{
insert_data(data, i);
}
return ;
}
/* ----- end of function sort ----- */

int
main()
{
int lens =100000;
int a[100000];
int i =0;
for(i =0; i < lens; i++ )
{
a[i]
= rand();
}
struct timeval tv1, tv2;
double sec =0;
gettimeofday(
&tv1, 0);

sort(a,lens);

gettimeofday(
&tv2, 0);
sec
= (double)(tv2.tv_sec - tv1.tv_sec) + (double)(tv2.tv_usec - tv1.tv_usec) /1000000;
printf(
"time1: %f\n", sec);

return0;
}

时间性能:

 

time1: 23.330364

 

快速排序:

 

#include <stdio.h>
#include
<sys/time.h>
#include
<unistd.h>
#include
<stdlib.h>

void swap(int*a,int*b)
{
int temp;
temp
=*a;
*a=*b;
*b=temp;
}

int qSort_pos(int a[],int low,int high)
{
int temp=a[low],i=low,j=high;
while(i<j)
{
while((a[j]>=temp)&&(i<j)) j--;
if(i<j) {swap(&a[i],&a[j]);i++;}
while((a[i]<=temp)&&(i<j)) i++;
if(i<j) {swap(&a[i],&a[j]);j--;}
}
return i;
}


void qSort(int a[],int low,int high)
{
int pos;
if(low<high)
{
pos
=qSort_pos(a,low,high);
qSort(a,low,pos
-1);
qSort(a,pos
+1,high);
}
}

void quicksort(int l[],int n)
{
qSort(l,
0,n -1); //第一个作为枢轴 ,从第一个排到第n个
}

int
main()
{
int lens =100000;
int a[100000];
int i =0;
for(i =0; i < lens; i++ )
{
a[i]
= rand();
}


for(i =0; i <10; i++)
{
printf(
"%d\n",a[i]);
}
struct timeval tv1, tv2;
double sec =0;
gettimeofday(
&tv1, 0); 

quicksort(a,lens);

gettimeofday(&tv2, 0);
sec 
= (double)(tv2.tv_sec - tv1.tv_sec) + (double)(tv2.tv_usec - tv1.tv_usec) /1000000;
printf(
"time1: %f\n", sec);
printf(
"\n\n");

for(i =0; i <10; i++)
{
printf(
"%d\n",a[i]);
}



return0;
}

 

时间性能:

time1: 0.051894

 

指针

#include <stdio.h>
#include
<sys/time.h>
#include
<unistd.h>
#include
<stdlib.h>

void swap(int*a,int*b)
{
int temp;
temp
=*a;
*a=*b;
*b=temp;
}

int qSort_pos(int* a[],int low,int high)
{
int temp=*a[low],i=low,j=high;
while(i<j)
{
while((*a[j]>=temp)&&(i<j)) j--;
if(i<j) {swap(a[i],a[j]);i++;}
while((*a[i]<=temp)&&(i<j)) i++;
if(i<j) {swap(a[i],a[j]);j--;}
}
return i;
}

void qSort(int* a[],int low,int high)
{
int pos;
if(low<high)
{
pos
=qSort_pos(a,low,high);
qSort(a,low,pos
-1);
qSort(a,pos
+1,high);
}
}

void quicksort(int* l[],int n)
{
qSort(l,
0,n -1); //第一个作为枢轴 ,从第一个排到第n个
}

int
main()
{
int lens =100;
int* a[100];
int i =0;
for(i =0; i < lens; i++ )
{
a[i]
= (int*)malloc(sizeof(int));
*a[i] = rand();
}

quicksort(a,lens);


return0;
}
posted @ 2011-07-05 21:40  wangkangluo1  阅读(302)  评论(0编辑  收藏  举报