1 | #include <functional> |
---|
2 | #include <Utils/utils.hh> |
---|
3 | // #include <algorithm> |
---|
4 | // #include <iterator> |
---|
5 | // #include <iostream> |
---|
6 | |
---|
7 | template <class T> void sort(T *array, int begin, int end) |
---|
8 | { |
---|
9 | T pivot = array[begin]; |
---|
10 | int i = begin + 1, j = end, k = end; |
---|
11 | T t; |
---|
12 | |
---|
13 | while (i < j) { |
---|
14 | if (array[i] < pivot) i++; |
---|
15 | else if (array[i] > pivot) { |
---|
16 | j--; k--; |
---|
17 | t = array[i]; |
---|
18 | array[i] = array[j]; |
---|
19 | array[j] = array[k]; |
---|
20 | array[k] = t; } |
---|
21 | else { |
---|
22 | j--; |
---|
23 | swap(array[i], array[j]); |
---|
24 | } } |
---|
25 | i--; |
---|
26 | swap(array[begin], array[i]); |
---|
27 | if (i - begin > 1) |
---|
28 | sort(array, begin, i); |
---|
29 | if (end - k > 1) |
---|
30 | sort(array, k, end); |
---|
31 | } |
---|
32 | template void sort<int>(int *array, int begin, int end); |
---|
33 | template void sort<long>(long *array, int begin, int end); |
---|
34 | template void sort<float>(float *array, int begin, int end); |
---|
35 | template void sort<double>(double *array, int begin, int end); |
---|
36 | //-------------------------------------------------------------------- |
---|
37 | |
---|
38 | template <class T1,class T2> |
---|
39 | void sort(T1 *array, T2 *matchingArray, int begin, int end) |
---|
40 | { |
---|
41 | T1 pivot = array[begin]; |
---|
42 | int i = begin + 1, j = end, k = end; |
---|
43 | T1 t; |
---|
44 | T2 tt; |
---|
45 | |
---|
46 | while (i < j) { |
---|
47 | if (array[i] < pivot) i++; |
---|
48 | else if (array[i] > pivot) { |
---|
49 | j--; k--; |
---|
50 | t = array[i]; |
---|
51 | array[i] = array[j]; |
---|
52 | array[j] = array[k]; |
---|
53 | array[k] = t; |
---|
54 | tt = matchingArray[i]; |
---|
55 | matchingArray[i] = matchingArray[j]; |
---|
56 | matchingArray[j] = matchingArray[k]; |
---|
57 | matchingArray[k] = tt; |
---|
58 | } |
---|
59 | else { |
---|
60 | j--; |
---|
61 | swap(array[i], array[j]); |
---|
62 | swap(matchingArray[i], matchingArray[j]); |
---|
63 | } |
---|
64 | } |
---|
65 | i--; |
---|
66 | swap(array[begin], array[i]); |
---|
67 | swap(matchingArray[begin], matchingArray[i]); |
---|
68 | if (i - begin > 1) |
---|
69 | sort(array, matchingArray, begin, i); |
---|
70 | if (end - k > 1) |
---|
71 | sort(array, matchingArray, k, end); |
---|
72 | } |
---|
73 | template void sort<int,int>(int *array, int *arrayB, int begin, int end); |
---|
74 | template void sort<int,float>(int *arrayA, float *arrayB, int begin, int end); |
---|
75 | template void sort<float,int>(float *array, int *arrayB, int begin, int end); |
---|
76 | template void sort<float,float>(float *array, float *arrayB, int begin, int end); |
---|
77 | //-------------------------------------------------------------------- |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | // template< typename BidirectionalIterator, typename Compare > |
---|
83 | // void quick_sort( BidirectionalIterator first, BidirectionalIterator last, Compare cmp ) { |
---|
84 | // if( first != last ) { |
---|
85 | // BidirectionalIterator left = first; |
---|
86 | // BidirectionalIterator right = last; |
---|
87 | // BidirectionalIterator pivot = left++; |
---|
88 | |
---|
89 | // while( left != right ) { |
---|
90 | // if( cmp( *left, *pivot ) ) { |
---|
91 | // ++left; |
---|
92 | // } else { |
---|
93 | // while( (left != --right) && cmp( *pivot, *right ) ) |
---|
94 | // ; |
---|
95 | // std::iter_swap( left, right ); |
---|
96 | // } |
---|
97 | // } |
---|
98 | |
---|
99 | // --left; |
---|
100 | // std::iter_swap( first, left ); |
---|
101 | |
---|
102 | // quick_sort( first, left, cmp ); |
---|
103 | // quick_sort( right, last, cmp ); |
---|
104 | // } |
---|
105 | // } |
---|
106 | |
---|
107 | // template< typename BidirectionalIterator > |
---|
108 | // inline void quick_sort( BidirectionalIterator first, BidirectionalIterator last ) { |
---|
109 | // quick_sort( first, last, |
---|
110 | // std::less_equal< typename std::iterator_traits< BidirectionalIterator >::value_type >() |
---|
111 | // ); |
---|
112 | // } |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | // void sort(float *arr, int beg, int end) |
---|
118 | // { |
---|
119 | // if (end > beg + 1) |
---|
120 | // { |
---|
121 | // float piv = arr[beg]; |
---|
122 | // int l = beg + 1, r = end; |
---|
123 | // while (l < r) |
---|
124 | // { |
---|
125 | // if (arr[l] <= piv) |
---|
126 | // l++; |
---|
127 | // else |
---|
128 | // swap(arr[l], arr[--r]); |
---|
129 | // } |
---|
130 | // swap(arr[--l], arr[beg]); |
---|
131 | // sort(arr, beg, l); |
---|
132 | // sort(arr, r, end); |
---|
133 | // } |
---|
134 | // } |
---|
135 | |
---|