summaryrefslogtreecommitdiff
path: root/algo1w2/main.c
blob: 84eb41feb9faf042bef3420bddfcfe7be69a6963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <memory.h>
#include "main.h"

#include "bubble.h"
#include "selection.h"
#include "insertion.h"

int main() {
	const int original[] = { 7, 3, 8, 1, 2, 5, 4, 6, 9, 0 };
	const unsigned size = sizeof(original) / sizeof(int); // array lengte (niet sizeof)
	int* arr = malloc(sizeof(original));
	memcpy(arr, original, sizeof(original));

	printf("unsorted: ");
	print_arr(arr, size);
	printf("\n");

	printf("-- bubble sort --\n");
	bubble_sort(arr, size);
	printf("sorted:   ");
	print_arr(arr, size);
	printf("\n");

	memcpy(arr, original, sizeof(original)); // reset array
	printf("-- selection sort --\n");
	selection_sort(arr, size);
	printf("sorted: ");
	print_arr(arr, size);
	printf("\n");

	memcpy(arr, original, sizeof(original)); // reset array
	printf("-- insertion sort --\n");
	insertion_sort(arr, size);
	printf("sorted:");
	print_arr(arr, size);
	printf("\n");


	return 0;
}

void print_arr(int* arr, unsigned size) {
	printf("[");
	for (unsigned i = 0; i < size; i++) {
		if (i > 0) printf(", ");
		printf("%d", arr[i]);
	}
	printf("]\n");
}