| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- static int pile[256];
- static int pileQ[256];
- static int pileR[256];
- #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
- __attribute__((pure)) static int compare(const void * restrict p1, const void * restrict p2)
- {
- int left = *(const int *)p1;
- int right = *(const int *)p2;
- return (left > right) ? 1 : (left == right) ? 0 : -1;
- }
- static int is_equal(const int * restrict p1, const int * restrict p2, size_t n)
- {
- while (n--) {
- if (*p1 != *p2)
- return -1;
- ++p1;
- ++p2;
- }
- return 0;
- }
- static int get_max(const int *p1, size_t n)
- {
- int max = *p1;
- --n;
- ++p1;
- while (n--) {
- if (max < *p1)
- max = *p1;
- ++p1;
- }
- return max;
- }
- int main()
- {
- arc4random_buf(pile, sizeof(pile));
- clock_t tq0, tq1;
- memcpy(pileQ, pile, sizeof pileQ);
- memcpy(pileR, pile, sizeof pileR);
- qsort(&pileQ, ARRAY_SIZE(pileQ), sizeof(*pileQ), compare);
- heapsort(&pileR, ARRAY_SIZE(pileR), sizeof(*pileR), compare);
- fputs("000: ", stdout);
- for (size_t k = 0; k < ARRAY_SIZE(pileQ); ) {
- printf("%16d ", pileQ[k]);
- ++k;
- if (!(k % 8)) {
- puts("");
- printf("%03zu: ", k);
- }
- }
- puts("");
- printf("sort: %s.\n", is_equal(pileR, pileQ, ARRAY_SIZE(pile)) ? "KO" : "OK");
- printf("Max : %d.\n", get_max(pile, ARRAY_SIZE(pile)));
- return 0;
- }
|