@@ -31,7 +31,11 @@ namespace BubbleSort {
3131 stepCount++;
3232 }
3333 }
34- if (!swapped) break ;
34+ if (!swapped) {
35+ // INCREMENT FOR FINAL STEP
36+ stepCount++;
37+ break ;
38+ }
3539 }
3640
3741 free (array);
@@ -43,7 +47,7 @@ namespace BubbleSort {
4347 int stepCount = GetStepCount_BubbleSort (items);
4448
4549 SortSequence sort = create_sort_sequence (stepCount);
46- sort.steps = (int *) malloc (LIST_SIZE * stepCount * sizeof (int ));
50+ sort.steps = (SortStep *) malloc (LIST_SIZE * stepCount * sizeof (SortStep ));
4751
4852 Rectangle* array = (Rectangle*) malloc (LIST_SIZE * sizeof (Rectangle));
4953 memcpy (array, items, LIST_SIZE * sizeof (Rectangle));
@@ -63,15 +67,33 @@ namespace BubbleSort {
6367
6468 // RECORD THE SORTING STEP (SNAPSHOT OF THE ARRAY)
6569 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+
6674 offset = (currentStep * LIST_SIZE) + i;
67- sort.steps [offset] = array[i].value ;
75+ sort.steps [offset].value = array[i].value ;
76+
77+ // 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;
6882 }
6983 currentStep++;
7084 }
7185 }
7286
7387 // BREAK WHEN NO ELEMENTS SWAPPED (OPTIMIZED)
74- if (!swapped) break ;
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+ }
7597 }
7698
7799 free (array);
@@ -86,7 +108,8 @@ namespace BubbleSort {
86108 int offset;
87109 for (int i = 0 ; i < LIST_SIZE; i++) {
88110 offset = (index * LIST_SIZE) + i;
89- items[i].value = sequence.steps [offset];
111+ items[i].value = sequence.steps [offset].value ;
112+ items[i].rect_color = sequence.steps [offset].rect_color ;
90113 }
91114 return false ;
92115 }
0 commit comments