Browse code

Merge sort: reorganize some conditions.

Xavier G authored on 25/08/2016 02:20:23
Showing 1 changed files
... ...
@@ -55,27 +55,21 @@ void array_sort(unsigned int **array, unsigned int count) {
55 55
             }
56 56
 
57 57
 			for (j = left_i; j <= right_end; ++ j) {
58
-				if (left_i <= left_end && right_i <= right_end) {
59
-					/* There remain items on both sides: compare them: */
60
-					if (current[left_i] <= current[right_i]) {
61
-						other[j] = current[left_i];
62
-						++ left_i;
63
-					}
64
-					else {
65
-						other[j] = current[right_i];
66
-						++ right_i;
67
-					}
58
+				/* Pick from the left array... */
59
+				if (
60
+					left_i <= left_end && /* if still available, and if... */
61
+					(
62
+						right_i > right_end || /* the right one was emptied... */
63
+						current[left_i] <= current[right_i] /* or the left value is smaller. */
64
+					)
65
+				) {
66
+					other[j] = current[left_i];
67
+					++ left_i;
68 68
 				}
69
-				else if (left_i > left_end) {
70
-					/* There remain no more items on the left side: */
69
+				else {
71 70
 					other[j] = current[right_i];
72 71
 					++ right_i;
73 72
 				}
74
-				else if (right_i > right_end) {
75
-					/* There remain no more items on the right side: */
76
-					other[j] = current[left_i];
77
-					++ left_i;
78
-				}
79 73
 			}
80 74
 		}
81 75
 		/* Swap the roles of our arrays: */