-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#ifndef HEAP_SORT | ||
#define HEAP_SORT | ||
|
||
#include <generic.h> | ||
#include <cassert> | ||
|
||
namespace algo { | ||
|
||
template <typename T> | ||
static void __sift_down(T list[], int low, int high) { | ||
int root = low; | ||
while( (root * 2 + 1) <= high ) { | ||
int left_child = root * 2 + 1; | ||
int right_child = left_child + 1; | ||
int swpIdx = root; | ||
//if root is less than left child. | ||
if ( list[root] < list[left_child] ) { | ||
swpIdx = left_child; | ||
} | ||
// if right child exists and bigger | ||
if ( (right_child <= high) && list[swpIdx] < list[right_child] ) { | ||
swpIdx = right_child; | ||
} | ||
|
||
if ( swpIdx != root ) { | ||
algo::swap( list[swpIdx], list[root] ); | ||
} else { | ||
break; | ||
} | ||
//keep going down | ||
swpIdx = root; | ||
} | ||
} | ||
|
||
template <typename T> | ||
static void __heapify(T list[], int low, int high) { | ||
int mid = (high - low - 1)/2; | ||
while( mid >=0 ) { | ||
__sift_down(list, mid, high); | ||
--mid; | ||
} | ||
} | ||
|
||
template <typename T> | ||
static void heap_sort(T list[], int size) { | ||
assert(list); | ||
assert(size > 0); | ||
|
||
//heapify the list | ||
__heapify(list, 0, size-1); | ||
|
||
int high = size - 1; | ||
while ( high > 0 ) { | ||
algo::swap(list[high], list[0]); | ||
--high; | ||
__heapify(list, 0, high); | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <generic.h> | ||
#include <heap_sort.h> | ||
|
||
int main() | ||
{ | ||
const int MAX_ELEMENTS = 10; | ||
int arr[MAX_ELEMENTS]; | ||
double arrD[MAX_ELEMENTS]; | ||
|
||
//Filling up the array with random numbers; | ||
for (int i = 0; i < MAX_ELEMENTS; ++i) | ||
{ | ||
arr[i] = algo::random_range(1, 100); | ||
arrD[i] = algo::random_range(1.0, 99.99); | ||
} | ||
|
||
std::cout << "Before Sorting:\n"; | ||
algo::printList(arr, MAX_ELEMENTS); | ||
algo::heap_sort(arr, MAX_ELEMENTS); | ||
std::cout << "After Sorting:\n"; | ||
algo::printList(arr, MAX_ELEMENTS); | ||
|
||
|
||
std::cout << "\n\nBefore Sorting:\n"; | ||
algo::printList(arrD, MAX_ELEMENTS); | ||
algo::heap_sort(arrD, MAX_ELEMENTS); | ||
std::cout << "After Sorting:\n"; | ||
algo::printList(arrD, MAX_ELEMENTS); | ||
return 0; | ||
} |