Skip to content

Commit dcf8016

Browse files
committed
added cocktail sort
1 parent f674095 commit dcf8016

File tree

9 files changed

+247
-82
lines changed

9 files changed

+247
-82
lines changed

core/include/sorts/BubbleSort.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ namespace BubbleSort {
1212

1313
SortSequence GetSequence_BubbleSort(Rectangle* items);
1414

15-
bool IncrementStep_BubbleSort(int stepNum, SortSequence sequence, Rectangle* items);
16-
1715
}
1816

1917
#endif
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef COCKTAIL_SORT_H
2+
#define COCKTAIL_SORT_H
3+
4+
#include <SDL3/SDL.h>
5+
6+
#include <Rect.hpp>
7+
#include <Sort.hpp>
8+
9+
namespace CocktailSort {
10+
11+
int GetStepCount_CocktailSort(Rectangle* items);
12+
13+
SortSequence GetSequence_CocktailSort(Rectangle* items);
14+
15+
}
16+
17+
#endif

core/include/utils/Sort.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ typedef struct {
2020

2121
SortSequence GetSortSequence(int sortId, Rectangle* items);
2222

23-
bool IncrementStep(int sortId, int stepIndex, SortSequence sequence, Rectangle* items);
23+
bool IncrementStep(int stepIndex, SortSequence sequence, Rectangle* items);
2424

2525
#endif

core/include/utils/Utils.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef UTILS_H
2+
#define UTILS_H
3+
4+
#include <SDL3/SDL.h>
5+
6+
#include <List.hpp>
7+
#include <Rect.hpp>
8+
9+
void Swap(Rectangle* items, int index_one, int index_two);
10+
11+
rgb_color GetRectangleColor(bool isOrdered, bool isCurrent, bool isChecking);
12+
13+
#endif

core/src/sorts/BubbleSort.cpp

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#include <stdlib.h>
2-
#include <algorithm>
3-
42
#include <SDL3/SDL.h>
53

64
#include <BubbleSort.hpp>
75
#include <List.hpp>
86
#include <Rect.hpp>
97
#include <Sort.hpp>
8+
#include <Utils.hpp>
109

1110
namespace BubbleSort {
1211

@@ -22,22 +21,19 @@ namespace BubbleSort {
2221
swapped = false;
2322
for (int j = 0; j < LIST_SIZE - i - 1; j++) {
2423
if (array[j].value > array[j + 1].value) {
25-
temp = array[j].value;
26-
array[j].value = array[j + 1].value;
27-
array[j + 1].value = temp;
24+
Swap(array, j, (j + 1));
2825
swapped = true;
29-
26+
3027
// INCREMENT THE STEP COUNTER
3128
stepCount++;
3229
}
3330
}
34-
if (!swapped) {
35-
// INCREMENT FOR FINAL STEP
36-
stepCount++;
37-
break;
38-
}
31+
if (!swapped) break;
3932
}
4033

34+
// INCREMENT FOR FINAL STEP
35+
stepCount++;
36+
4137
free(array);
4238
return stepCount;
4339
}
@@ -59,59 +55,37 @@ namespace BubbleSort {
5955
swapped = false;
6056
for (int j = 0; j < LIST_SIZE - index - 1; j++) {
6157
if (array[j].value > array[j + 1].value) {
62-
// SWAP VALUES
63-
temp = array[j].value;
64-
array[j].value = array[j + 1].value;
65-
array[j + 1].value = temp;
58+
Swap(array, j, (j + 1));
6659
swapped = true;
6760

6861
// RECORD THE SORTING STEP (SNAPSHOT OF THE ARRAY)
6962
for (int i = 0; i < LIST_SIZE; i++) {
70-
bool isOrdered = i >= (LIST_SIZE - index);
71-
bool isCurrent = i == j;
72-
bool isChecking = i == (j + 1);
73-
7463
offset = (currentStep * LIST_SIZE) + i;
7564
sort.steps[offset].value = array[i].value;
7665

7766
// SET RECTANGLE COLOR
78-
sort.steps[offset].rect_color =
79-
isOrdered ? rect_green_color :
80-
isCurrent ? rect_blue_color :
81-
isChecking ? rect_orange_color : rect_base_color;
67+
bool isOrdered = i >= (LIST_SIZE - index);
68+
bool isCurrent = i == j;
69+
bool isChecking = i == (j + 1);
70+
sort.steps[offset].rect_color = GetRectangleColor(isOrdered, isCurrent, isChecking);
8271
}
8372
currentStep++;
8473
}
8574
}
8675

8776
// BREAK WHEN NO ELEMENTS SWAPPED (OPTIMIZED)
88-
if (!swapped) {
89-
// RECORD FINAL STEP
90-
for (int i = 0; i < LIST_SIZE; i++) {
91-
offset = (currentStep * LIST_SIZE) + i;
92-
sort.steps[offset].value = array[i].value;
93-
sort.steps[offset].rect_color = rect_green_color;
94-
}
95-
break;
96-
}
77+
if (!swapped) break;
9778
}
9879

99-
free(array);
100-
return sort;
101-
}
102-
103-
bool IncrementStep_BubbleSort(int index, SortSequence sequence, Rectangle* items) {
104-
// RETURN TRUE WHEN SORT IS COMPLETE
105-
if(index >= sequence.stepCount) return true;
106-
107-
// SWAP THE VALUES TO THAT OF THE CURRENT STEP SNAPSHOT
108-
int offset;
80+
// RECORD FINAL STEP (ORDERED LIST)
10981
for (int i = 0; i < LIST_SIZE; i++) {
110-
offset = (index * LIST_SIZE) + i;
111-
items[i].value = sequence.steps[offset].value;
112-
items[i].rect_color = sequence.steps[offset].rect_color;
82+
offset = (currentStep * LIST_SIZE) + i;
83+
sort.steps[offset].value = array[i].value;
84+
sort.steps[offset].rect_color = rect_green_color;
11385
}
114-
return false;
86+
87+
free(array);
88+
return sort;
11589
}
11690

11791
}

core/src/sorts/CocktailSort.cpp

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#include <stdlib.h>
2+
3+
#include <SDL3/SDL.h>
4+
5+
#include <CocktailSort.hpp>
6+
#include <List.hpp>
7+
#include <Rect.hpp>
8+
#include <Sort.hpp>
9+
#include <Utils.hpp>
10+
11+
namespace CocktailSort {
12+
13+
int GetStepCount_CocktailSort(Rectangle* items) {
14+
Rectangle* array = (Rectangle*) malloc(LIST_SIZE * sizeof(Rectangle));
15+
memcpy(array, items, LIST_SIZE * sizeof(Rectangle));
16+
17+
int temp;
18+
int stepCount = 0;
19+
20+
bool swapped = true;
21+
int start = 0;
22+
int end = LIST_SIZE - 1;
23+
24+
while (swapped) {
25+
// RESET SWAPPED FLAG
26+
swapped = false;
27+
28+
// FROM LEFT TO RIGHT, BUBBLE THE HIGHEST VALUE TO THE END
29+
for (int index = start; index < end; ++index) {
30+
if (array[index].value > array[index + 1].value) {
31+
Swap(array, index, (index + 1));
32+
swapped = true;
33+
34+
// INCREMENT THE STEP COUNTER
35+
stepCount++;
36+
}
37+
}
38+
39+
// BREAK WHEN NO ELEMENTS SWAPPED (OPTIMIZED)
40+
if (!swapped) break;
41+
42+
// RESET SWAPPED FLAG
43+
swapped = false;
44+
45+
// MOVE END POINT BACK ONCE,
46+
// BECAUSE THAT INDEX IS SORTED
47+
--end;
48+
49+
// FROM RIGHT TO LEFT, BUBBLE THE LOWEST VALUE TO THE START
50+
for (int index = end - 1; index >= start; --index) {
51+
if (array[index].value > array[index + 1].value) {
52+
Swap(array, index, (index + 1));
53+
swapped = true;
54+
55+
// INCREMENT THE STEP COUNTER
56+
stepCount++;
57+
}
58+
}
59+
60+
// MOVE START POINT FORWARD ONCE,
61+
// BECAUSE THAT INDEX IS NOW SORTED
62+
++start;
63+
}
64+
65+
// INCREMENT FOR FINAL STEP
66+
stepCount++;
67+
68+
free(array);
69+
return stepCount;
70+
}
71+
72+
SortSequence GetSequence_CocktailSort(Rectangle* items) {
73+
// INITIALLY RUN THE COCKTAIL SORT CALCULATING THE TOTAL NUMBER OF STEPS
74+
int stepCount = GetStepCount_CocktailSort(items);
75+
76+
SortSequence sort = create_sort_sequence(stepCount);
77+
sort.steps = (SortStep*) malloc(LIST_SIZE * stepCount * sizeof(SortStep));
78+
79+
Rectangle* array = (Rectangle*) malloc(LIST_SIZE * sizeof(Rectangle));
80+
memcpy(array, items, LIST_SIZE * sizeof(Rectangle));
81+
82+
int currentStep = 0;
83+
int temp, offset;
84+
85+
bool swapped = true;
86+
int start = 0;
87+
int end = LIST_SIZE - 1;
88+
89+
while (swapped) {
90+
// RESET SWAPPED FLAG
91+
swapped = false;
92+
93+
// FROM LEFT TO RIGHT, BUBBLE THE HIGHEST VALUE TO THE END
94+
for (int index = start; index < end; ++index) {
95+
if (array[index].value > array[index + 1].value) {
96+
Swap(array, index, (index + 1));
97+
swapped = true;
98+
99+
// RECORD THE SORTING STEP (SNAPSHOT OF THE ARRAY)
100+
for (int i = 0; i < LIST_SIZE; i++) {
101+
offset = (currentStep * LIST_SIZE) + i;
102+
sort.steps[offset].value = array[i].value;
103+
104+
// SET RECTANGLE COLOR
105+
bool isOrdered = i > end || i < start;
106+
bool isCurrent = i == index;
107+
bool isChecking = i == (index + 1);
108+
sort.steps[offset].rect_color = GetRectangleColor(isOrdered, isCurrent, isChecking);
109+
}
110+
currentStep++;
111+
}
112+
}
113+
114+
// BREAK WHEN NO ELEMENTS SWAPPED (OPTIMIZED)
115+
if (!swapped) break;
116+
117+
// RESET SWAPPED FLAG
118+
swapped = false;
119+
120+
// MOVE END POINT BACK ONCE,
121+
// BECAUSE THAT INDEX IS SORTED
122+
--end;
123+
124+
// FROM RIGHT TO LEFT, BUBBLE THE LOWEST VALUE TO THE START
125+
for (int index = end - 1; index >= start; --index) {
126+
if (array[index].value > array[index + 1].value) {
127+
Swap(array, index, (index + 1));
128+
swapped = true;
129+
130+
// RECORD THE SORTING STEP (SNAPSHOT OF THE ARRAY)
131+
for (int i = 0; i < LIST_SIZE; i++) {
132+
offset = (currentStep * LIST_SIZE) + i;
133+
sort.steps[offset].value = array[i].value;
134+
135+
// SET RECTANGLE COLOR
136+
bool isOrdered = i > end || i < start;
137+
bool isCurrent = i == index;
138+
bool isChecking = i == (index + 1);
139+
sort.steps[offset].rect_color = GetRectangleColor(isOrdered, isCurrent, isChecking);
140+
}
141+
currentStep++;
142+
}
143+
}
144+
145+
// MOVE START POINT FORWARD ONCE,
146+
// BECAUSE THAT INDEX IS NOW SORTED
147+
++start;
148+
}
149+
150+
// RECORD FINAL STEP (ORDERED LIST)
151+
for (int i = 0; i < LIST_SIZE; i++) {
152+
offset = (currentStep * LIST_SIZE) + i;
153+
sort.steps[offset].value = array[i].value;
154+
sort.steps[offset].rect_color = rect_green_color;
155+
}
156+
157+
free(array);
158+
return sort;
159+
}
160+
161+
}

core/src/utils/Sort.cpp

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
#include <Sort.hpp>
55

66
#include <BubbleSort.hpp>
7+
#include <CocktailSort.hpp>
78

89
SortSequence GetSortSequence(int sortId, Rectangle* items) {
910
switch(sortId) {
1011
case 0:
1112
return BubbleSort::GetSequence_BubbleSort(items);
12-
break;
1313
case 1:
14-
SDL_Log("Start Cocktail Sort...");
15-
break;
14+
return CocktailSort::GetSequence_CocktailSort(items);
1615
case 2:
1716
SDL_Log("Start Heap Sort...");
1817
break;
@@ -35,31 +34,16 @@ SortSequence GetSortSequence(int sortId, Rectangle* items) {
3534
return { };
3635
}
3736

38-
bool IncrementStep(int sortId, int stepIndex, SortSequence sequence, Rectangle* items) {
39-
switch(sortId) {
40-
case 0:
41-
return BubbleSort::IncrementStep_BubbleSort(stepIndex, sequence, items);
42-
case 1:
43-
SDL_Log("Iterate Cocktail Sort...");
44-
break;
45-
case 2:
46-
SDL_Log("Iterate Heap Sort...");
47-
break;
48-
case 3:
49-
SDL_Log("Iterate Insertion Sort...");
50-
break;
51-
case 4:
52-
SDL_Log("Iterate Merge Sort...");
53-
break;
54-
case 5:
55-
SDL_Log("Iterate Quick Sort...");
56-
break;
57-
case 6:
58-
SDL_Log("Iterate Radix Sort...");
59-
break;
60-
case 7:
61-
SDL_Log("Iterate Selection Sort...");
62-
break;
37+
bool IncrementStep(int stepIndex, SortSequence sequence, Rectangle* items) {
38+
// RETURN TRUE WHEN SORT IS COMPLETE
39+
if(stepIndex >= sequence.stepCount) return true;
40+
41+
// SWAP THE VALUES TO THAT OF THE CURRENT STEP SNAPSHOT
42+
int offset;
43+
for (int i = 0; i < LIST_SIZE; i++) {
44+
offset = (stepIndex * LIST_SIZE) + i;
45+
items[i].value = sequence.steps[offset].value;
46+
items[i].rect_color = sequence.steps[offset].rect_color;
6347
}
64-
return true;
48+
return false;
6549
}

0 commit comments

Comments
 (0)