[C/C++ 演算法]- 快速排序法(三)
剛才找資料時發現一個C/C++的教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。
拷貝來源:
http://openhome.cc/Gossip/AlgorithmGossip/
http://openhome.cc/Gossip/AlgorithmGossip/QuickSort3.htm
#include <stdio.h> #include <stdlib.h> #include <time.h> #define MAX 10 #define SWAP(x,y) {int t; t = x; x = y; y = t;}
int partition(int[], int, int); void quickSort(int[], int, int);
int main(void) { srand(time(NULL)); int number[MAX] = {0};
printf("排序前:"); int i; for(i = 0; i < MAX; i++) { number[i] = rand() % 100; printf("%d ", number[i]); }
quickSort(number, 0, MAX-1);
printf("\n排序後:"); for(i = 0; i < MAX; i++) printf("%d ", number[i]); printf("\n");
return 0; }
int partition(int number[], int left, int right) { int i = left - 1; int j; for(j = left; j < right; j++) { if(number[j] <= number[right]) { i++; SWAP(number[i], number[j]); } }
SWAP(number[i+1], number[right]); return i+1; }
void quickSort(int number[], int left, int right) { if(left < right) { int q = partition(number, left, right); quickSort(number, left, q-1); quickSort(number, q+1, right); } }
|
沒有留言:
張貼留言