Skip to content

Commit

Permalink
+ comparison count for heapsort
Browse files Browse the repository at this point in the history
  • Loading branch information
PjrCodes committed Apr 2, 2024
1 parent 6acf5b6 commit 7cd312b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
16 changes: 12 additions & 4 deletions cmplx/heapsort/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <math.h>

namespace cmplx::heapsort {
int ComparisonCount;

template<class T>
class MaxHeap {
public:
Expand Down Expand Up @@ -38,12 +40,17 @@ class MaxHeap {
auto Right = this->right(Root);
int Largest;

if (Left <= this->HeapSize && this->HeapArr[Left] > this->HeapArr[Root])
if (Left <= this->HeapSize && this->HeapArr[Left] > this->HeapArr[Root]) {
ComparisonCount++;
Largest = Left;
else
} else {
ComparisonCount++;
Largest = Root;
if (Right <= this->HeapSize && this->HeapArr[Right] > this->HeapArr[Largest])
}
if (Right <= this->HeapSize && this->HeapArr[Right] > this->HeapArr[Largest]) {
ComparisonCount++;
Largest = Right;
}
if (Largest != Root) {
exchange(Root, Largest);
this->maxHeapify(Largest);
Expand All @@ -53,8 +60,9 @@ class MaxHeap {
void buildMaxHeap() {
// 1 less than length since first element is discarded
this->HeapSize = this->Length - 1;
for (int Idx = parent(this->Length - 1); Idx >= 0; Idx--)
for (int Idx = parent(this->Length - 1); Idx >= 0; Idx--) {
this->maxHeapify(Idx);
}
}

void heapSort() {
Expand Down
5 changes: 5 additions & 0 deletions cmplx/heapsort/heapsort.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
#include <iostream>

namespace cmplx::heapsort {

template<typename T>
int sort(T *Arr, int N) {
ComparisonCount = 0;

auto *Heap = new cmplx::heapsort::MaxHeap(Arr, N);
Heap->heapSort();
delete Heap;

return ComparisonCount;
}
}// namespace cmplx::heapsort
2 changes: 2 additions & 0 deletions cmplx/quicksort/quicksort.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ void qSort(T *Arr, int Left, int Right) {
template<typename T>
int sort(T *Arr, int Len) {
qSort(Arr, 0, Len - 1);

return 0;
}
}// namespace cmplx::quicksort
12 changes: 6 additions & 6 deletions executables/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main(int Argc, char *Argv[]) {
int SortFlag = -1;
bool ToPrintTime = false;
bool ToPrintArray = false;
bool ToPrintComparisionCount = true;
bool ToPrintComparisonCount = true;

std::string FileName;
for (int I = 1; I < Argc; I++) {
Expand Down Expand Up @@ -57,19 +57,19 @@ int main(int Argc, char *Argv[]) {

double_time Start;
double_time Stop;
int ComparisionCount;
int ComparisonCount;

for (int I = 0; I < ArrayCount; I++) {

cmplx::utils::ComplexNumber *Arr = ComplexNumberArrays[I];

if (SortFlag == 0) {
ToPrintTime ? Start = std::chrono::high_resolution_clock::now() : Start;
ComparisionCount = cmplx::heapsort::sort(Arr, N);
ComparisonCount = cmplx::heapsort::sort(Arr, N);
ToPrintTime ? Stop = std::chrono::high_resolution_clock::now() : Stop;
} else {
ToPrintTime ? Start = std::chrono::high_resolution_clock::now() : Start;
ComparisionCount = cmplx::quicksort::sort(Arr, N);
ComparisonCount = cmplx::quicksort::sort(Arr, N);
ToPrintTime ? Stop = std::chrono::high_resolution_clock::now() : Stop;
}

Expand All @@ -83,8 +83,8 @@ int main(int Argc, char *Argv[]) {
std::cout << "Time: " << Duration.count() << "ns" << std::endl;
}

if (ToPrintComparisionCount) {
std::cout << "Comparisions: " << ComparisionCount << std::endl;
if (ToPrintComparisonCount) {
std::cout << "Comparisions: " << ComparisonCount << std::endl;
}

delete Arr;
Expand Down
3 changes: 1 addition & 2 deletions testfiles/file.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
3 8
1 + 0i, 0 + 2i, 3 + 4i, 0 - 5i, 6 - 7i, 8 + 9i, 0 - 10i, -10 - 11i
2 8
1 + 0i, 0 + 2i, 3 + 4i, 0 - 5i, 6 - 7i, 8 + 9i, 0 - 10i, -10 - 11i
1 + 0i, 0 + 2i, 3 + 4i, 0 - 5i, 6 - 7i, 8 + 9i, 0 - 10i, -10 - 11i

0 comments on commit 7cd312b

Please sign in to comment.