個人相片
回應: ㄜㄜㄅ忍縮阿婭馬殺雞ㄜㄜㄜ穎全是否?
100資專0941 謝宜珊發表於2012年 03月 7日(三.) 17:07
 
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 10000


void SetData(int A[], int n)
{
srand( (unsigned) time(NULL));
int i;

for(i=0; i<n; i++)
A[i]=rand()%N;
}

void PrintData(int A[], int n)
{
int i;

for(i=0; i<n; i++)
{
if((i+1)%10!=0)
printf("%4d ", A[i]);
else
printf("%4d\n", A[i]);
}
}

void InsertionSort(int A[], int n)
{
int i, j, temp;

for(i=1; i<n; i++)
{
temp=A[i];
j=i-1;

while(temp<A[j] && j>=0)
{
A[j+1]=A[j];
j--;
}
A[j+1]=temp;
}
}

void reverse(int A[],int x[], int n)
{
int i, j;

for(i=n-1, j=0; j<n; i--, j++)
x[j]=A[i];


}


int main()
{
int A[N]={0};
int x[N]={0};
double Run_Time1, Run_Time2, Run_Time3;
clock_t S1,E1,S2,E2,S3,E3;

S1=clock();
SetData(A, N);
PrintData(A, N);
printf("\n");
InsertionSort(A, N);
PrintData(A, N);
E1=clock();
Run_Time1=((double)(E1-S1))/CLK_TCK;
printf("%lf\n", Run_Time1);

S2=clock();
InsertionSort(A, N);
PrintData(A, N);
E2=clock();
Run_Time2=((double)(E2-S2))/CLK_TCK;
printf("%lf\n", Run_Time2);

S3=clock();
reverse(A, x, N);
PrintData(x, N);
E3=clock();
Run_Time3=((double)(E3-S3))/CLK_TCK;
printf("%lf\n", Run_Time3);

system("PAUSE");
return 0;
}