Skip to content

Commit 2c512da

Browse files
committed
added insertion sort
1 parent 5a38a19 commit 2c512da

6 files changed

Lines changed: 119 additions & 7 deletions

File tree

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

core/src/sorts/BubbleSort.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace BubbleSort {
1414

1515
int stepCount = 1;
1616

17-
int temp;
1817
bool swapped;
1918
for (int i = 0; i < LIST_SIZE - 1; i++) {
2019
swapped = false;
@@ -44,7 +43,7 @@ namespace BubbleSort {
4443
sort.steps = (SortStep*) malloc(LIST_SIZE * stepCount * sizeof(SortStep));
4544

4645
int currentStep = 0;
47-
int temp, offset;
46+
int offset;
4847
bool swapped;
4948
for (int index = 0; index < LIST_SIZE - 1; index++) {
5049
swapped = false;

core/src/sorts/CocktailSort.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace CocktailSort {
1212
int GetStepCount(Rectangle* items) {
1313
Rectangle* array = CopyArray(items);
1414

15-
int temp;
1615
int stepCount = 0;
1716

1817
bool swapped = true;
@@ -76,8 +75,8 @@ namespace CocktailSort {
7675
SortSequence sort = create_sort_sequence(stepCount);
7776
sort.steps = (SortStep*) malloc(LIST_SIZE * stepCount * sizeof(SortStep));
7877

78+
int offset;
7979
int currentStep = 0;
80-
int temp, offset;
8180

8281
bool swapped = true;
8382
int start = 0;

core/src/sorts/HeapSort.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ namespace HeapSort {
6262
(*stepCount)++;
6363

6464
free(array);
65-
int test = stepCount[0];
6665
return stepCount[0];
6766
}
6867

core/src/sorts/InsertionSort.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <stdlib.h>
2+
#include <SDL3/SDL.h>
3+
4+
#include <InsertionSort.hpp>
5+
#include <List.hpp>
6+
#include <Rect.hpp>
7+
#include <Sort.hpp>
8+
#include <Utils.hpp>
9+
10+
namespace InsertionSort {
11+
12+
int GetStepCount(Rectangle* items) {
13+
Rectangle* array = CopyArray(items);
14+
15+
int stepCount = 1;
16+
17+
for (int index = 1; index < LIST_SIZE; ++index) {
18+
int key = array[index].value;
19+
int j = index - 1;
20+
21+
while (j >= 0 && array[j].value > key) {
22+
array[j + 1].value = array[j].value;
23+
j = j - 1;
24+
25+
// INCREMENT THE STEP COUNTER (CHECKING)
26+
stepCount++;
27+
}
28+
array[j + 1].value = key;
29+
30+
// INCREMENT THE STEP COUNTER
31+
stepCount++;
32+
}
33+
34+
free(array);
35+
return stepCount;
36+
}
37+
38+
SortSequence GetSequence(Rectangle* items) {
39+
Rectangle* array = CopyArray(items);
40+
41+
// INITIALLY RUN THE SORT TO CALCULATE THE TOTAL NUMBER OF STEPS
42+
int stepCount = GetStepCount(items);
43+
44+
SortSequence sort = create_sort_sequence(stepCount);
45+
sort.steps = (SortStep*) malloc(LIST_SIZE * stepCount * sizeof(SortStep));
46+
47+
int currentStep = 0;
48+
int offset;
49+
50+
for (int index = 1; index < LIST_SIZE; ++index) {
51+
int key = array[index].value;
52+
int j = index - 1;
53+
54+
while (j >= 0 && array[j].value > key) {
55+
array[j + 1].value = array[j].value;
56+
j = j - 1;
57+
58+
// RECORD THE SORTING STEP (SNAPSHOT OF THE ARRAY)
59+
for (int i = 0; i < LIST_SIZE; i++) {
60+
offset = (currentStep * LIST_SIZE) + i;
61+
sort.steps[offset].value = array[i].value;
62+
63+
// SET RECTANGLE COLOR VALUE
64+
bool isCurrent = i == (index - 1);
65+
bool isChecking = i == j;
66+
bool isOrdered = !isCurrent && !isChecking && i < index;
67+
sort.steps[offset].rect_color = GetRectangleColor(isOrdered, isCurrent, isChecking);
68+
}
69+
currentStep++;
70+
}
71+
array[j + 1].value = key;
72+
73+
// RECORD THE SORTING STEP (SNAPSHOT OF THE ARRAY)
74+
for (int i = 0; i < LIST_SIZE; i++) {
75+
offset = (currentStep * LIST_SIZE) + i;
76+
sort.steps[offset].value = array[i].value;
77+
78+
// SET RECTANGLE COLOR VALUE
79+
bool isCurrent = i == (index - 1);
80+
bool isChecking = i == j;
81+
bool isOrdered = !isCurrent && !isChecking && i < index;
82+
sort.steps[offset].rect_color = GetRectangleColor(isOrdered, isCurrent, isChecking);
83+
}
84+
currentStep++;
85+
}
86+
87+
// RECORD FINAL STEP (ORDERED LIST)
88+
for (int i = 0; i < LIST_SIZE; i++) {
89+
offset = (currentStep * LIST_SIZE) + i;
90+
sort.steps[offset].value = array[i].value;
91+
sort.steps[offset].rect_color = rect_green_color;
92+
}
93+
94+
free(array);
95+
return sort;
96+
}
97+
98+
}

core/src/utils/Sort.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <BubbleSort.hpp>
77
#include <CocktailSort.hpp>
88
#include <HeapSort.hpp>
9+
#include <InsertionSort.hpp>
910

1011
SortSequence GetSortSequence(int sortId, Rectangle* items) {
1112
switch(sortId) {
@@ -16,8 +17,7 @@ SortSequence GetSortSequence(int sortId, Rectangle* items) {
1617
case 2:
1718
return HeapSort::GetSequence(items);
1819
case 3:
19-
SDL_Log("Start Insertion Sort...");
20-
break;
20+
return InsertionSort::GetSequence(items);
2121
case 4:
2222
SDL_Log("Start Merge Sort...");
2323
break;

0 commit comments

Comments
 (0)