0% found this document useful (0 votes)
6 views2 pages

Assignment 10

Uploaded by

riddhichaskar750
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Assignment 10

Uploaded by

riddhichaskar750
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

using namespace std;

void heapify(int arr[], int n, int i) {


int largest = i;
int lftidx = 2 * i;
int rftidx = 2 * i + 1;

if (lftidx <= n && arr[lftidx] > arr[largest]) {


largest = lftidx;
}

if (rftidx <= n && arr[rftidx] > arr[largest]) {


largest = rftidx;
}

if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}

void heapsort(int arr[], int n) {

for (int i = n / 2; i >= 1; i--) {


heapify(arr, n, i);
}

for (int i = n; i > 1; i--) {

swap(arr[1], arr[i]);

heapify(arr, i - 1, 1);
}
}

void print(int arr[], int n) {


for (int i = 1; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {

int arr[6] = {-1, 54, 53, 55, 52, 50};


int n = 5;

heapsort(arr, n);

print(arr, n);
return 0;
}

You might also like