程序(1):
<span style="font-size:14px;color:#000000;">#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAXNUM 100000
void selectsort(int a[], int n)
{
int i, j, k, tmp;
for(i = 0; i < n-1; i++)
{
k = i;
for(j = i+1; j < n; j++)
{
if(a[j] < a[k])
k = j;
}
if(k != j)
{
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
}
int main()
{
int x[MAXNUM];
int n = 0;
double t1,t2;
FILE *fp;
fp = fopen("numbers.txt", "r");
while(fscanf(fp, "%d", &x[n])!=EOF)
n++;
printf("数据量:%d, 开始排序....", n);
t1=time(0);
selectsort(x, n);
t2=time(0);
printf("用时 %d 秒!", (int)(t2-t1));
fclose(fp);
return 0;
}</span>
<img src="http://img.blog.csdn.net/20150914164935627?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
程序(2):
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAXNUM 100000
void quicksort(int data[],int first,int last)
{
int i, j, t, base;
if (first>last)
return;
base=data[first];
i=first;
j=last;
while(i!=j)
{
while(data[j]>=base && i<j)
j--;
while(data[i]<=base && i<j)
i++;
/*交换两个数*/
if(i<j)
{
t=data[i];
data[i]=data[j];
data[j]=t;
}
}
data[first]=data[i];
data[i]=base;
quicksort(data,first,i-1);
quicksort(data,i+1,last);
}
int main()
{
int x[MAXNUM];
int n = 0;
double t1,t2;
FILE *fp;
fp = fopen("numbers.txt", "r");
while(fscanf(fp, "%d", &x[n])!=EOF)
n++;
printf("数据量:%d, 开始排序....", n);
t1=time(0);
quicksort(x, 0, n-1);
t2=time(0);
printf("用时 %d 秒!", (int)(t2-t1));
fclose(fp);
return 0;
}
<img src="http://img.blog.csdn.net/20150914165135209?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<span style="font-size:18px;">总结:</span>
<span style="font-size:18px;"> 第一个复杂度是指数型增长,第二个复杂度是线性增长,所以第一个是运行比较慢的。
</span>