Skip to content

Commit 0d2c6bc

Browse files
committed
Day-52 Heap Sort 2
1 parent 05e2213 commit 0d2c6bc

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 71 |
8-
| Current Streak | 51 |
9-
| Longest Streak | 51 ( August 17, 2015 - October 6, 2015 ) |
7+
| Total Problems | 72 |
8+
| Current Streak | 52 |
9+
| Longest Streak | 52 ( August 17, 2015 - October 7, 2015 ) |
1010

1111
</center>
1212

@@ -45,6 +45,7 @@ Include contains single header implementation of data structures and some algori
4545
| Bubble Sort Implementation | [bubbleSort.h] (include/bubbleSort.h) |
4646
| Linux Kernel Double LinkedList Implementation | [double_linked_list.h](include/double_linked_list.h) |
4747
| Generic Graph Implementation (Adjacency List) | [graph.h](include/graph.h) |
48+
| Heap Sort Implementation | [heap_sort.h](include/heap_sort.h)|
4849

4950
###Bit Manipulation Problems
5051
| Problem | Solution |

include/heap_sort.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifndef HEAP_SORT
2+
#define HEAP_SORT
3+
4+
#include <generic.h>
5+
#include <cassert>
6+
7+
namespace algo {
8+
9+
template <typename T>
10+
static void __sift_down(T list[], int low, int high) {
11+
int root = low;
12+
while( (root * 2 + 1) <= high ) {
13+
int left_child = root * 2 + 1;
14+
int right_child = left_child + 1;
15+
int swpIdx = root;
16+
//if root is less than left child.
17+
if ( list[root] < list[left_child] ) {
18+
swpIdx = left_child;
19+
}
20+
// if right child exists and bigger
21+
if ( (right_child <= high) && list[swpIdx] < list[right_child] ) {
22+
swpIdx = right_child;
23+
}
24+
25+
if ( swpIdx != root ) {
26+
algo::swap( list[swpIdx], list[root] );
27+
} else {
28+
break;
29+
}
30+
//keep going down
31+
swpIdx = root;
32+
}
33+
}
34+
35+
template <typename T>
36+
static void __heapify(T list[], int low, int high) {
37+
int mid = (high - low - 1)/2;
38+
while( mid >=0 ) {
39+
__sift_down(list, mid, high);
40+
--mid;
41+
}
42+
}
43+
44+
template <typename T>
45+
static void heap_sort(T list[], int size) {
46+
assert(list);
47+
assert(size > 0);
48+
49+
//heapify the list
50+
__heapify(list, 0, size-1);
51+
52+
int high = size - 1;
53+
while ( high > 0 ) {
54+
algo::swap(list[high], list[0]);
55+
--high;
56+
__heapify(list, 0, high);
57+
}
58+
}
59+
}
60+
#endif

sort_search_problems/heapSortDemo.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <generic.h>
2+
#include <heap_sort.h>
3+
4+
int main()
5+
{
6+
const int MAX_ELEMENTS = 10;
7+
int arr[MAX_ELEMENTS];
8+
double arrD[MAX_ELEMENTS];
9+
10+
//Filling up the array with random numbers;
11+
for (int i = 0; i < MAX_ELEMENTS; ++i)
12+
{
13+
arr[i] = algo::random_range(1, 100);
14+
arrD[i] = algo::random_range(1.0, 99.99);
15+
}
16+
17+
std::cout << "Before Sorting:\n";
18+
algo::printList(arr, MAX_ELEMENTS);
19+
algo::heap_sort(arr, MAX_ELEMENTS);
20+
std::cout << "After Sorting:\n";
21+
algo::printList(arr, MAX_ELEMENTS);
22+
23+
24+
std::cout << "\n\nBefore Sorting:\n";
25+
algo::printList(arrD, MAX_ELEMENTS);
26+
algo::heap_sort(arrD, MAX_ELEMENTS);
27+
std::cout << "After Sorting:\n";
28+
algo::printList(arrD, MAX_ELEMENTS);
29+
return 0;
30+
}

0 commit comments

Comments
 (0)