mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
formatting source-code for e464ddac36
This commit is contained in:
parent
e464ddac36
commit
97bf410b36
@ -8,24 +8,24 @@
|
||||
* \author [Ayaan Khan](http://github.com/ayaankhan98)
|
||||
*
|
||||
* \details
|
||||
* 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
|
||||
* 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
|
||||
* in each step.
|
||||
*
|
||||
*
|
||||
* Time Complexity - O(nlog(n))
|
||||
*
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* Utility Lambda function to print the array after
|
||||
* sorting.
|
||||
*
|
||||
@ -33,25 +33,24 @@
|
||||
* @param sz size of array
|
||||
*
|
||||
*/
|
||||
auto printArray = [] (int *arr, int sz) {
|
||||
for (int i = 0 ; i < sz ; i++)
|
||||
std::cout << arr[i] <<" ";
|
||||
auto printArray = [](int *arr, int sz) {
|
||||
for (int i = 0; i < sz; i++) std::cout << arr[i] << " ";
|
||||
std::cout << "\n";
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* The heapify procedure can be thought of as building a heap from
|
||||
* the bottom up by successively sifting downward to establish the
|
||||
* heap property.
|
||||
*
|
||||
* 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
|
||||
* @param
|
||||
*/
|
||||
void(*heapify)(int *arr, int n, int i) = [] (int *arr, int n, int i) {
|
||||
void (*heapify)(int *arr, int n, int i) = [](int *arr, int n, int i) {
|
||||
int largest = i;
|
||||
int l = 2*i + 1;
|
||||
int r = 2*i + 2;
|
||||
int l = 2 * i + 1;
|
||||
int r = 2 * i + 2;
|
||||
|
||||
if (l < n && arr[l] > arr[largest])
|
||||
largest = l;
|
||||
@ -73,11 +72,10 @@ void(*heapify)(int *arr, int n, int i) = [] (int *arr, int n, int i) {
|
||||
* @param n size of array
|
||||
*
|
||||
*/
|
||||
auto heapSort = [] (int *arr, int n) {
|
||||
for (int i = n-1 ; i >= 0; i--)
|
||||
heapify(arr, n, i);
|
||||
auto heapSort = [](int *arr, int n) {
|
||||
for (int i = n - 1; i >= 0; i--) heapify(arr, n, i);
|
||||
|
||||
for (int i = n-1 ; i >= 0; i--) {
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
std::swap(arr[0], arr[i]);
|
||||
heapify(arr, i, 0);
|
||||
}
|
||||
@ -88,7 +86,7 @@ int main() {
|
||||
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
|
||||
heapSort(arr, sz); // calling heapsort to sort the array
|
||||
printArray(arr, sz); // display array after sorting
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user