Browse code

Bump copyright year.

Xavier G authored on 01/01/2019 18:04:44
Showing 1 changed files
... ...
@@ -1,5 +1,5 @@
1 1
 /**
2
- * Copyright © 2016 Xavier G. <xavier@kindwolf.org>
2
+ * Copyright © 2016-2019 Xavier G. <xavier@kindwolf.org>
3 3
  * This work is free. You can redistribute it and/or modify it under the
4 4
  * terms of the Do What The Fuck You Want To Public License, Version 2,
5 5
  * as published by Sam Hocevar. See the COPYING file for more details.
Browse code

Replace printf statements with VERBOSE statements.

Xavier G authored on 25/08/2016 01:48:35
Showing 1 changed files
... ...
@@ -5,8 +5,6 @@
5 5
  * as published by Sam Hocevar. See the COPYING file for more details.
6 6
  */
7 7
 
8
-#include <stdio.h>
9
-
10 8
 void array_sort(unsigned int **array, unsigned int count);
11 9
 #define ARRAY_SORT 1
12 10
 #include "common.c"
... ...
@@ -48,5 +46,5 @@ void array_sort(unsigned int **array, unsigned int count) {
48 46
 		/* Optimization: iterate one less step next time */
49 47
 		target = latest_swap;
50 48
 	} while (latest_swap);
51
-	printf("  Bubble-sort: made %u swaps to sort %u items.\n", swap_count, count);
49
+	VERBOSE(1, "  Bubble sort: made %u swaps to sort %u items.\n", swap_count, count);
52 50
 }
Browse code

Change the way we include common.c.

Xavier G authored on 25/08/2016 01:37:59
Showing 1 changed files
... ...
@@ -7,6 +7,10 @@
7 7
 
8 8
 #include <stdio.h>
9 9
 
10
+void array_sort(unsigned int **array, unsigned int count);
11
+#define ARRAY_SORT 1
12
+#include "common.c"
13
+
10 14
 /*
11 15
  * Implementation of bubble sort:
12 16
  */
... ...
@@ -46,6 +50,3 @@ void array_sort(unsigned int **array, unsigned int count) {
46 50
 	} while (latest_swap);
47 51
 	printf("  Bubble-sort: made %u swaps to sort %u items.\n", swap_count, count);
48 52
 }
49
-
50
-#define ARRAY_SORT 1
51
-#include "common.c"
Browse code

Bubble-sort: add copyright notice.

Xavier G authored on 23/08/2016 20:51:11
Showing 1 changed files
... ...
@@ -1,3 +1,10 @@
1
+/**
2
+ * Copyright © 2016 Xavier G. <xavier@kindwolf.org>
3
+ * This work is free. You can redistribute it and/or modify it under the
4
+ * terms of the Do What The Fuck You Want To Public License, Version 2,
5
+ * as published by Sam Hocevar. See the COPYING file for more details.
6
+ */
7
+
1 8
 #include <stdio.h>
2 9
 
3 10
 /*
Browse code

Bubble sort: fix an English mistake in the comments.

Xavier G authored on 23/08/2016 03:36:18
Showing 1 changed files
... ...
@@ -10,7 +10,7 @@ void array_sort(unsigned int **array, unsigned int count) {
10 10
 	unsigned int target;        /* Target for our iterations. */
11 11
 	int latest_swap;            /* Whether we had to swap in the past iteration. */
12 12
 
13
-	/* Do nothing unless there is at least two items to sort: */
13
+	/* Do nothing unless there are at least two items to sort: */
14 14
 	if (count < 2) return;
15 15
 
16 16
 	/* Bubble-sort is an in-place algorithm, i.e. we work directly on the
Browse code

Add bubble sort.

Xavier G authored on 23/08/2016 02:35:03
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,44 @@
1
+#include <stdio.h>
2
+
3
+/*
4
+ * Implementation of bubble sort:
5
+ */
6
+void array_sort(unsigned int **array, unsigned int count) {
7
+	unsigned int *src;          /* Unsorted, source array. */
8
+	unsigned int register swap; /* Used to swap integers. */
9
+	unsigned int swap_count;    /* Used to count swap operations. */
10
+	unsigned int target;        /* Target for our iterations. */
11
+	int latest_swap;            /* Whether we had to swap in the past iteration. */
12
+
13
+	/* Do nothing unless there is at least two items to sort: */
14
+	if (count < 2) return;
15
+
16
+	/* Bubble-sort is an in-place algorithm, i.e. we work directly on the
17
+	provided array: */
18
+	src = *array;
19
+
20
+	swap_count = 0;
21
+	target = count;
22
+	
23
+	do {
24
+		latest_swap = 0;
25
+		for (unsigned int i = 1; i < target ; ++ i) {
26
+			if (src[i - 1] > src[i]) {
27
+				/* Swap the values: */
28
+				swap = src[i - 1];
29
+				src[i - 1] = src[i];
30
+				src[i] = swap;
31
+				/* Note: having declared swap as register is, sadly, only
32
+				visible when compiling with no optimization (-O0). */
33
+				++ swap_count;
34
+				latest_swap = i;
35
+			}
36
+		}
37
+		/* Optimization: iterate one less step next time */
38
+		target = latest_swap;
39
+	} while (latest_swap);
40
+	printf("  Bubble-sort: made %u swaps to sort %u items.\n", swap_count, count);
41
+}
42
+
43
+#define ARRAY_SORT 1
44
+#include "common.c"