/** * * Utility function to print the array after * sorting. * * @param arr array to be printed * @param sz size of array * */ template <typename T> voidprintArray(T *arr, int sz){ for (int i = 0; i < sz; i++) std::cout << arr[i] << " "; std::cout << "\n"; }
/** * * \addtogroup sorting Sorting Algorithm * @{ * * 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 to be sorted * @param n size of array * @param i node position in Binary Tress or element position in * Array to be compared with it's childern * */ template <typename T> voidheapify(T *arr, int n, int i){ int largest = i; int l = 2 * i + 1; int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest]) largest = l;
if (r < n && arr[r] > arr[largest]) largest = r;
if (largest != i) { std::swap(arr[i], arr[largest]); heapify(arr, n, largest); } }
/** * Utilizes heapify procedure to sort * the array * * @param arr array to be sorted * @param n size of array * */ template <typename T> voidheapSort(T *arr, int n){ for (int i = n - 1; i >= 0; i--) heapify(arr, n, i);
for (int i = n - 1; i >= 0; i--) { std::swap(arr[0], arr[i]); heapify(arr, i, 0); } }
/** * * @} * Test cases to test the program * */ voidtest(){ std::cout << "Test 1\n"; 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 printArray(arr, sz); // display array after sorting assert(std::is_sorted(arr, arr + sz)); std::cout << "Test 1 Passed\n========================\n";