Created
February 5, 2014 00:42
-
-
Save kotaroito/8815370 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
#define N 10 | |
int sort[N]; | |
void quickSort(int bottom, int top, int *data) | |
{ | |
int lower, upper, div, temp; | |
if ( bottom >= top ) | |
return; | |
div = data[bottom]; | |
for ( lower = bottom, upper = top; lower < upper; ) { | |
while ( lower <= upper && data[lower] <= div ) { | |
lower++; | |
} | |
while ( lower <= upper && data[upper] > div ) { | |
upper--; | |
} | |
if ( lower < upper ) { | |
temp = data[lower]; | |
data[lower] = data[upper]; | |
data[upper] = temp; | |
} | |
} | |
temp = data[bottom]; | |
data[bottom] = data[upper]; | |
data[upper] = temp; | |
quickSort(bottom, upper - 1, data); | |
quickSort(upper + 1, top, data); | |
} | |
int main(void) | |
{ | |
int i; | |
srand((unsigned int) time(NULL)); | |
for ( i = 0; i < N; i++ ) { | |
sort[i] = rand(); | |
printf("%010d ", sort[i]); | |
} | |
printf("\n\nstart...\n\n"); | |
quickSort(0, N - 1, sort); | |
for ( i = 0; i < N; i++ ) { | |
printf("%010d ", sort[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment