Download code

Jump to: navigation, search

Back to Quicksort_(C_Plus_Plus)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

quicksort.cpp

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Quicksort_(C_Plus_Plus)?oldid=19541
 14 */
 15 
 16 #include <iostream>
 17 #include <algorithm>	// std::swap()
 18 #include <vector>
 19 template<typename IT> 
 20 IT partition(IT begin, IT end, IT pivot)
 21 {
 22 	typename std::iterator_traits<IT>::value_type piv(*pivot);
 23 	std::iter_swap(pivot, end-1);
 24 	IT store=begin;
 25 	for(IT it=begin, e=end-1; it!=e; ++it) {
 26 		if(*it<=piv) {
 27 			std::iter_swap(store, it);
 28 			++store;
 29 		}
 30 	}
 31 	std::iter_swap(end-1, store);
 32 	return store;
 33 }
 34 
 35 template<typename IT>
 36 IT pivot_median(IT begin, IT end)
 37 {
 38 	IT pivot(begin+(end-begin)/2);
 39 	if(*begin<=*pivot && *(end-1)<=*begin) pivot=begin;
 40 	else if(*(end-1)<=*pivot && *begin<=*(end-1)) pivot=end-1;
 41 	return pivot;
 42 }
 43 
 44 template<typename IT>
 45 struct pivot_random {
 46 	pivot_random() {
 47 		srand(time(NULL));
 48 	}
 49 	IT operator()(IT begin, IT end) {
 50 		return begin+(rand()%(end-begin));
 51 	}
 52 };
 53 
 54 
 55 template<typename IT, typename PF> 
 56 void quick_sort(IT begin, IT end, PF pf)
 57 {
 58 	if(end-1>begin) {
 59 		IT pivot=pf(begin, end);
 60 
 61 		pivot=partition(begin, end, pivot);
 62 
 63 		quick_sort(begin, pivot, pf);
 64 		quick_sort(pivot+1, end, pf);
 65 	}
 66 }
 67 template<typename IT>
 68 void quick_sort(IT begin, IT end)
 69 {
 70 	quick_sort(begin, end, pivot_random<IT>());
 71 }
 72 
 73 template<typename T> void print(const T &data)
 74 {
 75 	std::cout<<" "<<data;
 76 }
 77 
 78 int main()
 79 {
 80 	std::vector<std::string> v(10);
 81 	v[0]="Paris";
 82 	v[1]="London";
 83 	v[2]="Stockholm";
 84 	v[3]="Berlin";
 85 	v[4]="Oslo";
 86 	v[5]="Rome";
 87 	v[6]="Madrid";
 88 	v[7]="Tallinn";
 89 	v[8]="Amsterdam";
 90 	v[9]="Dublin";
 91 
 92 	std::cout<<"v before qsort: ";
 93 	std::for_each(v.begin(), v.end(), print<std::string>);
 94 	std::cout<<'\n';
 95 
 96 	quick_sort(v.begin(), v.end());
 97 
 98 	std::cout<<"v after qsort: ";
 99 	std::for_each(v.begin(), v.end(), print<std::string>);
100 	std::cout<<'\n';
101 
102 	int a[]={3,8,0,6,7,4,2,1,9,3,1,8,3,9,2,0,9};
103 	int *a_end=a+sizeof a/sizeof(int);
104 
105 	std::cout<<"a before qsort: ";
106 	std::for_each(a, a_end, print<int>);
107 	std::cout<<'\n';
108 
109 	quick_sort(a, a_end, pivot_random<int*>());
110 	
111 	std::cout<<"a after qsort: ";
112 	std::for_each(a, a_end, print<int>);
113 	std::cout<<'\n';
114 
115 	return 0;
116 }
117 


hijacker
hijacker
hijacker
hijacker