aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/quicksort/quicksort.c
diff options
context:
space:
mode:
authorDavid Monniaux <david.monniaux@univ-grenoble-alpes.fr>2020-03-03 08:17:40 +0100
committerDavid Monniaux <david.monniaux@univ-grenoble-alpes.fr>2020-03-03 08:17:40 +0100
commit1ab7b51c30e1b10ac45b0bd64cefdc01da0f7f68 (patch)
tree210ffc156c83f04fb0c61a40b4f9037d7ba8a7e1 /test/monniaux/quicksort/quicksort.c
parent222c9047d61961db9c6b19fed5ca49829223fd33 (diff)
parent12be46d59a2483a10d77fa8ee67f7e0ca1bd702f (diff)
downloadcompcert-kvx-1ab7b51c30e1b10ac45b0bd64cefdc01da0f7f68.tar.gz
compcert-kvx-1ab7b51c30e1b10ac45b0bd64cefdc01da0f7f68.zip
Merge branch 'mppa-cse2' of gricad-gitlab.univ-grenoble-alpes.fr:sixcy/CompCert into mppa-work
Diffstat (limited to 'test/monniaux/quicksort/quicksort.c')
-rw-r--r--test/monniaux/quicksort/quicksort.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/test/monniaux/quicksort/quicksort.c b/test/monniaux/quicksort/quicksort.c
new file mode 100644
index 00000000..4b93ae7b
--- /dev/null
+++ b/test/monniaux/quicksort/quicksort.c
@@ -0,0 +1,42 @@
+#include "quicksort.h"
+
+/* Rosetta Code */
+void quicksort(data *A, int len) {
+ if (len < 2) return;
+
+ data pivot = A[len / 2];
+
+ int i, j;
+ for (i = 0, j = len - 1; ; i++, j--) {
+ while (A[i] < pivot) i++;
+ while (A[j] > pivot) j--;
+
+ if (i >= j) break;
+
+ data temp = A[i];
+ A[i] = A[j];
+ A[j] = temp;
+ }
+
+ quicksort(A, i);
+ quicksort(A + i, len - i);
+}
+
+data data_random(void) {
+ static uint64_t next = 1325997111;
+ next = next * 1103515249 + 12345;
+ return next;
+}
+
+void data_vec_random(data *a, unsigned len) {
+ for(unsigned i=0; i<len; i++) {
+ a[i] = data_random();
+ }
+}
+
+bool data_vec_is_sorted(const data *a, unsigned len) {
+ for(unsigned i=0; i<len-1; i++) {
+ if (a[i] > a[i+1]) return false;
+ }
+ return true;
+}