| 1 | #include <stdlib.h> |
| 2 | |
| 3 | #include "sort.h" |
| 4 | |
| 5 | int create_tab(int tab[], unsigned tab_size) { |
| 6 | tab = malloc(sizeof(unsigned) * tab_size); |
| 7 | if (tab == NULL) { |
| 8 | return -1; |
| 9 | } else { |
| 10 | return 0; |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | void free_tab(int tab[]) { |
| 15 | free(tab); |
| 16 | } |
| 17 | |
| 18 | /* we suppose both tab are already created */ |
| 19 | static void copy_tab(int src_tab[], int dest_tab[], unsigned min_tab_size, unsigned index_offset) { |
| 20 | for (unsigned i = 0; i < min_tab_size; i++) { |
| 21 | dest_tab[i + index_offset] = src_tab[i]; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | int concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2, int tab_dest[]) { |
| 26 | int rt = create_tab(tab_dest, tab_size1 + tab_size2); |
| 27 | |
| 28 | copy_tab(tab1, tab_dest, tab_size1, 0); |
| 29 | copy_tab(tab2, tab_dest, tab_size2, tab_size1); |
| 30 | return rt; |
| 31 | } |
| 32 | |
| 33 | int resize_tab(int tab[], unsigned tab_size) { |
| 34 | tab = realloc(tab, sizeof(int) * tab_size); |
| 35 | if (tab == NULL) { |
| 36 | return -1; |
| 37 | } else { |
| 38 | return 0; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /* number of occurences of an element in an unsorted array */ |
| 43 | int count_tab_element(int tab[], unsigned tab_size, int element) { |
| 44 | unsigned el_count = 0; |
| 45 | for (unsigned i = 0; i < tab_size; i++) { |
| 46 | if (tab[i] == element) { |
| 47 | el_count++; |
| 48 | } |
| 49 | } |
| 50 | return el_count; |
| 51 | } |
| 52 | |
| 53 | void sort_tab(int tab[], unsigned tab_size, criteria_cb criteria) { |
| 54 | sort_bubble_array(tab, tab_size, criteria); |
| 55 | } |