File tree Expand file tree Collapse file tree 3 files changed +94
-3
lines changed Expand file tree Collapse file tree 3 files changed +94
-3
lines changed Original file line number Diff line number Diff line change 4
4
5
5
| Current Status| Stats |
6
6
| :------------: | :----------: |
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 ) |
10
10
11
11
</center >
12
12
@@ -45,6 +45,7 @@ Include contains single header implementation of data structures and some algori
45
45
| Bubble Sort Implementation | [ bubbleSort.h] (include/bubbleSort.h) |
46
46
| Linux Kernel Double LinkedList Implementation | [ double_linked_list.h] ( include/double_linked_list.h ) |
47
47
| Generic Graph Implementation (Adjacency List) | [ graph.h] ( include/graph.h ) |
48
+ | Heap Sort Implementation | [ heap_sort.h] ( include/heap_sort.h ) |
48
49
49
50
###Bit Manipulation Problems
50
51
| Problem | Solution |
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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\n Before 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
+ }
You can’t perform that action at this time.
0 commit comments