Quick Sort:
template <class T> struct Qsort { T *a, t; void swap (int i, int j) {t=a[i]; a[i]=a[j]; a[j]=t;} void operator = (T* v) {a=v;} void sort (int L, int R) //M=midden, L=left, R=right { if (L >= R) return; int i, M=L; swap (L, (L+R)/2) for (i=L+1; i<=R; ++i) if (a[i] < a[L]) swap (++M, i); swap (L, M); sort (L, M-1); sort (M+1, R); } }; int main () { int i, a[] = {26, 8, 42, 3, 81, 14, 60, 16, 50, 18}; int n = sizeof a/ sizeof*a - 1; Qsort<int> Q; Q = a; Q.sort (0, n); for (i=0; i<=n; i++) cout <<" " << a[i]; }
沒有留言:
張貼留言