Download code

From LiteratePrograms

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 /* Copyright (c) 2010 the authors listed at the following URL, and/or
  2 the authors of referenced articles or incorporated external code:
  3 http://en.literateprograms.org/Quicksort_(C_Plus_Plus)?action=history&offset=20080623154833
  4 
  5 Permission is hereby granted, free of charge, to any person obtaining
  6 a copy of this software and associated documentation files (the
  7 "Software"), to deal in the Software without restriction, including
  8 without limitation the rights to use, copy, modify, merge, publish,
  9 distribute, sublicense, and/or sell copies of the Software, and to
 10 permit persons to whom the Software is furnished to do so, subject to
 11 the following conditions:
 12 
 13 The above copyright notice and this permission notice shall be
 14 included in all copies or substantial portions of the Software.
 15 
 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 23 
 24 Retrieved from: http://en.literateprograms.org/Quicksort_(C_Plus_Plus)?oldid=13758
 25 */
 26 
 27 #include <iostream>

 28 #include <algorithm>	// std::swap()

 29 #include <vector>

 30 
 31 template<typename IT> 
 32 IT partition(IT begin, IT end, IT pivot)
 33 {
 34 	typename std::iterator_traits<IT>::value_type piv(*pivot);
 35 	std::swap(*pivot, *(end-1));
 36 	IT store=begin;
 37 	for(IT it=begin; it!=end-1; ++it) {
 38 		if(*it<=piv) {
 39 			std::swap(*store, *it);
 40 			++store;
 41 		}
 42 	}
 43 	std::swap(*(end-1), *store);
 44 	return store;
 45 }
 46 
 47 
 48 template<typename IT>
 49 IT pivot_median(IT begin, IT end)
 50 {
 51 	IT pivot(begin+(end-begin)/2);
 52 	if(*begin<=*pivot && *(end-1)<=*begin) pivot=begin;
 53 	else if(*(end-1)<=*pivot && *begin<=*(end-1)) pivot=end-1;
 54 	return pivot;
 55 }
 56 
 57 
 58 template<typename IT>
 59 struct pivot_random {
 60 	pivot_random() {
 61 		srand(time(NULL));
 62 	}
 63 	IT operator()(IT begin, IT end) {
 64 		return begin+(rand()%(end-begin));
 65 	}
 66 };
 67 
 68 
 69 
 70 template<typename IT, typename PF> 
 71 void quick_sort(IT begin, IT end, PF pf)
 72 {
 73 	if((end-begin)>1) {
 74 		IT pivot=pf(begin, end);
 75 
 76 		pivot=partition(begin, end, pivot);
 77 
 78 		quick_sort(begin, pivot, pf);
 79 		quick_sort(pivot+1, end, pf);
 80 	}
 81 }
 82 
 83 template<typename IT>
 84 void quick_sort(IT begin, IT end)
 85 {
 86 	quick_sort(begin, end, pivot_random<IT>());
 87 }
 88 
 89 
 90 template<typename T> void print(const T &data)
 91 {
 92 	std::cout<<" "<<data;
 93 }
 94 
 95 int main()
 96 {
 97 	std::vector<std::string> v(10);
 98 	v[0]="Paris";
 99 	v[1]="London";
100 	v[2]="Stockholm";
101 	v[3]="Berlin";
102 	v[4]="Oslo";
103 	v[5]="Rome";
104 	v[6]="Madrid";
105 	v[7]="Tallinn";
106 	v[8]="Amsterdam";
107 	v[9]="Dublin";
108 
109 	std::cout<<"v before qsort: ";
110 	std::for_each(v.begin(), v.end(), print<std::string>);
111 	std::cout<<'\n';
112 
113 	quick_sort(v.begin(), v.end());
114 
115 	std::cout<<"v after qsort: ";
116 	std::for_each(v.begin(), v.end(), print<std::string>);
117 	std::cout<<'\n';
118 
119 	int a[]={3,8,0,6,7,4,2,1,9,3,1,8,3,9,2,0,9};
120 	int *a_end=a+sizeof a/sizeof(int);
121 
122 	std::cout<<"a before qsort: ";
123 	std::for_each(a, a_end, print<int>);
124 	std::cout<<'\n';
125 
126 	quick_sort(a, a_end, pivot_random<int*>());
127 	
128 	std::cout<<"a after qsort: ";
129 	std::for_each(a, a_end, print<int>);
130 	std::cout<<'\n';
131 
132 	return 0;
133 }
134 
135 


Views
Personal tools