#include
#include
#include
#define N 20
void myqsort(int *a, int l, int r);
int partition(int *a, int l, int r);
void swap(int *x, int *y);
void printArray(int *a, int len);
void init(int *a, int len);
int main()
{
int a[N] ;//= {8033, 1139, 4735, 14470};
while(getchar() != EOF){
init(a, N);
printArray(a, N);
srand((unsigned)time(NULL));
printf("\n");
myqsort(a, 0, N-1);
printf("\n");
printArray(a, N);
}
return 0;
}
void init(int *a, int len)
{
int i = 0;
srand((unsigned)(time(NULL)));
while(i < len){
a[i] = rand();
i++;
}
}
void myqsort(int *a, int l, int r)
{
if(a == NULL || l <0 || l >= r){
//printf("end\n");
return;
}
int p = partition(a, l, r);
myqsort(a, l, p);
myqsort(a, p+1 , r);
}
int partition(int *a, int l, int r)
{
int point = l + rand()%(r-l+1);
int key = a[point];
int i = l, j = r;
while(i < j){
while(a[j] > key) j--;
while(a[i] < key) i++;
if(i < j){
swap(a+i, a+j);
//i++;
//j--;
}
}
//printArray(a, N);
//printf("point=%d\ti=%d\tl=%d\tr=%d\n", point, i, l, r);
return i;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void printArray(int *a, int len)
{
int i = 0;
while(i < len){
printf("%d\t", *(a+i));
i++;
}
printf("\n");
}