quick.hxx
Go to the documentation of this file.
1 /*===========================================================================================================
2  *
3  * HUC - Hurna Core
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/Hurna/Hurna-Core/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 huc
26 {
27  namespace sort
28  {
39  template <typename IT, typename Compare = std::less_equal<typename std::iterator_traits<IT>::value_type>>
40  void QuickSort(const IT& begin, const IT& end)
41  {
42  const auto distance = static_cast<const int>(std::distance(begin, end));
43  if (distance < 2)
44  return;
45 
46  auto pivot = begin + (rand() % distance); // Pick Random Pivot € [begin, end]
47  auto newPivot = Partition<IT, Compare>(begin, pivot, end); // Proceed partition
48 
49  QuickSort<IT, Compare>(begin, newPivot); // Recurse on first partition
50  QuickSort<IT, Compare>(newPivot + 1, end); // Recurse on second partition
51  }
52  }
53 }
54 
55 #endif // MODULE_SORT_QUICK_HXX
void QuickSort(const IT &begin, const IT &end)
Definition: quick.hxx:40