From 2c850318090473fdbf1a647e74be11dc00550393 Mon Sep 17 00:00:00 2001 From: SantoshAnand271 Date: Sun, 16 Oct 2022 22:54:17 +0530 Subject: [PATCH] HeapSort Added heap sort in cpp --- heapSort.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 heapSort.cpp diff --git a/heapSort.cpp b/heapSort.cpp new file mode 100644 index 0000000..dbb79ad --- /dev/null +++ b/heapSort.cpp @@ -0,0 +1,115 @@ +#include +#include + +using namespace std; + +int find_min_element_index(int a[] , int element , int left_child , int right_child){ + if(a[element] <= a[left_child] && a[element] <= a[right_child]) + return element; + + if(a[left_child] <= a[element] && a[left_child] <= a[right_child]) + return left_child; + + if(a[right_child] <= a[element] && a[right_child] <= a[left_child]) + return right_child; +} + +int check_heap(int a[] , int n){ + + int second_last_level = (int)log2(n); + + int check_elements_upto_index = pow( 2 , second_last_level+1)-2; + for(int i=0; i<=check_elements_upto_index; i++){ + + if(2*i+1 <= n-1 || 2*i+2 <= n-1){ + if(2*i+1 <= n-1 && 2*i+2 <= n-1){ + if(a[i] <= a[2*i+1] && a[i] <= a[2*i+2]) + continue; + else + return 0; + } + else if(2*i+1 <= n-1){ + if(a[i] <= a[2*i+1]) + continue; + else + return 0; + } + else{ + if(a[i] <= a[2*i+2]) + continue; + else + return 0; + } + } + else{ + break; + } + } + return 1; +} + +void heapify(int a[] , int n){ + int heap_flag = 0; + while(heap_flag == 0){ + int level = (int)log2(n); + int ending_index_at_corresponding_level = pow(2 ,level+1)-2; + for(int i = ending_index_at_corresponding_level;i >= 0 ;i--){ + if(2*i+1 <= n-1 || 2*i+2 <= n-1){ + if(2*i+1 <= n-1 && 2*i+2 <= n-1){ + int min_index = find_min_element_index(a , i , 2*i+1 , 2*i+2); + if(min_index == i){ + continue; + } + else{ + int temp = a[min_index]; + a[min_index] = a[i]; + a[i] = temp; + } + } + else if( 2*i+1 <= n-1){ + if(a[i] <= a[2*i+1]) + continue; + else{ + int temp = a[i]; + a[i] = a[2*i+1]; + a[2*i+1] = temp; + } + } + else{ + if(a[i] <= a[2*i+2]) + continue; + else{ + int temp = a[i]; + a[i] = a[2*i+2]; + a[2*i+2] = temp; + } + } + } + } + + heap_flag = check_heap(a , n); + if(heap_flag == 1){ + cout << a[0] <<", "; + int temp = a[0]; + a[0] = a[n-1]; + a[n-1] = temp; + n = n-1; + } + if(n==1){ + cout <