summaryrefslogtreecommitdiffhomepage
path: root/misc/examples
diff options
context:
space:
mode:
Diffstat (limited to 'misc/examples')
-rw-r--r--misc/examples/prime.c9
-rw-r--r--misc/examples/sort.c64
2 files changed, 4 insertions, 69 deletions
diff --git a/misc/examples/prime.c b/misc/examples/prime.c
index 4a3b8498..1a272e78 100644
--- a/misc/examples/prime.c
+++ b/misc/examples/prime.c
@@ -27,20 +27,19 @@ cbits sieveOfEratosthenes(intptr_t n)
int main(void)
{
intptr_t n = 1000000000;
- printf("computing prime numbers up to %" c_ZI "\n", n);
+ printf("Computing prime numbers up to %" c_ZI "\n", n);
clock_t t1 = clock();
c_with (cbits primes = sieveOfEratosthenes(n + 1), cbits_drop(&primes)) {
- puts("done");
intptr_t np = cbits_count(&primes);
clock_t t2 = clock();
- printf("number of primes: %" c_ZI ", time: %f\n", np, (float)(t2 - t1) / (float)CLOCKS_PER_SEC);
+ printf("Number of primes: %" c_ZI ", time: %f\n\n", np, (float)(t2 - t1) / (float)CLOCKS_PER_SEC);
puts("Show all the primes in the range [2, 1000):");
printf("2");
c_forrange (i, 3, 1000, 2)
if (cbits_test(&primes, i>>1)) printf(" %lld", i);
- puts("");
+ puts("\n");
puts("Show the last 50 primes using a temporary crange generator:");
crange R = crange_make(n - 1, 0, -2);
@@ -48,7 +47,7 @@ int main(void)
, cbits_test(&primes, *i.ref>>1)
, c_flt_take(i, 50)) {
printf("%lld ", *i.ref);
- if (i.count % 10 == 0) puts("");
+ if (c_flt_last(i) % 10 == 0) puts("");
}
}
}
diff --git a/misc/examples/sort.c b/misc/examples/sort.c
deleted file mode 100644
index 65077143..00000000
--- a/misc/examples/sort.c
+++ /dev/null
@@ -1,64 +0,0 @@
-#include <time.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <stc/crandom.h>
-
-#ifdef __cplusplus
- #include <algorithm>
- typedef long long csort_elm_value;
-#else
- #define i_tag elm
- #define i_val long long
- #include <stc/algo/csort.h>
-#endif
-#define fmt_Elem "%lld"
-
-
-int testsort(csort_elm_value *a, long size, const char *desc) {
- clock_t t = clock();
-#ifdef __cplusplus
- printf("std::sort: ");
- std::sort(a, a + size);
-#else
- printf("csort: ");
- csort_elm(a, size);
-#endif
- t = clock() - t;
-
- printf("%s: %d elements sorted in %.2f ms\n",
- desc, (int)size, (float)t*1000.0f/CLOCKS_PER_SEC);
- return 0;
-}
-
-int main(int argc, char *argv[]) {
- long i, size = argc > 1 ? strtol(argv[1], NULL, 0) : 10000000;
- csort_elm_value *a = (csort_elm_value*)c_malloc(c_sizeof(*a) * size);
- if (a == NULL) return -1;
-
- for (i = 0; i < size; i++)
- a[i] = crandom() & ((1U << 28) - 1);
-
- testsort(a, size, "random");
- for (i = 0; i < 20; i++)
- printf(" " fmt_Elem, a[i]);
- puts("");
- for (i = 1; i < size; i++)
- if (a[i - 1] > a[i]) return -1;
-
- testsort(a, size, "sorted");
-
- for (i = 0; i < size; i++) a[i] = size - i;
- testsort(a, size, "reverse sorted");
-
- for (i = 0; i < size; i++) a[i] = 126735;
- testsort(a, size, "constant");
-
- for (i = 0; i < size; i++) a[i] = i + 1;
- a[size - 1] = 0;
- testsort(a, size, "rotated");
-
- for (i = 0; i < size; i++) a[i] = i % (size / 8);
- testsort(a, size, "repeated");
-
- free(a);
-}