Skip to content

Commit

Permalink
Day-52 Heap Sort 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Oct 7, 2015
1 parent 05e2213 commit 0d2c6bc
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

| Current Status| Stats |
| :------------: | :----------: |
| Total Problems | 71 |
| Current Streak | 51 |
| Longest Streak | 51 ( August 17, 2015 - October 6, 2015 ) |
| Total Problems | 72 |
| Current Streak | 52 |
| Longest Streak | 52 ( August 17, 2015 - October 7, 2015 ) |

</center>

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

###Bit Manipulation Problems
| Problem | Solution |
Expand Down
60 changes: 60 additions & 0 deletions include/heap_sort.h
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
30 changes: 30 additions & 0 deletions sort_search_problems/heapSortDemo.cpp
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;
}

0 comments on commit 0d2c6bc

Please sign in to comment.