quick.hxx
Go to the documentation of this file.
1 /*===========================================================================================================
2  *
3  * SHA - Simple Hybesis Algorithms
4  *
5  * Copyright (c) Michael Jeulin-Lagarrigue
6  *
7  * Licensed under the MIT License, you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * https://github.com/michael-jeulinl/Simple-Hybesis-Algorithms/blob/master/LICENSE
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License is
13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and limitations under the License.
15  *
16  * The above copyright notice and this permission notice shall be included in all copies or
17  * substantial portions of the Software.
18  *
19  *=========================================================================================================*/
20 #ifndef MODULE_SORT_QUICK_HXX
21 #define MODULE_SORT_QUICK_HXX
22 
23 #include <partition.hxx>
24 
25 namespace SHA_Sort
26 {
42  template <typename IT, typename Compare = std::less_equal<typename std::iterator_traits<IT>::value_type>>
43  void QuickSort(const IT& begin, const IT& end)
44  {
45  const auto distance = static_cast<const int>(std::distance(begin, end));
46  if (distance < 2)
47  return;
48 
49  auto pivot = begin + (rand() % distance); // Pick Random Pivot € [begin, end]
50  auto newPivot = Partition<IT, Compare>(begin, pivot, end); // Proceed partition
51 
52  QuickSort<IT, Compare>(begin, newPivot); // Recurse on first partition
53  QuickSort<IT, Compare>(newPivot + 1, end); // Recurse on second partition
54  }
55 }
56 
57 #endif // MODULE_SORT_QUICK_HXX
void QuickSort(const IT &begin, const IT &end)
Definition: quick.hxx:43