//头文件
******************************排序作业.h***************************************
/*typedef struct stackNode{ //快排非递归算法需要使用的栈节点
int low;
int high;
}stackNode;*/
void random(int a[], int n);
//打乱数组顺序
void select(int a[],int n);
//选择法
void bubble(int a[],int n);
//冒泡法
void insert(int a[],int n);
//插入法
int partition(int a[], int l, int h);
//供qsort函数调用
void q_sort(int a[], int l, int h);
//快排递归
/*void quickSortNoneRecursive(int a[],int n);*/
//快排非递归
**********************************排序作业.c*************************************
#include<stdio.h>
#include<time.h>
#include"排序作业.h"
void select(int a[],int n) //选择法
{
int i,j,temp,k;
for(i = 0;i<n-1;i++)
{
k=i;
for(j = i+1;j < n;j++)
{
if(a[k]>a[j])
k=j;
}
if(k!=i)
{
temp = a[k];
a[k] = a[i];
a[i] = temp;
}
}
}
void bubble(int a[],int n)//冒泡法
{
int i,j,temp,flag=1;
for(i = 0;i < n&&flag;i++)
{
flag=0;
for(j = 0;j < n-i;j++)
{
if(a[j]>a[j+1])
{
temp = a[j]; a[j] = a[j+1]; a[j+1] = temp;
flag=1;
}
}
}
}
void insert(int a[],int n)//插入法
{
int i,j,temp;
for(i = 1;i < n;i++)
{
temp = a[i];
for(j = i-1;j>=0&&a[j] > temp;j--)
a[j+1] = a[j];
a[j+1] = temp;
}
}
int partition(int a[], int l, int h)
{
int x = a[l];
int i = l;
int j = h+1;
int temp;
while (i<j)
{
while (a[++i]<x&&i<h);
while(a[--j]>x);
if (i<j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
a[l] = a[j];
a[j] = x;
return j;
}
void q_sort(int a[], int l, int h)//快排递归
{
if(l>=h)
return;
int q = partition(a, l, h);
q_sort(a, l, q-1);
q_sort(a, q+1, h);
}
//快排非递归算法
/*void quickSortNoneRecursive(int a[], int n){
int i, j, low, high, temp;
int top = 0;
stackNode *st = (stackNode*) malloc(sizeof(stackNode)*n);
if( !st ){
printf("内存分配故障!");
exit(0);
}
st[top].low = 0;
st[top].high = n-1;
while( top > -1){
low = st[top].low;
high = st[top].high;
top--;
i = low;
j = high;
if( low < high ){
temp = a[low];
while( i < j){
while(i<j && a[j]>=temp){
j--;
}
a[i] = a[j];
while(i<j && a[i]<=temp){
i++;
}
a[j] = a[i];
}
a[i] = temp;
if(i <= j){
top++;
st[top].low = low;
st[top].high = i-1;
top++;
st[top].low = ++i;
st[top].high = high;
}
}
}
}*/
*********************************test.c***************************************
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"排序作业.c"
#define N 100000
int main()
{
int a[N];
int b[N],c[N],d[N],e[N],i;
// int f[N];
long start,end;
srand( (unsigned)time( NULL ) );
for( i=0;i<N;i++)
{
a[i]=rand()%(N+1);
}
FILE *fp=fopen("number.dat","w");
for(i=0;i<N;i++)
{
fprintf(fp,"%6d",a[i]);
}
fclose(fp);
fp=fopen("number.dat","r");
i=0;
while(~fscanf(fp,"%6d",b+i)) i++;
fp=fopen("number.dat","r");
i=0;
while(~fscanf(fp,"%6d",c+i)) i++;
fp=fopen("number.dat","r");
i=0;
while(~fscanf(fp,"%6d",d+i)) i++;
fp=fopen("number.dat","r");
i=0;
while(~fscanf(fp,"%6d",e+i)) i++;
/* fp=fopen("number.dat","r");
i=0;
while(~fscanf(fp,"%6d",f+i)) i++;*/
start = clock();
select(b,N);
end = clock();
printf("time_1=%ldms\n",end-start);
printf("按回车键继续\n");
getchar();
start = clock();
// bubble(c,N);
end = clock();
printf("time_2=%ldms\n",end-start);
printf("按回车键继续\n");
getchar();
start = clock();
// insert(d,N);
end = clock();
printf("time_3=%ldms\n",end-start);
printf("按回车键继续\n");
getchar();
start = clock();
// q_sort(e,0,N-1);
end = clock();
printf("time_4=%ldms\n",end-start);
// start = clock();
// quickSortNoneRecursive(f,N);
// end = clock();
// printf("time_5=%ldms\n",end-start);
return 0;
}