2020-06-21 23:58:00 +08:00
|
|
|
/**
|
2020-06-21 02:33:36 +08:00
|
|
|
* \addtogroup sorting Sorting Algorithm
|
|
|
|
* @{
|
2020-06-21 23:58:00 +08:00
|
|
|
* \file
|
|
|
|
* \brief [Heap Sort Algorithm
|
2020-06-21 02:33:36 +08:00
|
|
|
* (HEAP SORT)](https://en.wikipedia.org/wiki/Heapsort) implementation
|
2020-06-21 23:58:00 +08:00
|
|
|
*
|
|
|
|
* \author [Ayaan Khan](http://github.com/ayaankhan98)
|
|
|
|
*
|
|
|
|
* \details
|
2020-06-21 02:34:51 +08:00
|
|
|
* heapsort is a comparison-based sorting algorithm.
|
|
|
|
* Heapsort can be thought of as an improved selection sort:
|
|
|
|
* like selection sort, heapsort divides its input into a sorted
|
|
|
|
* and an unsorted region, and it iteratively shrinks the unsorted
|
|
|
|
* region by extracting the largest element from it and inserting
|
|
|
|
* it into the sorted region. Unlike selection sort,
|
|
|
|
* heapsort does not waste time with a linear-time scan of the
|
|
|
|
* unsorted region; rather, heap sort maintains the unsorted region
|
|
|
|
* in a heap data structure to more quickly find the largest element
|
2020-06-21 23:58:00 +08:00
|
|
|
* in each step.
|
2020-06-21 02:34:51 +08:00
|
|
|
*
|
2020-06-21 02:33:36 +08:00
|
|
|
* Time Complexity - O(nlog(n))
|
2020-06-21 23:58:00 +08:00
|
|
|
*
|
|
|
|
*/
|
2019-12-16 20:21:16 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2020-06-21 23:58:00 +08:00
|
|
|
/**
|
2020-06-21 02:34:51 +08:00
|
|
|
*
|
2020-06-21 23:58:00 +08:00
|
|
|
* Utility Lambda function to print the array after
|
|
|
|
* sorting.
|
|
|
|
*
|
|
|
|
* @param arr array to be printed
|
|
|
|
* @param sz size of array
|
|
|
|
*
|
|
|
|
*/
|
2020-06-21 02:34:51 +08:00
|
|
|
auto printArray = [](int *arr, int sz) {
|
|
|
|
for (int i = 0; i < sz; i++) std::cout << arr[i] << " ";
|
2020-06-21 23:58:00 +08:00
|
|
|
std::cout << "\n";
|
2020-06-21 02:33:36 +08:00
|
|
|
};
|
2020-06-21 23:58:00 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2020-06-21 02:34:51 +08:00
|
|
|
* The heapify procedure can be thought of as building a heap from
|
|
|
|
* the bottom up by successively sifting downward to establish the
|
|
|
|
* heap property.
|
|
|
|
*
|
|
|
|
* @param arr array be to sorted
|
|
|
|
* @param
|
2020-06-21 23:58:00 +08:00
|
|
|
*/
|
2020-06-21 02:34:51 +08:00
|
|
|
void (*heapify)(int *arr, int n, int i) = [](int *arr, int n, int i) {
|
2020-01-06 19:23:29 +08:00
|
|
|
int largest = i;
|
2020-06-21 02:34:51 +08:00
|
|
|
int l = 2 * i + 1;
|
|
|
|
int r = 2 * i + 2;
|
2020-01-06 19:23:29 +08:00
|
|
|
|
2020-06-21 23:58:00 +08:00
|
|
|
if (l < n && arr[l] > arr[largest])
|
2020-01-06 19:23:29 +08:00
|
|
|
largest = l;
|
|
|
|
|
2020-06-21 23:58:00 +08:00
|
|
|
if (r < n && arr[r] > arr[largest])
|
2020-01-06 19:23:29 +08:00
|
|
|
largest = r;
|
|
|
|
|
|
|
|
if (largest != i) {
|
2020-06-21 23:58:00 +08:00
|
|
|
std::swap(arr[i], arr[largest]);
|
|
|
|
heapify(arr, n, largest);
|
2019-12-16 20:21:16 +08:00
|
|
|
}
|
2020-06-21 02:33:36 +08:00
|
|
|
};
|
2020-01-04 16:33:56 +08:00
|
|
|
|
2020-06-21 23:58:00 +08:00
|
|
|
/**
|
2020-06-21 02:33:36 +08:00
|
|
|
* heapSort lambda function utilizes heapify procedure to sort
|
2020-06-21 23:58:00 +08:00
|
|
|
* the array
|
|
|
|
*
|
|
|
|
* @param arr array to be sorted
|
|
|
|
* @param n size of array
|
|
|
|
*
|
|
|
|
*/
|
2020-06-21 02:34:51 +08:00
|
|
|
auto heapSort = [](int *arr, int n) {
|
|
|
|
for (int i = n - 1; i >= 0; i--) heapify(arr, n, i);
|
2020-01-04 16:33:56 +08:00
|
|
|
|
2020-06-21 02:34:51 +08:00
|
|
|
for (int i = n - 1; i >= 0; i--) {
|
2020-06-21 23:58:00 +08:00
|
|
|
std::swap(arr[0], arr[i]);
|
|
|
|
heapify(arr, i, 0);
|
2019-12-16 20:21:16 +08:00
|
|
|
}
|
2020-06-21 02:33:36 +08:00
|
|
|
};
|
2020-01-04 16:33:56 +08:00
|
|
|
|
2020-06-21 02:33:36 +08:00
|
|
|
/** Main function */
|
|
|
|
int main() {
|
2020-06-21 23:58:00 +08:00
|
|
|
int arr[] = {-10, 78, -1, -6, 7, 4, 94, 5, 99, 0};
|
|
|
|
int sz = sizeof(arr) / sizeof(arr[0]); // sz - size of array
|
|
|
|
printArray(arr, sz); // displaying the array before sorting
|
2020-06-21 02:34:51 +08:00
|
|
|
heapSort(arr, sz); // calling heapsort to sort the array
|
2020-06-21 23:58:00 +08:00
|
|
|
printArray(arr, sz); // display array after sorting
|
|
|
|
return 0;
|
2019-12-16 20:21:16 +08:00
|
|
|
}
|
2020-06-21 02:33:36 +08:00
|
|
|
/** @} */
|