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+ }
0 commit comments