代码:
- #include <iostream>
- using namespace std;
- template <typename T>
- void sawp(T a[],int first,int second){
- T temp;
- temp = a[first];
- a[first] = a[second];
- a[second] = temp;
- }
- template <typename T>
- int sort(T a[],int low,int high){
- if(low < high){
- int lt = low,i=low+1,gt = high;
- int temp = a[low];
- while(i <= gt){
- if(a[i] < temp){
- sawp(a,lt++,i++);
- }else if(a[i] > temp){
- sawp(a,i,gt--);
- }else{
- i++;
- }
- }
- sort(a,low,lt-1);
- sort(a,gt+1,high);
- }
- }
- int main() {
- int a[] = {2,2,2,2,2,0,0,0,0,0,0,1,5};
- //int a[] = {2,0,1,5};
- int len = sizeof(a)/sizeof(int);
- sort(a,0,len-1);
- for (int i = 0; i < len; ++i)
- cout << a[i] << " ";
- cout << endl;
- }
|