Browse Source

working on heapsort with positive results for the first sample array and fairly bad results on the next one. Issues include disappearances of numbers and some numbers coming up twice.

Natalie Pueyo 7 years ago
parent
commit
71be100c9a
4 changed files with 52 additions and 24 deletions
  1. 31 1
      program2/heapsort.cxx
  2. 16 23
      program2/priorityqueue.cpp
  3. 4 0
      program2/priorityqueue.h
  4. 1 0
      program2/testCode.cxx

+ 31 - 1
program2/heapsort.cxx

@@ -19,19 +19,49 @@ int main(int argc, char** argv) {
 
   std::string sampleName;     // Sample key name to iterate
   nlohmann::json outputJSON;  // output JSON file
-  //int currValue = 0;          // current key value
+  int currValue = 0;          // current key value
 
   int n = jsonObject["metadata"]["arraySize"];
 
+  std::vector<int> A(n, 0);
+
   class PriorityQueue sampleHeap;
   sampleHeap.initiateHeap(n);
 
   for (auto itr = jsonObject.begin(); itr != jsonObject.end(); ++itr) {
+    sampleName = itr.key();
+
     if (sampleName != "metadata") {
+      nlohmann::json topOfHeap;
       for (auto arrayItr = jsonObject[sampleName].begin(); arrayItr != jsonObject[sampleName].end(); ++arrayItr) {
+        // insert each array element into heap
+        currValue = *arrayItr;
+        sampleHeap.insert(currValue);
+      }
+      std::cout << "before sorting" << std::endl;
+      sampleHeap.printArray();
+      for (int i = n - 1; i >= 0; i--) {
+        // insert heap.top into array A backwards to sort the array from min to max
+        A[i] = sampleHeap.returnMax();
+        // remove the max value from the heap
+        sampleHeap.removeMax();
+        std::cout << "after sorting" << std::endl;
+        sampleHeap.printArray();
       }
+      for (int j = 0; j <= n - 1; j++) {
+        // adding values directly to JSON object
+        topOfHeap.push_back(A[j]);
+        std::cout << A[j] << ", ";
+      }
+      std::cout << std::endl;
+      outputJSON[sampleName] = topOfHeap;
     }
   }
+
   std::cout << outputJSON << std::endl;
+
+  // clean up memory
   file.close();
+  sampleHeap.deleteHeap();
+  return 0;
 }

+ 16 - 23
program2/priorityqueue.cpp

@@ -6,31 +6,10 @@
 #include "json.hpp"
 
 // priority queue class using binary heap
-/*
-class PriorityQueue{
-  int *heapArray;   // pointer to heap array
-  int max_size;     // max size of heap array
-  int heap_size;    // elements in heap
-public:
-  // required functions
-  void insert(int);
-  void removeMax();
-  void removeKey(int);
-  void change(int*, int*);
-  // helpful functions
-  void heapifyUp(int, int);
-  void heapifyDown(int);
-  //nlohmann::json JSON();
-  // other required functions (for now)
-  void initiateHeap(int);
-};
-*/
-
 void PriorityQueue::initiateHeap(int capacity){
     heap_size = 0;
     max_size = capacity;
     heapArray = new int[max_size];
-    //std::vector<int>* insArray = new std::vector<int>();
 }
 
 void PriorityQueue::insert(int key){
@@ -46,14 +25,23 @@ void PriorityQueue::insert(int key){
   heapifyUp(key, keyIndex);
 }
 
+void PriorityQueue::deleteHeap(){
+  // clean up memory so deallocate array
+  delete[] heapArray;
+}
+
 void PriorityQueue::removeMax(){
   // check if heap is empty
-  if (heap_size <= 1) {
+  if (heap_size <= 0) {
     std::cout << "PriorityQueue::removeMax called on an empty priority queue" << std::endl;
     return;
   }
 
-  if (heap_size == 1) { // need to check if at root to avoid error
+  if (heap_size == 1){
+    heapArray[0] == 0;
+    heap_size--;
+  } else if (heap_size == 2) { // need to check if at root to avoid error
+    heapArray[1] = heapArray[2];
     heap_size--;
   } else {
     // remove the max value (at root)
@@ -62,6 +50,11 @@ void PriorityQueue::removeMax(){
   }
 }
 
+int PriorityQueue::returnMax(){
+  int topHeapKey = heapArray[1];
+  return topHeapKey;
+}
+
 void PriorityQueue::removeKey(int key){
   for (int i = 1; i <= heap_size; i++) {
     if (heapArray[i] == key) {

+ 4 - 0
program2/priorityqueue.h

@@ -21,6 +21,8 @@ public:
   // other required functions (for now)
   void initiateHeap(int);
   void printArray();
+  void deleteHeap();
+  int returnMax();
 };
 
 
@@ -35,6 +37,8 @@ void heapifyDown(int index);
 // other required functions (for now)
 void initiateHeap(int capacity);
 void printArray();
+int returnMax();
+void deleteHeap();
 
 
 #endif

+ 1 - 0
program2/testCode.cxx

@@ -19,5 +19,6 @@ int main(){
   h.removeKey(15);
   h.removeMax();
   h.printArray();
+  h.deleteHeap();
   return 0;
 }