HEAP SORT
#include <stdio.h>
// Function to swap the the position of two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void heapify(int arr[], int n, int i) {
// Find largest among root, left child and right child
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
// Swap and continue heapifying if root is not largest
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
// Main function to do heap sort
void heapSort(int arr[], int n) {
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// Heap sort
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
// Heapify root element to get highest element at root again
heapify(arr, i, 0);
}
}
// Driver code
int main() {
printf("enter length of your array :- ");
printf("\n");
int n;
scanf("%d",&n);
int arr[n];
printf("Enter %d element in array:- ",n);
printf("\n");
int temp;
/*gujju computervalo*/
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
heapSort(arr, n);
printf("Sorted array in acsending order : \n");
for(int i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
}
Comments
Post a Comment