1 //===-- Implementation of heap sort -----------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_STDLIB_HEAP_SORT_H
10 #define LLVM_LIBC_SRC_STDLIB_HEAP_SORT_H
12 #include "src/__support/CPP/cstddef.h"
13 #include "src/stdlib/qsort_data.h"
15 namespace LIBC_NAMESPACE_DECL
{
18 // A simple in-place heapsort implementation.
19 // Follow the implementation in https://en.wikipedia.org/wiki/Heapsort.
21 LIBC_INLINE
void heap_sort(const Array
&array
) {
22 size_t end
= array
.size();
23 size_t start
= end
/ 2;
25 auto left_child
= [](size_t i
) -> size_t { return 2 * i
+ 1; };
29 // Select the next unheapified element to sift down.
32 // Extract the max element of the heap, moving a leaf to root to be sifted
38 // Sift start down the heap.
40 while (left_child(root
) < end
) {
41 size_t child
= left_child(root
);
42 // If there are two children, set child to the greater.
43 if (child
+ 1 < end
&&
44 array
.elem_compare(child
, array
.get(child
+ 1)) < 0)
47 // If the root is less than the greater child
48 if (array
.elem_compare(root
, array
.get(child
)) >= 0)
51 // Swap the root with the greater child and continue sifting down.
52 array
.swap(root
, child
);
58 } // namespace internal
59 } // namespace LIBC_NAMESPACE_DECL
61 #endif // LLVM_LIBC_SRC_STDLIB_HEAP_SORT_H