diff options
Diffstat (limited to 'test/mppa/sort/insertion.c')
-rw-r--r-- | test/mppa/sort/insertion.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/test/mppa/sort/insertion.c b/test/mppa/sort/insertion.c new file mode 100644 index 00000000..2c6065e7 --- /dev/null +++ b/test/mppa/sort/insertion.c @@ -0,0 +1,52 @@ +#include "../lib/prng.h" +#include "../lib/types.h" + +void swap(uint64_t *a, uint64_t *b){ + uint64_t tmp = *a; + *a = *b; + *b = tmp; +} + +int insert_sort(uint64_t *res, const uint64_t *T, int size){ + if (size <= 0) + return -1; + + for (int i = 0 ; i < size ; i++) + res[i] = T[i]; + + for (int i = 0 ; i < size-1 ; i++){ + if (res[i] > res[i+1]){ + swap(&res[i], &res[i+1]); + for (int j = i ; j > 1 ; j--) + if (res[j-1] > res[j]) + swap(&res[j-1], &res[j]); + } + } + + return 0; +} + +#ifdef __UNIT_TEST_INSERTION__ +#define SIZE 100 + +int main(void){ + uint64_t T[SIZE]; + uint64_t res[SIZE]; + srand(42); + + for (int i = 0 ; i < SIZE ; i++) + T[i] = randlong(); + + /* Sorting the table */ + if (insert_sort(res, T, SIZE) < 0) return -1; + + /* Computing max(T) */ + uint64_t max = T[0]; + for (int i = 1 ; i < SIZE ; i++) + if (T[i] > max) + max = T[i]; + + /* We should have: max(T) == res[SIZE] */ + return !(max == res[SIZE-1]); +} +#endif // __UNIT_TEST_INSERTION__ |