mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
[Bug Fix] of sorting/heapsort.cpp (#873)
* formatting source-code for72c365dcd3
* Fixed Bug [munmap_chunck() core dumped] * formatting source-code forb06bbf4dc6
* fixed line spacing * fixed line spacing * fixed documentation * closed the paranthesis of line 3 * formatting source-code for8233eda889
* Bug Fix heap sort [Fresh Implementation] * formatting source-code fore464ddac36
* Bug Fix heap sort [Fresh Implementation] * formatting source-code for803981c831
* switched to normal functions from lambda * formatting source-code forced5dcd6c4
* Added template and test cases * formatting source-code for7c8617fa46
* fixed docs * fixed line spacing in tests * fix docs Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
edc17ec9e8
commit
de70c5d864
@ -1,52 +1,123 @@
|
|||||||
|
/**
|
||||||
|
* \file
|
||||||
|
* \brief [Heap Sort Algorithm
|
||||||
|
* (heap sort)](https://en.wikipedia.org/wiki/Heapsort) implementation
|
||||||
|
*
|
||||||
|
* \author [Ayaan Khan](http://github.com/ayaankhan98)
|
||||||
|
*
|
||||||
|
* \details
|
||||||
|
* Heap-sort is a comparison-based sorting algorithm.
|
||||||
|
* Heap-sort can be thought of as an improved selection sort:
|
||||||
|
* like selection sort, heap sort 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,
|
||||||
|
* heap sort 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
|
||||||
|
* in each step.
|
||||||
|
*
|
||||||
|
* Time Complexity - \f$O(n \log(n))\f$
|
||||||
|
*
|
||||||
|
*/
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void heapify(int *a, int i, int n) {
|
/**
|
||||||
int largest = i;
|
*
|
||||||
const int l = 2 * i + 1;
|
* Utility Lambda function to print the array after
|
||||||
const int r = 2 * i + 2;
|
* sorting.
|
||||||
|
*
|
||||||
|
* @param arr array to be printed
|
||||||
|
* @param sz size of array
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
void printArray(T *arr, int sz) {
|
||||||
|
for (int i = 0; i < sz; i++) std::cout << arr[i] << " ";
|
||||||
|
std::cout << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
if (l < n && a[l] > a[largest])
|
/**
|
||||||
|
*
|
||||||
|
* \addtogroup sorting Sorting Algorithm
|
||||||
|
* @{
|
||||||
|
*
|
||||||
|
* 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 to be sorted
|
||||||
|
* @param n size of array
|
||||||
|
* @param i node position in Binary Tress or element position in
|
||||||
|
* Array to be compared with it's childern
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
void heapify(T *arr, int n, int i) {
|
||||||
|
int largest = i;
|
||||||
|
int l = 2 * i + 1;
|
||||||
|
int r = 2 * i + 2;
|
||||||
|
|
||||||
|
if (l < n && arr[l] > arr[largest])
|
||||||
largest = l;
|
largest = l;
|
||||||
|
|
||||||
if (r < n && a[r] > a[largest])
|
if (r < n && arr[r] > arr[largest])
|
||||||
largest = r;
|
largest = r;
|
||||||
|
|
||||||
if (largest != i) {
|
if (largest != i) {
|
||||||
std::swap(a[i], a[largest]);
|
std::swap(arr[i], arr[largest]);
|
||||||
heapify(a, n, largest);
|
heapify(arr, n, largest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void heapsort(int *a, int n) {
|
/**
|
||||||
for (int i = n - 1; i >= 0; --i) {
|
* Utilizes heapify procedure to sort
|
||||||
std::swap(a[0], a[i]);
|
* the array
|
||||||
heapify(a, 0, i);
|
*
|
||||||
|
* @param arr array to be sorted
|
||||||
|
* @param n size of array
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
void heapSort(T *arr, int n) {
|
||||||
|
for (int i = n - 1; i >= 0; i--) heapify(arr, n, i);
|
||||||
|
|
||||||
|
for (int i = n - 1; i >= 0; i--) {
|
||||||
|
std::swap(arr[0], arr[i]);
|
||||||
|
heapify(arr, i, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void build_maxheap(int *a, int n) {
|
/**
|
||||||
for (int i = n / 2 - 1; i >= 0; --i) {
|
*
|
||||||
heapify(a, i, n);
|
* @}
|
||||||
}
|
* Test cases to test the program
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void test() {
|
||||||
|
std::cout << "Test 1\n";
|
||||||
|
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
|
||||||
|
heapSort(arr, sz); // calling heapsort to sort the array
|
||||||
|
printArray(arr, sz); // display array after sorting
|
||||||
|
assert(std::is_sorted(arr, arr + sz));
|
||||||
|
std::cout << "Test 1 Passed\n========================\n";
|
||||||
|
|
||||||
|
std::cout << "Test 2\n";
|
||||||
|
double arr2[] = {4.5, -3.6, 7.6, 0, 12.9};
|
||||||
|
sz = sizeof(arr2) / sizeof(arr2[0]);
|
||||||
|
printArray(arr2, sz);
|
||||||
|
heapSort(arr2, sz);
|
||||||
|
printArray(arr2, sz);
|
||||||
|
assert(std::is_sorted(arr2, arr2 + sz));
|
||||||
|
std::cout << "Test 2 passed\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Main function */
|
||||||
int main() {
|
int main() {
|
||||||
int n;
|
test();
|
||||||
std::cout << "Enter number of elements of array\n";
|
return 0;
|
||||||
std::cin >> n;
|
|
||||||
int a[20];
|
|
||||||
for (int i = 0; i < n; ++i) {
|
|
||||||
std::cout << "Enter Element " << i << std::endl;
|
|
||||||
std::cin >> a[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
build_maxheap(a, n);
|
|
||||||
heapsort(a, n);
|
|
||||||
std::cout << "Sorted Output\n";
|
|
||||||
for (int i = 0; i < n; ++i) {
|
|
||||||
std::cout << a[i] << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::getchar();
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user