├── 0-O ├── 0-bubble_sort.c ├── 1-O ├── 1-insertion_sort_list.c ├── 100-shell_sort.c ├── 1000-sort_deck.c ├── 101-O ├── 101-cocktail_sort_list.c ├── 102-O ├── 102-counting_sort.c ├── 103-O ├── 103-merge_sort.c ├── 104-O ├── 104-heap_sort.c ├── 105-radix_sort.c ├── 106-O ├── 106-bitonic_sort.c ├── 107-O ├── 107-quick_sort_hoare.c ├── 2-O ├── 2-selection_sort.c ├── 3-O ├── 3-quick_sort.c ├── LICENSE ├── README.md ├── deck.h ├── print_array.c ├── print_list.c └── sort.h /0-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0-bubble_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | /** 4 | * swap_ints - Swap two integers in an array. 5 | * @a: The first integer to swap. 6 | * @b: The second integer to swap. 7 | */ 8 | void swap_ints(int *a, int *b) 9 | { 10 | int tmp; 11 | 12 | tmp = *a; 13 | *a = *b; 14 | *b = tmp; 15 | } 16 | 17 | /** 18 | * bubble_sort - Sort an array of integers in ascending order. 19 | * @array: An array of integers to sort. 20 | * @size: The size of the array. 21 | * 22 | * Description: Prints the array after each swap. 23 | */ 24 | void bubble_sort(int *array, size_t size) 25 | { 26 | size_t i, len = size; 27 | bool bubbly = false; 28 | 29 | if (array == NULL || size < 2) 30 | return; 31 | 32 | while (bubbly == false) 33 | { 34 | bubbly = true; 35 | for (i = 0; i < len - 1; i++) 36 | { 37 | if (array[i] > array[i + 1]) 38 | { 39 | swap_ints(array + i, array + i + 1); 40 | print_array(array, size); 41 | bubbly = false; 42 | } 43 | } 44 | len--; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /1-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /1-insertion_sort_list.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | /** 4 | * swap_nodes - Swap two nodes in a listint_t doubly-linked list. 5 | * @h: A pointer to the head of the doubly-linked list. 6 | * @n1: A pointer to the first node to swap. 7 | * @n2: The second node to swap. 8 | */ 9 | void swap_nodes(listint_t **h, listint_t **n1, listint_t *n2) 10 | { 11 | (*n1)->next = n2->next; 12 | if (n2->next != NULL) 13 | n2->next->prev = *n1; 14 | n2->prev = (*n1)->prev; 15 | n2->next = *n1; 16 | if ((*n1)->prev != NULL) 17 | (*n1)->prev->next = n2; 18 | else 19 | *h = n2; 20 | (*n1)->prev = n2; 21 | *n1 = n2->prev; 22 | } 23 | 24 | /** 25 | * insertion_sort_list - Sorts a doubly linked list of integers 26 | * using the insertion sort algorithm. 27 | * @list: A pointer to the head of a doubly-linked list of integers. 28 | * 29 | * Description: Prints the list after each swap. 30 | */ 31 | void insertion_sort_list(listint_t **list) 32 | { 33 | listint_t *iter, *insert, *tmp; 34 | 35 | if (list == NULL || *list == NULL || (*list)->next == NULL) 36 | return; 37 | 38 | for (iter = (*list)->next; iter != NULL; iter = tmp) 39 | { 40 | tmp = iter->next; 41 | insert = iter->prev; 42 | while (insert != NULL && iter->n < insert->n) 43 | { 44 | swap_nodes(list, &insert, iter); 45 | print_list((const listint_t *)*list); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /100-shell_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | /** 4 | * swap_ints - Swap two integers in an array. 5 | * @a: The first integer to swap. 6 | * @b: The second integer to swap. 7 | */ 8 | void swap_ints(int *a, int *b) 9 | { 10 | int tmp; 11 | 12 | tmp = *a; 13 | *a = *b; 14 | *b = tmp; 15 | } 16 | 17 | /** 18 | * shell_sort - Sort an array of integers in ascending 19 | * order using the shell sort algorithm. 20 | * @array: An array of integers. 21 | * @size: The size of the array. 22 | * 23 | * Description: Uses the Knuth interval sequence. 24 | */ 25 | void shell_sort(int *array, size_t size) 26 | { 27 | size_t gap, m, n; 28 | 29 | if (array == NULL || size < 2) 30 | return; 31 | 32 | for (gap = 1; gap < (size / 3);) 33 | gap = gap * 3 + 1; 34 | 35 | for (; gap >= 1; gap /= 3) 36 | { 37 | for (m = gap; m < size; m++) 38 | { 39 | n = m; 40 | while (n >= gap && array[n - gap] > array[n]) 41 | { 42 | swap_ints(array + n, array + (n - gap)); 43 | n -= gap; 44 | } 45 | } 46 | print_array(array, size); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /1000-sort_deck.c: -------------------------------------------------------------------------------- 1 | #include "deck.h" 2 | 3 | int _strcmp(const char *s1, const char *s2); 4 | char get_value(deck_node_t *card); 5 | void insertion_sort_deck_kind(deck_node_t **deck); 6 | void insertion_sort_deck_value(deck_node_t **deck); 7 | void sort_deck(deck_node_t **deck); 8 | 9 | /** 10 | * _strcmp - Compares two strings. 11 | * @s1: The first string to be compared. 12 | * @s2: The second string to be compared. 13 | * 14 | * Return: Positive byte difference if s1 > s2 15 | * 0 if s1 == s2 16 | * Negative byte difference if s1 < s2 17 | */ 18 | int _strcmp(const char *s1, const char *s2) 19 | { 20 | while (*s1 && *s2 && *s1 == *s2) 21 | { 22 | s1++; 23 | s2++; 24 | } 25 | 26 | if (*s1 != *s2) 27 | return (*s1 - *s2); 28 | return (0); 29 | } 30 | 31 | /** 32 | * get_value - Get the numerical value of a card. 33 | * @card: A pointer to a deck_node_t card. 34 | * 35 | * Return: The numerical value of the card. 36 | */ 37 | char get_value(deck_node_t *card) 38 | { 39 | if (_strcmp(card->card->value, "Ace") == 0) 40 | return (0); 41 | if (_strcmp(card->card->value, "1") == 0) 42 | return (1); 43 | if (_strcmp(card->card->value, "2") == 0) 44 | return (2); 45 | if (_strcmp(card->card->value, "3") == 0) 46 | return (3); 47 | if (_strcmp(card->card->value, "4") == 0) 48 | return (4); 49 | if (_strcmp(card->card->value, "5") == 0) 50 | return (5); 51 | if (_strcmp(card->card->value, "6") == 0) 52 | return (6); 53 | if (_strcmp(card->card->value, "7") == 0) 54 | return (7); 55 | if (_strcmp(card->card->value, "8") == 0) 56 | return (8); 57 | if (_strcmp(card->card->value, "9") == 0) 58 | return (9); 59 | if (_strcmp(card->card->value, "10") == 0) 60 | return (10); 61 | if (_strcmp(card->card->value, "Jack") == 0) 62 | return (11); 63 | if (_strcmp(card->card->value, "Queen") == 0) 64 | return (12); 65 | return (13); 66 | } 67 | 68 | /** 69 | * insertion_sort_deck_kind - Sort a deck of cards from spades to diamonds. 70 | * @deck: A pointer to the head of a deck_node_t doubly-linked list. 71 | */ 72 | void insertion_sort_deck_kind(deck_node_t **deck) 73 | { 74 | deck_node_t *iter, *insert, *tmp; 75 | 76 | for (iter = (*deck)->next; iter != NULL; iter = tmp) 77 | { 78 | tmp = iter->next; 79 | insert = iter->prev; 80 | while (insert != NULL && insert->card->kind > iter->card->kind) 81 | { 82 | insert->next = iter->next; 83 | if (iter->next != NULL) 84 | iter->next->prev = insert; 85 | iter->prev = insert->prev; 86 | iter->next = insert; 87 | if (insert->prev != NULL) 88 | insert->prev->next = iter; 89 | else 90 | *deck = iter; 91 | insert->prev = iter; 92 | insert = iter->prev; 93 | } 94 | } 95 | } 96 | 97 | /** 98 | * insertion_sort_deck_value - Sort a deck of cards sorted from 99 | * spades to diamonds from ace to king. 100 | * @deck: A pointer to the head of a deck_node_t doubly-linked list. 101 | */ 102 | void insertion_sort_deck_value(deck_node_t **deck) 103 | { 104 | deck_node_t *iter, *insert, *tmp; 105 | 106 | for (iter = (*deck)->next; iter != NULL; iter = tmp) 107 | { 108 | tmp = iter->next; 109 | insert = iter->prev; 110 | while (insert != NULL && 111 | insert->card->kind == iter->card->kind && 112 | get_value(insert) > get_value(iter)) 113 | { 114 | insert->next = iter->next; 115 | if (iter->next != NULL) 116 | iter->next->prev = insert; 117 | iter->prev = insert->prev; 118 | iter->next = insert; 119 | if (insert->prev != NULL) 120 | insert->prev->next = iter; 121 | else 122 | *deck = iter; 123 | insert->prev = iter; 124 | insert = iter->prev; 125 | } 126 | } 127 | } 128 | 129 | /** 130 | * sort_deck - Sort a deck of cards from ace to king and 131 | * from spades to diamonds. 132 | * @deck: A pointer to the head of a deck_node_t doubly-linked list. 133 | */ 134 | void sort_deck(deck_node_t **deck) 135 | { 136 | if (deck == NULL || *deck == NULL || (*deck)->next == NULL) 137 | return; 138 | 139 | insertion_sort_deck_kind(deck); 140 | insertion_sort_deck_value(deck); 141 | } 142 | -------------------------------------------------------------------------------- /101-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /101-cocktail_sort_list.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | void swap_node_ahead(listint_t **list, listint_t **tail, listint_t **shaker); 4 | void swap_node_behind(listint_t **list, listint_t **tail, listint_t **shaker); 5 | void cocktail_sort_list(listint_t **list); 6 | 7 | /** 8 | * swap_node_ahead - Swap a node in a listint_t doubly-linked list 9 | * list of integers with the node ahead of it. 10 | * @list: A pointer to the head of a doubly-linked list of integers. 11 | * @tail: A pointer to the tail of the doubly-linked list. 12 | * @shaker: A pointer to the current swapping node of the cocktail shaker algo. 13 | */ 14 | void swap_node_ahead(listint_t **list, listint_t **tail, listint_t **shaker) 15 | { 16 | listint_t *tmp = (*shaker)->next; 17 | 18 | if ((*shaker)->prev != NULL) 19 | (*shaker)->prev->next = tmp; 20 | else 21 | *list = tmp; 22 | tmp->prev = (*shaker)->prev; 23 | (*shaker)->next = tmp->next; 24 | if (tmp->next != NULL) 25 | tmp->next->prev = *shaker; 26 | else 27 | *tail = *shaker; 28 | (*shaker)->prev = tmp; 29 | tmp->next = *shaker; 30 | *shaker = tmp; 31 | } 32 | 33 | /** 34 | * swap_node_behind - Swap a node in a listint_t doubly-linked 35 | * list of integers with the node behind it. 36 | * @list: A pointer to the head of a doubly-linked list of integers. 37 | * @tail: A pointer to the tail of the doubly-linked list. 38 | * @shaker: A pointer to the current swapping node of the cocktail shaker algo. 39 | */ 40 | void swap_node_behind(listint_t **list, listint_t **tail, listint_t **shaker) 41 | { 42 | listint_t *tmp = (*shaker)->prev; 43 | 44 | if ((*shaker)->next != NULL) 45 | (*shaker)->next->prev = tmp; 46 | else 47 | *tail = tmp; 48 | tmp->next = (*shaker)->next; 49 | (*shaker)->prev = tmp->prev; 50 | if (tmp->prev != NULL) 51 | tmp->prev->next = *shaker; 52 | else 53 | *list = *shaker; 54 | (*shaker)->next = tmp; 55 | tmp->prev = *shaker; 56 | *shaker = tmp; 57 | } 58 | 59 | /** 60 | * cocktail_sort_list - Sort a listint_t doubly-linked list of integers in 61 | * ascending order using the cocktail shaker algorithm. 62 | * @list: A pointer to the head of a listint_t doubly-linked list. 63 | */ 64 | void cocktail_sort_list(listint_t **list) 65 | { 66 | listint_t *tail, *shaker; 67 | bool shaken_not_stirred = false; 68 | 69 | if (list == NULL || *list == NULL || (*list)->next == NULL) 70 | return; 71 | 72 | for (tail = *list; tail->next != NULL;) 73 | tail = tail->next; 74 | 75 | while (shaken_not_stirred == false) 76 | { 77 | shaken_not_stirred = true; 78 | for (shaker = *list; shaker != tail; shaker = shaker->next) 79 | { 80 | if (shaker->n > shaker->next->n) 81 | { 82 | swap_node_ahead(list, &tail, &shaker); 83 | print_list((const listint_t *)*list); 84 | shaken_not_stirred = false; 85 | } 86 | } 87 | for (shaker = shaker->prev; shaker != *list; 88 | shaker = shaker->prev) 89 | { 90 | if (shaker->n < shaker->prev->n) 91 | { 92 | swap_node_behind(list, &tail, &shaker); 93 | print_list((const listint_t *)*list); 94 | shaken_not_stirred = false; 95 | } 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /102-O: -------------------------------------------------------------------------------- 1 | O(n+k) 2 | O(n+k) 3 | O(n+k) 4 | -------------------------------------------------------------------------------- /102-counting_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | /** 4 | * get_max - Get the maximum value in an array of integers. 5 | * @array: An array of integers. 6 | * @size: The size of the array. 7 | * 8 | * Return: The maximum integer in the array. 9 | */ 10 | int get_max(int *array, int size) 11 | { 12 | int max, q; 13 | 14 | for (max = array[0], q = 1; q < size; q++) 15 | { 16 | if (array[q] > max) 17 | max = array[q]; 18 | } 19 | 20 | return (max); 21 | } 22 | 23 | /** 24 | * counting_sort - Sort an array of integers in ascending order 25 | * using the counting sort algorithm. 26 | * @array: An array of integers. 27 | * @size: The size of the array. 28 | * 29 | * Description: Prints the counting array after setting it up. 30 | */ 31 | void counting_sort(int *array, size_t size) 32 | { 33 | int *count, *sorted, max, q; 34 | 35 | if (array == NULL || size < 2) 36 | return; 37 | 38 | sorted = malloc(sizeof(int) * size); 39 | if (sorted == NULL) 40 | return; 41 | max = get_max(array, size); 42 | count = malloc(sizeof(int) * (max + 1)); 43 | if (count == NULL) 44 | { 45 | free(sorted); 46 | return; 47 | } 48 | 49 | for (q = 0; q < (max + 1); q++) 50 | count[q] = 0; 51 | for (q = 0; q < (int)size; q++) 52 | count[array[q]] += 1; 53 | for (q = 0; q < (max + 1); q++) 54 | count[q] += count[q - 1]; 55 | print_array(count, max + 1); 56 | 57 | for (q = 0; q < (int)size; q++) 58 | { 59 | sorted[count[array[q]] - 1] = array[q]; 60 | count[array[q]] -= 1; 61 | } 62 | 63 | for (q = 0; q < (int)size; q++) 64 | array[q] = sorted[q]; 65 | 66 | free(sorted); 67 | free(count); 68 | } 69 | -------------------------------------------------------------------------------- /103-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(nlog(n)) 4 | -------------------------------------------------------------------------------- /103-merge_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | void merge_subarr(int *subarr, int *buff, size_t front, size_t mid, 4 | size_t back); 5 | void merge_sort_recursive(int *subarr, int *buff, size_t front, size_t back); 6 | void merge_sort(int *array, size_t size); 7 | 8 | /** 9 | * merge_subarr - Sort a subarray of integers. 10 | * @subarr: A subarray of an array of integers to sort. 11 | * @buff: A buffer to store the sorted subarray. 12 | * @front: The front index of the array. 13 | * @mid: The middle index of the array. 14 | * @back: The back index of the array. 15 | */ 16 | void merge_subarr(int *subarr, int *buff, size_t front, size_t mid, 17 | size_t back) 18 | { 19 | size_t i, j, k = 0; 20 | 21 | printf("Merging...\n[left]: "); 22 | print_array(subarr + front, mid - front); 23 | 24 | printf("[right]: "); 25 | print_array(subarr + mid, back - mid); 26 | 27 | for (i = front, j = mid; i < mid && j < back; k++) 28 | buff[k] = (subarr[i] < subarr[j]) ? subarr[i++] : subarr[j++]; 29 | for (; i < mid; i++) 30 | buff[k++] = subarr[i]; 31 | for (; j < back; j++) 32 | buff[k++] = subarr[j]; 33 | for (i = front, k = 0; i < back; i++) 34 | subarr[i] = buff[k++]; 35 | 36 | printf("[Done]: "); 37 | print_array(subarr + front, back - front); 38 | } 39 | 40 | /** 41 | * merge_sort_recursive - Implement the merge sort algorithm through recursion. 42 | * @subarr: A subarray of an array of integers to sort. 43 | * @buff: A buffer to store the sorted result. 44 | * @front: The front index of the subarray. 45 | * @back: The back index of the subarray. 46 | */ 47 | void merge_sort_recursive(int *subarr, int *buff, size_t front, size_t back) 48 | { 49 | size_t mid; 50 | 51 | if (back - front > 1) 52 | { 53 | mid = front + (back - front) / 2; 54 | merge_sort_recursive(subarr, buff, front, mid); 55 | merge_sort_recursive(subarr, buff, mid, back); 56 | merge_subarr(subarr, buff, front, mid, back); 57 | } 58 | } 59 | 60 | /** 61 | * merge_sort - Sort an array of integers in ascending 62 | * order using the merge sort algorithm. 63 | * @array: An array of integers. 64 | * @size: The size of the array. 65 | * 66 | * Description: Implements the top-down merge sort algorithm. 67 | */ 68 | void merge_sort(int *array, size_t size) 69 | { 70 | int *buff; 71 | 72 | if (array == NULL || size < 2) 73 | return; 74 | 75 | buff = malloc(sizeof(int) * size); 76 | if (buff == NULL) 77 | return; 78 | 79 | merge_sort_recursive(array, buff, 0, size); 80 | 81 | free(buff); 82 | } 83 | -------------------------------------------------------------------------------- /104-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(nlog(n)) 4 | -------------------------------------------------------------------------------- /104-heap_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | void swap_ints(int *a, int *b); 4 | void max_heapify(int *array, size_t size, size_t base, size_t root); 5 | void heap_sort(int *array, size_t size); 6 | 7 | /** 8 | * swap_ints - Swap two integers in an array. 9 | * @a: The first integer to swap. 10 | * @b: The second integer to swap. 11 | */ 12 | void swap_ints(int *a, int *b) 13 | { 14 | int tmp; 15 | 16 | tmp = *a; 17 | *a = *b; 18 | *b = tmp; 19 | } 20 | 21 | /** 22 | * max_heapify - Turn a binary tree into a complete binary heap. 23 | * @array: An array of integers representing a binary tree. 24 | * @size: The size of the array/tree. 25 | * @base: The index of the base row of the tree. 26 | * @root: The root node of the binary tree. 27 | */ 28 | void max_heapify(int *array, size_t size, size_t base, size_t root) 29 | { 30 | size_t left, right, large; 31 | 32 | left = 2 * root + 1; 33 | right = 2 * root + 2; 34 | large = root; 35 | 36 | if (left < base && array[left] > array[large]) 37 | large = left; 38 | if (right < base && array[right] > array[large]) 39 | large = right; 40 | 41 | if (large != root) 42 | { 43 | swap_ints(array + root, array + large); 44 | print_array(array, size); 45 | max_heapify(array, size, base, large); 46 | } 47 | } 48 | 49 | /** 50 | * heap_sort - Sort an array of integers in ascending 51 | * order using the heap sort algorithm. 52 | * @array: An array of integers. 53 | * @size: The size of the array. 54 | * 55 | * Description: Implements the sift-down heap sort 56 | * algorithm. Prints the array after each swap. 57 | */ 58 | void heap_sort(int *array, size_t size) 59 | { 60 | int y; 61 | 62 | if (array == NULL || size < 2) 63 | return; 64 | 65 | for (y = (size / 2) - 1; y >= 0; y--) 66 | max_heapify(array, size, size, y); 67 | 68 | for (y = size - 1; y > 0; y--) 69 | { 70 | swap_ints(array, array + y); 71 | print_array(array, size); 72 | max_heapify(array, size, y, 0); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /105-radix_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | int get_max(int *array, int size); 4 | void radix_counting_sort(int *array, size_t size, int sig, int *buff); 5 | void radix_sort(int *array, size_t size); 6 | 7 | /** 8 | * get_max - Get the maximum value in an array of integers. 9 | * @array: An array of integers. 10 | * @size: The size of the array. 11 | * 12 | * Return: The maximum integer in the array. 13 | */ 14 | int get_max(int *array, int size) 15 | { 16 | int max, i; 17 | 18 | for (max = array[0], i = 1; i < size; i++) 19 | { 20 | if (array[i] > max) 21 | max = array[i]; 22 | } 23 | 24 | return (max); 25 | } 26 | 27 | /** 28 | * radix_counting_sort - Sort the significant digits of an array of integers 29 | * in ascending order using the counting sort algorithm. 30 | * @array: An array of integers. 31 | * @size: The size of the array. 32 | * @sig: The significant digit to sort on. 33 | * @buff: A buffer to store the sorted array. 34 | */ 35 | void radix_counting_sort(int *array, size_t size, int sig, int *buff) 36 | { 37 | int count[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 38 | size_t i; 39 | 40 | for (i = 0; i < size; i++) 41 | count[(array[i] / sig) % 10] += 1; 42 | 43 | for (i = 0; i < 10; i++) 44 | count[i] += count[i - 1]; 45 | 46 | for (i = size - 1; (int)i >= 0; i--) 47 | { 48 | buff[count[(array[i] / sig) % 10] - 1] = array[i]; 49 | count[(array[i] / sig) % 10] -= 1; 50 | } 51 | 52 | for (i = 0; i < size; i++) 53 | array[i] = buff[i]; 54 | } 55 | 56 | /** 57 | * radix_sort - Sort an array of integers in ascending 58 | * order using the radix sort algorithm. 59 | * @array: An array of integers. 60 | * @size: The size of the array. 61 | * 62 | * Description: Implements the LSD radix sort algorithm. Prints 63 | * the array after each significant digit increase. 64 | */ 65 | void radix_sort(int *array, size_t size) 66 | { 67 | int max, sig, *buff; 68 | 69 | if (array == NULL || size < 2) 70 | return; 71 | 72 | buff = malloc(sizeof(int) * size); 73 | if (buff == NULL) 74 | return; 75 | 76 | max = get_max(array, size); 77 | for (sig = 1; max / sig > 0; sig *= 10) 78 | { 79 | radix_counting_sort(array, size, sig, buff); 80 | print_array(array, size); 81 | } 82 | 83 | free(buff); 84 | } 85 | -------------------------------------------------------------------------------- /106-O: -------------------------------------------------------------------------------- 1 | O(log^2(n)) 2 | O(log^2(n)) 3 | O(log^2(n)) 4 | -------------------------------------------------------------------------------- /106-bitonic_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | void swap_ints(int *a, int *b); 4 | void bitonic_merge(int *array, size_t size, size_t start, size_t seq, 5 | char flow); 6 | void bitonic_seq(int *array, size_t size, size_t start, size_t seq, char flow); 7 | void bitonic_sort(int *array, size_t size); 8 | 9 | /** 10 | * swap_ints - Swap two integers in an array. 11 | * @a: The first integer to swap. 12 | * @b: The second integer to swap. 13 | */ 14 | void swap_ints(int *a, int *b) 15 | { 16 | int tmp; 17 | 18 | tmp = *a; 19 | *a = *b; 20 | *b = tmp; 21 | } 22 | 23 | /** 24 | * bitonic_merge - Sort a bitonic sequence inside an array of integers. 25 | * @array: An array of integers. 26 | * @size: The size of the array. 27 | * @start: The starting index of the sequence in array to sort. 28 | * @seq: The size of the sequence to sort. 29 | * @flow: The direction to sort in. 30 | */ 31 | void bitonic_merge(int *array, size_t size, size_t start, size_t seq, 32 | char flow) 33 | { 34 | size_t i, jump = seq / 2; 35 | 36 | if (seq > 1) 37 | { 38 | for (i = start; i < start + jump; i++) 39 | { 40 | if ((flow == UP && array[i] > array[i + jump]) || 41 | (flow == DOWN && array[i] < array[i + jump])) 42 | swap_ints(array + i, array + i + jump); 43 | } 44 | bitonic_merge(array, size, start, jump, flow); 45 | bitonic_merge(array, size, start + jump, jump, flow); 46 | } 47 | } 48 | 49 | /** 50 | * bitonic_seq - Convert an array of integers into a bitonic sequence. 51 | * @array: An array of integers. 52 | * @size: The size of the array. 53 | * @start: The starting index of a block of the building bitonic sequence. 54 | * @seq: The size of a block of the building bitonic sequence. 55 | * @flow: The direction to sort the bitonic sequence block in. 56 | */ 57 | void bitonic_seq(int *array, size_t size, size_t start, size_t seq, char flow) 58 | { 59 | size_t cut = seq / 2; 60 | char *str = (flow == UP) ? "UP" : "DOWN"; 61 | 62 | if (seq > 1) 63 | { 64 | printf("Merging [%lu/%lu] (%s):\n", seq, size, str); 65 | print_array(array + start, seq); 66 | 67 | bitonic_seq(array, size, start, cut, UP); 68 | bitonic_seq(array, size, start + cut, cut, DOWN); 69 | bitonic_merge(array, size, start, seq, flow); 70 | 71 | printf("Result [%lu/%lu] (%s):\n", seq, size, str); 72 | print_array(array + start, seq); 73 | } 74 | } 75 | 76 | /** 77 | * bitonic_sort - Sort an array of integers in ascending 78 | * order using the bitonic sort algorithm. 79 | * @array: An array of integers. 80 | * @size: The size of the array. 81 | * 82 | * Description: Prints the array after each swap. Only works for 83 | * size = 2^k where k >= 0 (ie. size equal to powers of 2). 84 | */ 85 | void bitonic_sort(int *array, size_t size) 86 | { 87 | if (array == NULL || size < 2) 88 | return; 89 | 90 | bitonic_seq(array, size, 0, size, UP); 91 | } 92 | -------------------------------------------------------------------------------- /107-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /107-quick_sort_hoare.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | void swap_ints(int *a, int *b); 4 | int hoare_partition(int *array, size_t size, int left, int right); 5 | void hoare_sort(int *array, size_t size, int left, int right); 6 | void quick_sort_hoare(int *array, size_t size); 7 | 8 | /** 9 | * swap_ints - Swap two integers in an array. 10 | * @a: The first integer to swap. 11 | * @b: The second integer to swap. 12 | */ 13 | void swap_ints(int *a, int *b) 14 | { 15 | int tmp; 16 | 17 | tmp = *a; 18 | *a = *b; 19 | *b = tmp; 20 | } 21 | 22 | /** 23 | * hoare_partition - Order a subset of an array of integers 24 | * according to the hoare partition scheme. 25 | * @array: The array of integers. 26 | * @size: The size of the array. 27 | * @left: The starting index of the subset to order. 28 | * @right: The ending index of the subset to order. 29 | * 30 | * Return: The final partition index. 31 | * 32 | * Description: Uses the last element of the partition as the pivot. 33 | * Prints the array after each swap of two elements. 34 | */ 35 | int hoare_partition(int *array, size_t size, int left, int right) 36 | { 37 | int pivot, above, below; 38 | 39 | pivot = array[right]; 40 | for (above = left - 1, below = right + 1; above < below;) 41 | { 42 | do { 43 | above++; 44 | } while (array[above] < pivot); 45 | do { 46 | below--; 47 | } while (array[below] > pivot); 48 | 49 | if (above < below) 50 | { 51 | swap_ints(array + above, array + below); 52 | print_array(array, size); 53 | } 54 | } 55 | 56 | return (above); 57 | } 58 | 59 | /** 60 | * hoare_sort - Implement the quicksort algorithm through recursion. 61 | * @array: An array of integers to sort. 62 | * @size: The size of the array. 63 | * @left: The starting index of the array partition to order. 64 | * @right: The ending index of the array partition to order. 65 | * 66 | * Description: Uses the Hoare partition scheme. 67 | */ 68 | void hoare_sort(int *array, size_t size, int left, int right) 69 | { 70 | int part; 71 | 72 | if (right - left > 0) 73 | { 74 | part = hoare_partition(array, size, left, right); 75 | hoare_sort(array, size, left, part - 1); 76 | hoare_sort(array, size, part, right); 77 | } 78 | } 79 | 80 | /** 81 | * quick_sort_hoare - Sort an array of integers in ascending 82 | * order using the quicksort algorithm. 83 | * @array: An array of integers. 84 | * @size: The size of the array. 85 | * 86 | * Description: Uses the Hoare partition scheme. Prints 87 | * the array after each swap of two elements. 88 | */ 89 | void quick_sort_hoare(int *array, size_t size) 90 | { 91 | if (array == NULL || size < 2) 92 | return; 93 | 94 | hoare_sort(array, size, 0, size - 1); 95 | } 96 | -------------------------------------------------------------------------------- /2-O: -------------------------------------------------------------------------------- 1 | O(n^2) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /2-selection_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | /** 4 | * swap_ints - Swap two integers in an array. 5 | * @a: The first integer to swap. 6 | * @b: The second integer to swap. 7 | */ 8 | void swap_ints(int *a, int *b) 9 | { 10 | int tmp; 11 | 12 | tmp = *a; 13 | *a = *b; 14 | *b = tmp; 15 | } 16 | 17 | /** 18 | * selection_sort - Sort an array of integers in ascending order 19 | * using the selection sort algorithm. 20 | * @array: An array of integers. 21 | * @size: The size of the array. 22 | * 23 | * Description: Prints the array after each swap. 24 | */ 25 | void selection_sort(int *array, size_t size) 26 | { 27 | int *min; 28 | size_t n, m; 29 | 30 | if (array == NULL || size < 2) 31 | return; 32 | 33 | for (n = 0; n < size - 1; n++) 34 | { 35 | min = array + n; 36 | for (m = n + 1; m < size; m++) 37 | min = (array[m] < *min) ? (array + m) : min; 38 | 39 | if ((array + n) != min) 40 | { 41 | swap_ints(array + n, min); 42 | print_array(array, size); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /3-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /3-quick_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | void swap_ints(int *a, int *b); 4 | int lomuto_partition(int *array, size_t size, int left, int right); 5 | void lomuto_sort(int *array, size_t size, int left, int right); 6 | void quick_sort(int *array, size_t size); 7 | 8 | /** 9 | * swap_ints - Swap two integers in an array. 10 | * @a: The first integer to swap. 11 | * @b: The second integer to swap. 12 | */ 13 | void swap_ints(int *a, int *b) 14 | { 15 | int tmp; 16 | 17 | tmp = *a; 18 | *a = *b; 19 | *b = tmp; 20 | } 21 | 22 | /** 23 | * lomuto_partition - Order a subset of an array of integers according to 24 | * the lomuto partition scheme (last element as pivot). 25 | * @array: The array of integers. 26 | * @size: The size of the array. 27 | * @left: The starting index of the subset to order. 28 | * @right: The ending index of the subset to order. 29 | * 30 | * Return: The final partition index. 31 | */ 32 | int lomuto_partition(int *array, size_t size, int left, int right) 33 | { 34 | int *pivot, above, below; 35 | 36 | pivot = array + right; 37 | for (above = below = left; below < right; below++) 38 | { 39 | if (array[below] < *pivot) 40 | { 41 | if (above < below) 42 | { 43 | swap_ints(array + below, array + above); 44 | print_array(array, size); 45 | } 46 | above++; 47 | } 48 | } 49 | 50 | if (array[above] > *pivot) 51 | { 52 | swap_ints(array + above, pivot); 53 | print_array(array, size); 54 | } 55 | 56 | return (above); 57 | } 58 | 59 | /** 60 | * lomuto_sort - Implement the quicksort algorithm through recursion. 61 | * @array: An array of integers to sort. 62 | * @size: The size of the array. 63 | * @left: The starting index of the array partition to order. 64 | * @right: The ending index of the array partition to order. 65 | * 66 | * Description: Uses the Lomuto partition scheme. 67 | */ 68 | void lomuto_sort(int *array, size_t size, int left, int right) 69 | { 70 | int part; 71 | 72 | if (right - left > 0) 73 | { 74 | part = lomuto_partition(array, size, left, right); 75 | lomuto_sort(array, size, left, part - 1); 76 | lomuto_sort(array, size, part + 1, right); 77 | } 78 | } 79 | 80 | /** 81 | * quick_sort - Sort an array of integers in ascending 82 | * order using the quicksort algorithm. 83 | * @array: An array of integers. 84 | * @size: The size of the array. 85 | * 86 | * Description: Uses the Lomuto partition scheme. Prints 87 | * the array after each swap of two elements. 88 | */ 89 | void quick_sort(int *array, size_t size) 90 | { 91 | if (array == NULL || size < 2) 92 | return; 93 | 94 | lomuto_sort(array, size, 0, size - 1); 95 | } 96 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 INNOCENT CHARLES UDO 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sorting Algorithms 2 | 3 | ![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSye8PCzadyBaQgXt8wKFEeq-zCkR4FJbGDUg&usqp=CAU) 4 | 5 | ![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSHSgZ8f8M4bBGI_iVE1_xVoelosvfQxIiQdw&usqp=CAU) 6 | 7 | Resources 8 | Read or watch: 9 | 10 | * [Sorting algorithm](https://en.wikipedia.org/wiki/Sorting_algorithm) 11 | * [Big O notation](https://stackoverflow.com/questions/487258/what-is-a-plain-english-explanation-of-big-o-notation) 12 | * [Sorting algorithms animations](https://www.toptal.com/developers/sorting-algorithms) 13 | * [15 sorting algorithms in 6 minutes (WARNING: The following video can trigger seizure/epilepsy. It is not required for the project, as it is only a funny visualization of different sorting algorithms)](https://www.youtube.com/watch?v=kPRA0W1kECg) 14 | * [CS50 Algorithms explanation in detail by David Malan](https://www.youtube.com/watch?v=yb0PY3LX2x8&t=2s) 15 | * [All about sorting algorithms](https://www.geeksforgeeks.org/sorting-algorithms/) 16 | 17 | ## General 18 | 19 | - Allowed editors: `vi`, `vim`, `emacs` 20 | - All your files will be compiled on `Ubuntu 20.04` LTS using `gcc`, using the options `-Wall` `-Werror` `-Wextra` `-pedantic` `-std=gnu89` 21 | - All your files should end with a new line 22 | - A `README.md` file, at the root of the folder of the project, is mandatory 23 | - Your code should use the Betty style. It will be checked using `betty-style.pl` and `betty-doc.pl` 24 | - You are not allowed to use global variables 25 | - No more than 5 functions per file 26 | - Unless specified otherwise, you are not allowed to use the standard library. Any use of functions like `printf`, `puts`, … is totally forbidden. 27 | - The prototypes of all your functions should be included in your header file called `sort.h` 28 | _Don’t forget to push your header file_ 29 | - All your header files should be include guarded 30 | - A list/array does not need to be sorted if its size is less than 2. 31 | 32 | ## AIM :sunflower: 33 | - At least four different sorting algorithms 34 | - What is the Big O notation, and how to evaluate the time complexity of an algorithm 35 | - How to select the best sorting algorithm for a given input 36 | - What is a stable sorting algorithm 37 | 38 | ## Tests :heavy_check_mark: 39 | 40 | * [tests](./tests): Folder of test files. 41 | 42 | ## Helper Files :raised_hands: 43 | 44 | * [print_array.c](./print_array.c): C function that prints an array of integers. 45 | * [print_list.c](./print_list.c): C function that prints a `listint_t` doubly-linked list. 46 | 47 | 48 | ## Tasks :page_with_curl: 49 | 50 | * **0. Bubble sort** 51 | * [0-bubble_sort.c](./0-bubble_sort.c): C function that sorts an array of integers in ascending order using the Bubble Sort algorithm. 52 | * Prints the array after each swap. 53 | * [0-O](./0-O): Text file containing the best, average, and worst case time complexities of the Bubble Sort algorithm, one per line. 54 | 55 | * **1. Insertion sort** 56 | * [1-insertion_sort_list.c](./1-insertion_sort_list.c): C function that sorts a `listint_t` doubly-linked list of integers in ascending order using the 57 | Insertion Sort algorithm. 58 | * Prints the list after each swap. 59 | * [1-O](./1-O): Text file containing the best, average, and worst case time complexities of the Insertion Sort algorithm, one per line. 60 | 61 | * **2. Selection sort** 62 | * [2-selection_sort.c](./2-selection_sort.c): C function that sorts an array of integers in ascending order using the Selection Sort algorithm. 63 | * Prints the array after each swap. 64 | * [2-O](./2-O): Text file containing the best, average, and worst case time complexities of the Selection Sort algorithm, one per line. 65 | 66 | * **3. Quick sort** 67 | * [3-quick_sort.c](./3-quick_sort.c): C function that sorts an array of integers in ascending order using the Quick Sort algorithm. 68 | * Implements the Lomuto partition scheme. 69 | * Always uses the last element of the partition being sorted as the pivot. 70 | * Prints the array after each swap. 71 | * [3-O](./3-O): Text file containing the best, average, and worst case time complexities of the Quick Sort Lomuto Partition scheme algorithm, one per line. 72 | 73 | * **4. Shell sort - Knuth Sequence** 74 | * [100-shell_sort.c](./100-shell_sort.c): C function that sorts an array of integers in ascending order using the Shell sort algorithm. 75 | * Implements the Knuth interval sequence. 76 | * Prints the array each time the interval is decreased. 77 | 78 | * **5. Cocktail shaker sort** 79 | * [101-cocktail_sort_list.c](./101-cocktail_sort_list.c): C function that sorts 80 | a `listint_t` doubly-linked list of integers in ascending order using the Cocktail Shaker Sort algorithm. 81 | * Prints the list after each swap. 82 | * [101-O](./101-O): Text file containing the best, average, and worst case time complexities of the Cocktail Shaker Sort algorithm, one per line. 83 | 84 | * **6. Counting sort** 85 | * [102-counting_sort.c](./102-counting_sort.c): C function that sorts an array of integers in ascending order using the Counting Sort algorithm. 86 | * Assumes that the array will only contain numbers `>= 0`. 87 | * Prints the counting array after it has been initialized. 88 | * [102-O](./102-O): Text file containing the best, average, and worst case time complexities of the Counting Sort algorithm, one per line. 89 | 90 | * **7. Merge sort** 91 | * [103-merge_sort.c](./103-merge_sort.c): C function that sorts an array of integers in ascending order using the Merge Sort algorithm. 92 | * Implements the `top-down` Merge Sort algorithm. 93 | * When an array is divided, the size of the left subarray is always less than or equal to the size of the right subarray. 94 | * Always sorts the left subarray before the right one. 95 | * Prints subarrays each time they are merged. 96 | * [103-O](./103-O): Text file containing the best, average, and worst case time complexities of the Merge Sort algorithm, one per line. 97 | 98 | * **8. Heap sort** 99 | * [104-heap_sort.c](./104-heap_sort.c): C function that sorts an array of integers in ascending order using the Heap Sort algorithm. 100 | * Implements the `sift-down` Heap Sort algorithm. 101 | * Prints the array after each swap. 102 | * [104-O](./104-O): Text file containing the best, average, and worst case time complexiites of the Heap Sort algorithm, one per line. 103 | 104 | * **9. Radix sort** 105 | * [105-radix_sort.c](./105-radix_sort.c): C function that sorts an array of integers in ascending order using the Radix Sort algorithm. 106 | * Implements the Least-Significant-Digit (LSD) Radix Sort algorithm. 107 | * Assumes that the array will only contain numbers `>= 0`. 108 | * Prints the array for each significant digit increase. 109 | * [105-O](./105-O): Text file containing the best, average, and worst case time complexities of the Radix Sort algorithm, one per line. 110 | 111 | * **10. Bitonic sort** 112 | * [106-bitonic_sort.c](./106-bitonic_sort.c): C function that sorts an array of integers in ascending order using the Bitonic Sort algorithm. 113 | * Assumes that `size` is a power of 2 (ie. `size` can be expressed as `2^k` where `k >= 0`). 114 | * Prints subarrays each time they are merged. 115 | * [106-O](./106-O): Text file containing the best, average, and worst case time complexities of the Bitonic Sort algorithm, one per line. 116 | 117 | * **11. Quick Sort - Hoare Partition scheme** 118 | * [107-quick_sort_hoare.c](./107-quick_sort_hoare.c): C function that sorts an array of integers in ascending order using the Quick Sort algorithm. 119 | * Implements the Hoare partition scheme. 120 | * Always uses the last elemement of the partition being sorted as the pivot. 121 | * Prints the array after each swap. 122 | * [107-O](./107-O): Text file containing the best, average, and worst case time complexities of the Quick Sort Hoare Partition cheme algorithm, one per line. 123 | 124 | * **12. Dealer** 125 | * [1000-sort_deck.c](./1000-sort_deck.c): C function that sorts a `deck_node_t` doubly-linked list deck of cards. 126 | * Assumes that there are exactly `52` elements in the doubly-linked list. 127 | * Orders the deck from spades to diamonds and from aces to kings. 128 | 129 | ## Author 130 | © [INNOCENT CHARLES UDO](https://github.com/Innocentsax) 131 | -------------------------------------------------------------------------------- /deck.h: -------------------------------------------------------------------------------- 1 | #ifndef DECK_H 2 | #define DECK_H 3 | 4 | #include 5 | 6 | /** 7 | * enum kind_e - Enumeration of card suits. 8 | * @SPADE: Spades suit. 9 | * @HEART: Hearts suit. 10 | * @CLUB: Clubs suit. 11 | * @DIAMOND: Diamonds suit. 12 | */ 13 | typedef enum kind_e 14 | { 15 | SPADE = 0, 16 | HEART, 17 | CLUB, 18 | DIAMOND 19 | } kind_t; 20 | 21 | /** 22 | * struct card_s - Playing card 23 | * 24 | * @value: Value of the card 25 | * From "Ace" to "King" 26 | * @kind: Kind of the card 27 | */ 28 | typedef struct card_s 29 | { 30 | const char *value; 31 | const kind_t kind; 32 | } card_t; 33 | 34 | /** 35 | * struct deck_node_s - Deck of card 36 | * 37 | * @card: Pointer to the card of the node 38 | * @prev: Pointer to the previous node of the list 39 | * @next: Pointer to the next node of the list 40 | */ 41 | typedef struct deck_node_s 42 | { 43 | const card_t *card; 44 | struct deck_node_s *prev; 45 | struct deck_node_s *next; 46 | } deck_node_t; 47 | 48 | void sort_deck(deck_node_t **deck); 49 | 50 | #endif /* DECK_H */ 51 | -------------------------------------------------------------------------------- /print_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * print_array - Prints an array of integers 6 | * 7 | * @array: The array to be printed 8 | * @size: Number of elements in @array 9 | */ 10 | void print_array(const int *array, size_t size) 11 | { 12 | size_t w; 13 | 14 | w = 0; 15 | while (array && w < size) 16 | { 17 | if (w > 0) 18 | printf(", "); 19 | printf("%d", array[w]); 20 | ++w; 21 | } 22 | printf("\n"); 23 | } 24 | -------------------------------------------------------------------------------- /print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "sort.h" 3 | 4 | /** 5 | * print_list - Prints a list of integers 6 | * 7 | * @list: The list to be printed 8 | */ 9 | void print_list(const listint_t *list) 10 | { 11 | int h; 12 | 13 | h = 0; 14 | while (list) 15 | { 16 | if (h > 0) 17 | printf(", "); 18 | printf("%d", list->n); 19 | ++h; 20 | list = list->next; 21 | } 22 | printf("\n"); 23 | } 24 | -------------------------------------------------------------------------------- /sort.h: -------------------------------------------------------------------------------- 1 | #ifndef SORT_H 2 | #define SORT_H 3 | 4 | #include 5 | #include 6 | 7 | /* Comparison direction macros for bitonic sort */ 8 | #define UP 0 9 | #define DOWN 1 10 | 11 | /** 12 | * enum bool - Enumeration of Boolean values. 13 | * @false: Equals 0. 14 | * @true: Equals 1. 15 | */ 16 | typedef enum bool 17 | { 18 | false = 0, 19 | true 20 | } bool; 21 | 22 | /** 23 | * struct listint_s - Doubly linked list node 24 | * 25 | * @n: Integer stored in the node 26 | * @prev: Pointer to the previous element of the list 27 | * @next: Pointer to the next element of the list 28 | */ 29 | typedef struct listint_s 30 | { 31 | const int n; 32 | struct listint_s *prev; 33 | struct listint_s *next; 34 | } listint_t; 35 | 36 | /* Printing helper functions */ 37 | void print_array(const int *array, size_t size); 38 | void print_list(const listint_t *list); 39 | 40 | /* Sorting algoritms */ 41 | void bubble_sort(int *array, size_t size); 42 | void insertion_sort_list(listint_t **list); 43 | void selection_sort(int *array, size_t size); 44 | void quick_sort(int *array, size_t size); 45 | void shell_sort(int *array, size_t size); 46 | void cocktail_sort_list(listint_t **list); 47 | void counting_sort(int *array, size_t size); 48 | void merge_sort(int *array, size_t size); 49 | void heap_sort(int *array, size_t size); 50 | void radix_sort(int *array, size_t size); 51 | void bitonic_sort(int *array, size_t size); 52 | void quick_sort_hoare(int *array, size_t size); 53 | 54 | #endif /* SORT_H */ 55 | --------------------------------------------------------------------------------