퀵 정렬
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 30
clock_t start; // == (time_t start)
clock_t end;
int check(int i, int *arry)
{
int k;
bool value = true;
for(k=0; k<i; k++)
{
if( arry[k] == arry[i] )
{
value = false;
break;
}
}
return value;
}
int input(int number, int *temp)
{
int element;
int i=0;
bool cmp;
for(i=0; i<number; i++)
{
element = rand() % MAX;
temp[i] = element;
cmp = check(i, temp );
if ( cmp == false )
{
--i;
}
}
for( i=0; i<number; i++)
printf("%d ",temp[i]);
printf("\n");
return 0;
}
int sort(int front,int rear, int *arry)
{
int front_p = front;
int rear_p = rear+1;
int pivot = front_p;
int tmp;
if ( front_p < rear_p )
{
while(1)
{
while( arry[++front_p] < arry[pivot] );
while( arry[--rear_p] > arry[pivot] );
if( front_p > rear_p )
{
break;
}
tmp = arry[front_p];
arry[front_p] = arry[rear_p];
arry[rear_p] = tmp;
}
tmp = arry[rear_p];
arry[rear_p] = arry[pivot];
arry[pivot] = tmp;
sort( 0, rear_p-1, arry);
sort( rear_p+1, rear, arry);
}
return 0;
}
int output(int number, int *arry)
{
int i;
printf("Sorted array : ");
for(i=0; i<number; i++)
printf("%d ",arry[i]);
printf("\n");
return 0;
}
int main()
{
int number = 0;
int i=0;
double elapsed;
srand((unsigned)time(NULL));
printf("The number of inputs : ");
scanf("%d", &number );
int *arry = (int*)malloc( number * sizeof(int) );
for(i=0; i<number; i++)
arry[i] = 'null';
printf("inputting...\n");
input(number, arry);
printf("sorting...\n");
start = clock();
sort(0, number-1, arry);
end = clock();
elapsed = ( end - start ) / (double)CLK_TCK;
printf("outputting...\n");
output(number, arry);
printf("running time : %lf\n", elapsed );
return 0;
}
[카페자료창고]자바 문자열 추출 방법. (0) | 2011.04.13 |
---|---|
[카페자료창고]JAVA 명명법 & MFC 명명법 차이 (0) | 2011.04.05 |
[카페자료창고]Http 에러코드 뜻 (0) | 2011.04.05 |
[카페자료창고]노화와 노령화 사회 (0) | 2011.04.05 |
[카페자료창고]합병 정렬 C++코드 구현. (0) | 2011.04.05 |
댓글 영역