Skip to content

Commit 26df57b

Browse files
committed
fixed heap sort
1 parent 2400e61 commit 26df57b

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

core/include/sorts/HeapSort.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
namespace HeapSort {
1010

11-
void GetHeapifyCount(Rectangle* array, int size, int index, int stepCount);
11+
void GetHeapifyCount(Rectangle* array, int size, int index, int* stepCount);
1212

1313
int GetStepCount(Rectangle* items);
1414

15-
void Heapify(Rectangle* array, int size, int index, int currentStep, SortSequence sort);
15+
void Heapify(Rectangle* array, int size, int index, int currentStep, SortSequence* sort);
1616

1717
SortSequence GetSequence(Rectangle* items);
1818

core/src/sorts/HeapSort.cpp

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace HeapSort {
1111

12-
void GetHeapifyCount(Rectangle* array, int size, int index, int stepCount) {
12+
void GetHeapifyCount(Rectangle* array, int size, int index, int* stepCount) {
1313
int largest = index;
1414

1515
// IF LEFT CHILD IS LARGER, SET AS LARGEST
@@ -29,7 +29,7 @@ namespace HeapSort {
2929
Swap(array, index, largest);
3030

3131
// INCREMENT STEP COUNTER
32-
stepCount++;
32+
(*stepCount)++;
3333

3434
// RECURSIVELY HEAPIFY THE AFFECTED SUB-TREE
3535
GetHeapifyCount(array, size, largest, stepCount);
@@ -39,7 +39,7 @@ namespace HeapSort {
3939
int GetStepCount(Rectangle* items) {
4040
Rectangle* array = CopyArray(items);
4141

42-
int stepCount = 0;
42+
int* stepCount = (int*) calloc(1, sizeof(int));
4343

4444
// BUILD INITIAL HEAP
4545
for (int i = LIST_SIZE / 2 - 1; i >= 0; i--) {
@@ -52,20 +52,21 @@ namespace HeapSort {
5252
Swap(array, 0, i);
5353

5454
// INCREMENT THE STEP COUNTER
55-
stepCount++;
55+
(*stepCount)++;
5656

5757
// CALL HEAPIFY ON THE REDUCED SIZE HEAP
5858
GetHeapifyCount(array, i, 0, stepCount);
5959
}
6060

6161
// INCREMENT FOR FINAL STEP
62-
stepCount++;
62+
(*stepCount)++;
6363

6464
free(array);
65-
return stepCount;
65+
int test = stepCount[0];
66+
return stepCount[0];
6667
}
6768

68-
void Heapify(Rectangle* array, int size, int index, int currentStep, SortSequence sort) {
69+
void Heapify(Rectangle* array, int size, int index, int* currentStep, SortSequence sort) {
6970
int largest = index;
7071

7172
// IF LEFT CHILD IS LARGER, SET AS LARGEST
@@ -87,15 +88,14 @@ namespace HeapSort {
8788
// RECORD THE SORTING STEP (SNAPSHOT OF THE ARRAY)
8889
int offset;
8990
for (int i = 0; i < LIST_SIZE; i++) {
90-
offset = (currentStep * LIST_SIZE) + i;
91+
offset = ((*currentStep) * LIST_SIZE) + i;
9192
sort.steps[offset].value = array[i].value;
92-
sort.steps[offset].rect_color = rect_base_color;
93-
94-
// TODO: ADD COLOR FOR HEAP SORT
95-
// bool isOrdered = i >= (LIST_SIZE - index);
96-
// sort.steps[offset].rect_color = GetRectangleColor(isOrdered, false, false);
93+
94+
// SET RECTANGLE COLOR VALUE
95+
bool isOrdered = i >= size;
96+
sort.steps[offset].rect_color = GetRectangleColor(isOrdered, false, false);
9797
}
98-
currentStep++;
98+
(*currentStep)++;
9999

100100
// RECURSIVELY HEAPIFY THE AFFECTED SUB-TREE
101101
Heapify(array, size, largest, currentStep, sort);
@@ -112,7 +112,7 @@ namespace HeapSort {
112112
sort.steps = (SortStep*) malloc(LIST_SIZE * stepCount * sizeof(SortStep));
113113

114114
int offset;
115-
int currentStep = 0;
115+
int* currentStep = (int*) calloc(1, sizeof(int));
116116

117117
// BUILD INITIAL HEAP
118118
for (int index = LIST_SIZE / 2 - 1; index >= 0; index--) {
@@ -126,28 +126,24 @@ namespace HeapSort {
126126

127127
// RECORD THE SORTING STEP (SNAPSHOT OF THE ARRAY)
128128
for (int i = 0; i < LIST_SIZE; i++) {
129-
offset = (currentStep * LIST_SIZE) + i;
129+
offset = (currentStep[0] * LIST_SIZE) + i;
130130
sort.steps[offset].value = array[i].value;
131-
sort.steps[offset].rect_color = rect_base_color;
132131

133-
// TODO: ADD COLOR FOR HEAP SORT
134-
// bool isOrdered = i >= (LIST_SIZE - index);
135-
// sort.steps[offset].rect_color = GetRectangleColor(isOrdered, false, false);
132+
// SET RECTANGLE COLOR VALUE
133+
bool isOrdered = i >= index;
134+
sort.steps[offset].rect_color = GetRectangleColor(isOrdered, false, false);
136135
}
137-
currentStep++;
136+
(*currentStep)++;
138137

139138
// CALL HEAPIFY ON THE REDUCED SIZE HEAP
140139
Heapify(array, index, 0, currentStep, sort);
141140
}
142141

143142
// RECORD FINAL STEP (ORDERED LIST)
144143
for (int i = 0; i < LIST_SIZE; i++) {
145-
offset = (currentStep * LIST_SIZE) + i;
144+
offset = (currentStep[0] * LIST_SIZE) + i;
146145
sort.steps[offset].value = array[i].value;
147-
sort.steps[offset].rect_color = rect_base_color;
148-
149-
// TODO: ADD COLOR FOR HEAP SORT
150-
// sort.steps[offset].rect_color = rect_green_color;
146+
sort.steps[offset].rect_color = rect_green_color;
151147
}
152148

153149
free(array);

src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
5858
SDL_SetWindowResizable(appContext->window, true);
5959
SDL_SetWindowMinimumSize(appContext->window, MINIMUM_WINDOW_WIDTH, MINIMUM_WINDOW_HEIGHT);
6060

61-
// CREATE LIST AND CALCULATE INITIAL WIDTH/HEIGHT OF RectangleS
61+
// CREATE LIST AND CALCULATE INITIAL WIDTH/HEIGHT OF RECTANGLES
6262
CreateList(appContext->renderer, appContext->items, appContext->width, appContext->height);
6363

6464
return SDL_APP_CONTINUE;
@@ -72,7 +72,7 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event* event) {
7272
case SDL_EVENT_QUIT:
7373
return SDL_APP_SUCCESS;
7474
case SDL_EVENT_WINDOW_RESIZED:
75-
// RESIZE WIDTH/HEIGHT OF RectangleS
75+
// RESIZE WIDTH/HEIGHT OF RECTANGLES
7676
SDL_GetWindowSize(appContext->window, &appContext->width, &appContext->height);
7777
ResizeList(appContext->renderer, appContext->items, appContext->width, appContext->height);
7878
}

0 commit comments

Comments
 (0)