赞
踩
一、结果显示
二、QuickSort.h
- #include<iostream>
-
- using namespace std;
-
- template<class T>
- class QuickSort
- {
- private:
- T a[10] = {'d','a','f','c','v','b','a','g','s','z'};
- int size = sizeof(a);
- public:
- QuickSort()
- {
- cout<<"size = "<<size<<endl;
- }
-
- void QuickSort00()
- {
- T b[10];
- for(int i = 0; i < size; i++)
- b[i] = a[i];
- QuickSort0( b, 0, size-1);
- cout<<"Quick Sort>>";
- print(b);
- }
-
- void QuickSort0(T a[], int left,int right )
- {
- if(left < right)
- {
- T pivot = median( a, left, right);
- int i = left,j = right-1;
- for( ; ; )
- {
- while(a[++i] < pivot) {}
- while(pivot < a[--j]) {}
- if(i < j) //Repeat until i > j ;
- swap(a[i], a[j]);
- else
- break;
- }
- swap(a[i], a[right-1]); //The bigger at Position "i" !!!
- QuickSort0( a, left, i-1);
- QuickSort0( a, i+1, right);
- }
-
- }
-
- const T & median(T a[], int left,int right)
- {
- int center = (left + right)/2;
- if(a[left] > a[center])
- swap(a[left], a[center]);
- if(a[center] > a[right])
- swap(a[center], a[right]);
- if(a[left] > a[right])
- swap(a[right], a[left]);
- swap(a[center], a[right-1]); //Place the pivot at right-1 , the right is biggest !!
- return a[right-1];
- }
-
- void vprint()
- {
- cout<<"The original char array is : ";
- print(a);
- }
- void print(char a[10])
- {
- for(int i = 0 ; i < size ; i++)
- {
- cout<<a[i]<<" ";
- }
- cout<<endl;
- }
- };
三、main.cpp
- #include "QuickSort.h"
-
- using namespace std;
-
- int main()
- {
- QuickSort<char> T;
- T.vprint();
- T.QuickSort00();
- cout << "Hello world!" << endl;
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。