Download code

Jump to: navigation, search

Back to Merge_sort_(C_Plus_Plus)

Download for Windows: single file, zip

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

mergesort.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/Merge_sort_(C_Plus_Plus)?oldid=17108
 14 */
 15 
 16 
 17 // This implementation is originally based on a public domain D program by
 18 // David Medlock (http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/32113).
 19 
 20 #include<vector>
 21 #include<string>
 22 #include<algorithm>
 23 #include<iostream>
 24 
 25 
 26 
 27 template<typename IT, typename VT> void insert(IT begin, IT end, const VT &v)
 28 {
 29 	using ::std::swap;
 30 	while (begin+1!=end && *(begin+1)<v) {
 31 		swap(*begin, *(begin+1));
 32 		++begin;
 33 	}
 34 	swap(*begin, v);
 35 }
 36 
 37 
 38 template<typename IT> void merge(IT begin, IT begin_right, IT end)
 39 {
 40 	for(;begin<begin_right; ++begin) {
 41 		if(*begin_right < *begin) {
 42 			typename std::iterator_traits<IT>::value_type v;
 43 			using ::std::swap;
 44 			swap(v, *begin);
 45 			swap(*begin, *begin_right);
 46 			insert(begin_right, end, v);
 47 		}
 48 	}
 49 }
 50 
 51 template<typename IT> void mergesort(IT begin, IT end)
 52 {
 53 	size_t size(end-begin);
 54 	if(size<2) return;
 55 
 56 	IT begin_right=begin+size/2;
 57 
 58 	mergesort(begin, begin_right);
 59 	mergesort(begin_right, end);
 60 	merge(begin, begin_right, end);
 61 }
 62 
 63 
 64 template<typename T> void print(const T &data)
 65 {
 66 	std::cout<<" "<<data;
 67 }
 68 
 69 
 70 int main()
 71 {
 72 	std::vector<std::string> v(10);
 73 	v[0]="Paris";
 74 	v[1]="London";
 75 	v[2]="Stockholm";
 76 	v[3]="Berlin";
 77 	v[4]="Oslo";
 78 	v[5]="Rome";
 79 	v[6]="Madrid";
 80 	v[7]="Tallinn";
 81 	v[8]="Amsterdam";
 82 	v[9]="Dublin";
 83 
 84 	std::cout<<"v before qsort: ";
 85 	std::for_each(v.begin(), v.end(), print<std::string>);
 86 	std::cout<<'\n';
 87 
 88 	mergesort(v.begin(), v.end());
 89 
 90 	std::cout<<"v after mergesort: ";
 91 	std::for_each(v.begin(), v.end(), print<std::string>);
 92 	std::cout<<'\n';
 93 
 94 	int a[]={3,8,0,6,7,4,2,1,9,3,1,8,3,9,2,0,9};
 95 	int *a_end=a+sizeof a/sizeof(int);
 96 	
 97 
 98 	std::cout<<"a before mergesort: ";
 99 	std::for_each(a, a_end, print<int>);
100 	std::cout<<'\n';
101 
102 	mergesort(a, a_end);
103 
104 	std::cout<<"a after mergesort: ";
105 	std::for_each(a, a_end, print<int>);
106 	std::cout<<'\n';
107 
108 	return 0;
109 }
110 
111 


hijacker
hijacker
hijacker
hijacker

build.log

 1 /tmp/litprog2638790/mergesort.cpp: In function 'void insert(IT, IT, const VT&) [with IT = __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >, VT = std::basic_string<char>]':
 2 /tmp/litprog2638790/mergesort.cpp:46:4:   instantiated from 'void merge(IT, IT, IT) [with IT = __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >]'
 3 /tmp/litprog2638790/mergesort.cpp:60:2:   instantiated from 'void mergesort(IT, IT) [with IT = __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >]'
 4 /tmp/litprog2638790/mergesort.cpp:88:30:   instantiated from here
 5 /tmp/litprog2638790/mergesort.cpp:34:2: error: no matching function for call to 'swap(std::basic_string<char>&, const std::basic_string<char>&)'
 6 /tmp/litprog2638790/mergesort.cpp:34:2: note: candidates are:
 7 /usr/include/c++/4.6/bits/move.h:122:5: note: template<class _Tp> void std::swap(_Tp&, _Tp&)
 8 /usr/include/c++/4.6/bits/move.h:136:5: note: template<class _Tp, unsigned int _Nm> void std::swap(_Tp (&)[_Nm], _Tp (&)[_Nm])
 9 /usr/include/c++/4.6/bits/stl_vector.h:1321:5: note: template<class _Tp, class _Alloc> void std::swap(std::vector<_Tp, _Alloc>&, std::vector<_Tp, _Alloc>&)
10 /usr/include/c++/4.6/bits/basic_string.h:2658:5: note: template<class _CharT, class _Traits, class _Alloc> void std::swap(std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>&)
11 /tmp/litprog2638790/mergesort.cpp: In function 'void insert(IT, IT, const VT&) [with IT = int*, VT = int]':
12 /tmp/litprog2638790/mergesort.cpp:46:4:   instantiated from 'void merge(IT, IT, IT) [with IT = int*]'
13 /tmp/litprog2638790/mergesort.cpp:60:2:   instantiated from 'void mergesort(IT, IT) [with IT = int*]'
14 /tmp/litprog2638790/mergesort.cpp:102:20:   instantiated from here
15 /tmp/litprog2638790/mergesort.cpp:34:2: error: no matching function for call to 'swap(int&, const int&)'
16 /tmp/litprog2638790/mergesort.cpp:34:2: note: candidates are:
17 /usr/include/c++/4.6/bits/move.h:122:5: note: template<class _Tp> void std::swap(_Tp&, _Tp&)
18 /usr/include/c++/4.6/bits/move.h:136:5: note: template<class _Tp, unsigned int _Nm> void std::swap(_Tp (&)[_Nm], _Tp (&)[_Nm])
19 /usr/include/c++/4.6/bits/stl_vector.h:1321:5: note: template<class _Tp, class _Alloc> void std::swap(std::vector<_Tp, _Alloc>&, std::vector<_Tp, _Alloc>&)
20 /usr/include/c++/4.6/bits/basic_string.h:2658:5: note: template<class _CharT, class _Traits, class _Alloc> void std::swap(std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>&)


hijacker
hijacker
hijacker
hijacker