Skip to content

Commit e02f030

Browse files
committed
Fixed a bug in the new implemented merge sort, and implemented the unit test.
1 parent 846420c commit e02f030

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

Algorithms/Sorting/Sources/MergeSort.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,22 +180,26 @@ void mergeSortHelper(char *arr, int length, int elemSize, int (*cmp)(const void
180180
mergeSortHelper(arr, firstHalfLength, elemSize, cmp);
181181
mergeSortHelper(arr + firstHalfLength * elemSize, secondHalfLength, elemSize, cmp);
182182

183-
char *tempArr = (char *) malloc(length * elemSize);
183+
char *tempArr = (char *) malloc(length * elemSize), *firstHalfPointer = arr;
184184
int counter = 0;
185-
short firstHalfCopiedBool = 0;
185+
186186
for (int i = firstHalfLength; i < length; i++) {
187187

188-
if (!firstHalfCopiedBool && cmp(arr + i * elemSize, arr) > 0) {
189-
for (int j = 0; j < firstHalfLength; j++) {
190-
memcpy(tempArr + counter++ * elemSize, arr + j * elemSize, elemSize);
191-
}
192-
firstHalfCopiedBool = 1;
188+
while (firstHalfPointer != arr + firstHalfLength * elemSize &&
189+
cmp(arr + i * elemSize, firstHalfPointer) > 0) {
190+
memcpy(tempArr + counter++ * elemSize, firstHalfPointer, elemSize);
191+
firstHalfPointer += elemSize;
193192
}
194193

195194
memcpy(tempArr + counter++ * elemSize, arr + i * elemSize, elemSize);
196195

197196
}
198197

198+
while (firstHalfPointer != arr + firstHalfLength * elemSize) {
199+
memcpy(tempArr + counter++ * elemSize, firstHalfPointer, elemSize);
200+
firstHalfPointer += elemSize;
201+
}
202+
199203
for (int i = 0; i < length; i++) {
200204
memcpy(arr + i * elemSize , tempArr + i * elemSize, elemSize);
201205
}

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ add_executable(C_DataStructures
1515
"Unit Test/CuTest"
1616
"Unit Test/CuTest/CuTest.c"
1717
"Unit Test/CuTest/CuTest.h"
18-
#"Unit Test/CuTest/AllTests.c"
19-
#"Unit Test/CuTest/CuTestTest.c"
20-
#"Unit Test/ErrorsTestStruct.h"
18+
"Unit Test/CuTest/AllTests.c"
19+
"Unit Test/CuTest/CuTestTest.c"
20+
"Unit Test/ErrorsTestStruct.h"
2121

2222
DataStructure/Stacks/Sources/Stack.c
2323
DataStructure/Stacks/Headers/Stack.h

Unit Test/Tests/AlgorithmsTests/SortAlgTests/SortAlgTest.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,29 @@ void testMergeSort(CuTest *cuTest) {
122122
for (int i = 9; i >= 0; i--)
123123
CuAssertIntEquals(cuTest, i + 1, arr[9 - i]);
124124

125+
int arr2[] = {5, 3, 9, 7, 6, 1, 2, 4, 10, 8};
126+
127+
mergeSortWS(NULL, 0, 0, NULL);
128+
CuAssertIntEquals(cuTest, NULL_POINTER, ERROR_TEST->errorCode);
129+
130+
mergeSortWS(arr2, -1, 0, NULL);
131+
CuAssertIntEquals(cuTest, INVALID_ARG, ERROR_TEST->errorCode);
132+
133+
mergeSortWS(arr2, 1, 0, NULL);
134+
CuAssertIntEquals(cuTest, INVALID_ARG, ERROR_TEST->errorCode);
135+
136+
mergeSortWS(arr2, 1, 1, NULL);
137+
CuAssertIntEquals(cuTest, INVALID_ARG, ERROR_TEST->errorCode);
138+
139+
mergeSortWS(arr2, 10, sizeof(int), intCompareFunSoAT1);
140+
141+
for (int i = 0; i < 10; i++)
142+
CuAssertIntEquals(cuTest, i + 1, arr2[i]);
143+
144+
mergeSortWS(arr2, 10, sizeof(int), intCompareFunSoAT2);
145+
for (int i = 9; i >= 0; i--)
146+
CuAssertIntEquals(cuTest, i + 1, arr2[9 - i]);
147+
125148
}
126149

127150

main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
// ██░░░░░░░░░░██ ██░░░░░░░░████ ██░░░░░░░░░░██ ██░░██ ██░░██ ██░░░░░░░░░░██
2121
// ██████████████ ████████████ ██████████████ ██████ ██████ ██████████████
2222

23-
int main(void) {
2423

24+
25+
int main(void) {
2526
return 0;
2627
}
2728

0 commit comments

Comments
 (0)