formatting source-code for e464ddac36

This commit is contained in:
github-actions 2020-06-20 18:34:51 +00:00 committed by Ayaan Khan
parent 38db7fdec0
commit 11728ee231

View File

@ -33,9 +33,8 @@
* @param sz size of array * @param sz size of array
* *
*/ */
auto printArray = [] (int *arr, int sz) { auto printArray = [](int *arr, int sz) {
for (int i = 0 ; i < sz ; i++) for (int i = 0; i < sz; i++) std::cout << arr[i] << " ";
std::cout << arr[i] <<" ";
std::cout << "\n"; std::cout << "\n";
}; };
@ -45,11 +44,13 @@ auto printArray = [] (int *arr, int sz) {
* the bottom up by successively sifting downward to establish the * the bottom up by successively sifting downward to establish the
* heap property. * heap property.
* *
* @param arr array be to sorted
* @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 largest = i;
int l = 2*i + 1; int l = 2 * i + 1;
int r = 2*i + 2; int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest]) if (l < n && arr[l] > arr[largest])
largest = l; largest = l;
@ -71,11 +72,10 @@ void(*heapify)(int *arr, int n, int i) = [] (int *arr, int n, int i) {
* @param n size of array * @param n size of array
* *
*/ */
auto heapSort = [] (int *arr, int n) { auto heapSort = [](int *arr, int n) {
for (int i = n-1 ; i >= 0; i--) for (int i = n - 1; i >= 0; i--) heapify(arr, n, 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]); std::swap(arr[0], arr[i]);
heapify(arr, i, 0); heapify(arr, i, 0);
} }
@ -86,7 +86,7 @@ int main() {
int arr[] = {-10, 78, -1, -6, 7, 4, 94, 5, 99, 0}; int arr[] = {-10, 78, -1, -6, 7, 4, 94, 5, 99, 0};
int sz = sizeof(arr) / sizeof(arr[0]); // sz - size of array int sz = sizeof(arr) / sizeof(arr[0]); // sz - size of array
printArray(arr, sz); // displaying the array before sorting 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 printArray(arr, sz); // display array after sorting
return 0; return 0;
} }