关于快速排序原理,这里不再多写,这里将其写出模板来适应多种数据类型。 //交换数据 template<typename T> void Swap(T& a, T& b) { T c(a); a = b; b = c; } //排序 template<typename T> void QuickSort(T* arr, int left, int right) { int l, r; T v; l = left; r = right; v = arr[(left + right) / 2]; while (l <= r) { while (arr[l] < v) ++l; while (arr[r] > v) --r; if (l <= r) { Swap<T>(arr[l], arr[r]); ++l; --r; } } if (l == r) l++; if (left < r) QuickSort<T>(arr, left, r); if (l < right) QuickSort<T>(arr, l, right); } int main() { int i; int a[]={1,4,2,66,22,23,5,6,8,7,99,54}; QuickSort<int>(a, 0, 11); for(i=0;i<12;i++) printf("%d/n",a[i]); double b[]={5.1,4.5,2.2,66.5,22.6,23,5,6,18.1,7,99,54}; QuickSort<double>(b, 0, 11); for(i=0;i<12;i++) printf("%f/n",b[i]); return 0; }