Skip to content

Commit 88a520d

Browse files
Add sort algorithm implementations
1 parent 5b6fc75 commit 88a520d

File tree

6 files changed

+284
-0
lines changed

6 files changed

+284
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CC=gcc
2+
CFLGAS=-Wall
3+
DEPS =
4+
OBJ = bubblesort
5+
6+
%.o: %.c $(DEPS)
7+
$(CC) -c -o $@ $< $(CFLAGS)
8+
9+
$(OBJ): $(OBJ).o
10+
$(CC) -o $@ $^ $(CFLAGS)
11+
12+
clean:
13+
rm -f $(OBJ) $(OBJ).o
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <time.h>
4+
5+
typedef void (*sortAlgorithm)(int *array, int size);
6+
7+
void bubblesort(int *array, int size) {
8+
int i, j;
9+
int temp;
10+
11+
for (i = size; i >= 0; i--) {
12+
for (j = 0; j < i-1; j++) {
13+
if (array[j] > array[j+1]) {
14+
temp = array[j];
15+
array[j] = array[j+1];
16+
array[j+1] = temp;
17+
}
18+
}
19+
}
20+
}
21+
22+
void printArray(int arr[], int size)
23+
{
24+
int i;
25+
for (i=0; i < size; i++)
26+
printf("%d ", arr[i]);
27+
printf("\n");
28+
}
29+
30+
void tests(int *nums, int size) {
31+
sortAlgorithm sort_method = bubblesort;
32+
clock_t start, end;
33+
double cpu_time_used;
34+
35+
start = clock();
36+
sort_method(nums, size);
37+
end = clock();
38+
39+
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
40+
41+
printf("==== Sorted array test results ====\n");
42+
printArray(nums, size);
43+
printf("CPU time used: %f\n\n", cpu_time_used);
44+
}
45+
46+
int main() {
47+
// test 1
48+
int nums[] = {10, 7, 8, 9, 1, 5};
49+
int n = sizeof(nums)/sizeof(nums[0]);
50+
tests(nums, n);
51+
52+
// test 2
53+
int nums2[] = {1, 2, 4, 6, 8, 2, 3, 4, 0, -1, 10, 7, 8, 9, 1, 5};
54+
n = sizeof(nums2)/sizeof(nums2[0]);
55+
tests(nums2, n);
56+
57+
// test 3
58+
int nums3[] = {};
59+
n = sizeof(nums3)/sizeof(nums3[0]);
60+
tests(nums3, n);
61+
62+
// test 4
63+
int nums4[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
64+
n = sizeof(nums4)/sizeof(nums4[0]);
65+
tests(nums4, n);
66+
67+
return 0;
68+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CC=gcc
2+
CFLGAS=-Wall
3+
DEPS =
4+
OBJ = insertionSort
5+
6+
%.o: %.c $(DEPS)
7+
$(CC) -c -o $@ $< $(CFLAGS)
8+
9+
$(OBJ): $(OBJ).o
10+
$(CC) -o $@ $^ $(CFLAGS)
11+
12+
clean:
13+
rm -f $(OBJ) $(OBJ).o
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <time.h>
4+
5+
typedef void (*sortAlgorithm)(int *array, int size);
6+
7+
void insertionSort(int *array, int size) {
8+
int i, j;
9+
int temp;
10+
11+
for (i = 0; i < size; i++) {
12+
temp = array[i];
13+
for (j = i; j >= 1; j--) {
14+
if (array[j-1] <= temp) {
15+
break;
16+
}
17+
array[j] = array[j-1];
18+
}
19+
array[j] = temp;
20+
}
21+
}
22+
23+
void printArray(int arr[], int size)
24+
{
25+
int i;
26+
for (i=0; i < size; i++)
27+
printf("%d ", arr[i]);
28+
printf("\n");
29+
}
30+
31+
void tests(int *nums, int size) {
32+
sortAlgorithm sort_method = insertionSort;
33+
clock_t start, end;
34+
double cpu_time_used;
35+
36+
printf("==== Sorted array test results ====\n");
37+
printf("Original:\n");
38+
printArray(nums, size);
39+
40+
start = clock();
41+
sort_method(nums, size);
42+
end = clock();
43+
44+
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
45+
46+
printf("Sorted:\n");
47+
printArray(nums, size);
48+
printf("CPU time used: %f\n\n", cpu_time_used);
49+
}
50+
51+
int main() {
52+
// test 1
53+
int nums[] = {10, 7, 8, 9, 1, 5};
54+
int n = sizeof(nums)/sizeof(nums[0]);
55+
tests(nums, n);
56+
57+
// test 2
58+
int nums2[] = {1, 2, 4, 6, 8, 2, 3, 4, 0, -1, 10, 7, 8, 9, 1, 5};
59+
n = sizeof(nums2)/sizeof(nums2[0]);
60+
tests(nums2, n);
61+
62+
// test 3
63+
int nums3[] = {};
64+
n = sizeof(nums3)/sizeof(nums3[0]);
65+
tests(nums3, n);
66+
67+
// test 4
68+
int nums4[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
69+
n = sizeof(nums4)/sizeof(nums4[0]);
70+
tests(nums4, n);
71+
72+
// test 5
73+
int nums5[] = {1, 2, -4, 6, -8, 2, 3, -4, 0, -1, 10, -1, 5};
74+
n = sizeof(nums5)/sizeof(nums5[0]);
75+
tests(nums5, n);
76+
77+
return 0;
78+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CC=gcc
2+
CFLGAS=-Wall
3+
DEPS =
4+
OBJ = mergesort
5+
6+
%.o: %.c $(DEPS)
7+
$(CC) -c -o $@ $< $(CFLAGS)
8+
9+
$(OBJ): $(OBJ).o
10+
$(CC) -o $@ $^ $(CFLAGS)
11+
12+
clean:
13+
rm -f $(OBJ) $(OBJ).o
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <time.h>
4+
#include <string.h>
5+
6+
typedef void (*sortAlgorithm)(int *array, int size);
7+
8+
static void printArray(int arr[], int size)
9+
{
10+
int i;
11+
for (i=0; i < size; i++)
12+
printf("%d ", arr[i]);
13+
printf("\n");
14+
}
15+
16+
static void merge_helper(int *array, int st_a, int st_b, int size) {
17+
int arr[size];
18+
19+
int i = st_a;
20+
int j = st_b;
21+
int k = 0;
22+
23+
while (i < st_b && j < (st_a + size)) {
24+
arr[k++] = (array[i] > array[j]) ? array[j++] : array[i++];
25+
}
26+
27+
while (i < st_b)
28+
arr[k++] = array[i++];
29+
30+
while (j < (st_a + size))
31+
arr[k++] = array[j++];
32+
33+
for (k = 0; k < size; k++)
34+
array[st_a++] = arr[k];
35+
}
36+
37+
static void sort_helper(int *array, int st, int end) {
38+
int mid = (end-st)/2 + st;
39+
int temp;
40+
41+
if (st < end) {
42+
sort_helper(array, st, mid);
43+
sort_helper(array, mid+1, end);
44+
merge_helper(array, st, mid+1, end-st+1);
45+
}
46+
}
47+
48+
void mergesort(int *array, int size) {
49+
sort_helper(array, 0, size-1);
50+
}
51+
52+
void tests(int *nums, int size) {
53+
sortAlgorithm sort_method = mergesort;
54+
clock_t start, end;
55+
double cpu_time_used;
56+
57+
printf("==== Sorted array test results ====\n");
58+
printf("Original:\n");
59+
printArray(nums, size);
60+
61+
start = clock();
62+
sort_method(nums, size);
63+
end = clock();
64+
65+
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
66+
67+
printf("Sorted:\n");
68+
printArray(nums, size);
69+
printf("CPU time used: %f\n\n", cpu_time_used);
70+
}
71+
72+
int main() {
73+
// test 1
74+
int nums[] = {10, 7, 8, 9, 1, 5};
75+
int n = sizeof(nums)/sizeof(nums[0]);
76+
tests(nums, n);
77+
78+
// test 2
79+
int nums2[] = {1, 2, 4, 6, 8, 2, 3, 4, 0, -1, 10, 7, 8, 9, 1, 5};
80+
n = sizeof(nums2)/sizeof(nums2[0]);
81+
tests(nums2, n);
82+
83+
// test 3
84+
int nums3[] = {};
85+
n = sizeof(nums3)/sizeof(nums3[0]);
86+
tests(nums3, n);
87+
88+
// test 4
89+
int nums4[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
90+
n = sizeof(nums4)/sizeof(nums4[0]);
91+
tests(nums4, n);
92+
93+
// test 5
94+
int nums5[] = {1, 2, -4, 6, -8, 2, 3, -4, 0, -1, 10, -1, 5};
95+
n = sizeof(nums5)/sizeof(nums5[0]);
96+
tests(nums5, n);
97+
98+
return 0;
99+
}

0 commit comments

Comments
 (0)