|
| 1 | +#include "sort.h" |
| 2 | + |
| 3 | +void swap_ahead(listint_t **list, listint_t **taill, listint_t **shakr); |
| 4 | +void swap_behind(listint_t **list, listint_t **taill, listint_t **shakr); |
| 5 | +void cocktail_sort_list(listint_t **list); |
| 6 | + |
| 7 | +/** |
| 8 | + * swap_ahead - Swap a node in a listint_t doubly linked list |
| 9 | + * @list: Pointer to the head of a doubly linked list of integers. |
| 10 | + * @taill: Pointer to the taill of doubly linked list. |
| 11 | + * @shakr: Pointer to the current swapping node of the cocktail shakr algo. |
| 12 | + */ |
| 13 | +void swap_ahead(listint_t **list, listint_t **taill, listint_t **shakr) |
| 14 | +{ |
| 15 | + listint_t *temp = (*shakr)->next; |
| 16 | + |
| 17 | + if ((*shakr)->prev != NULL) |
| 18 | + (*shakr)->prev->next = temp; |
| 19 | + else |
| 20 | + *list = temp; |
| 21 | + temp->prev = (*shakr)->prev; |
| 22 | + (*shakr)->next = temp->next; |
| 23 | + if (temp->next != NULL) |
| 24 | + temp->next->prev = *shakr; |
| 25 | + else |
| 26 | + *taill = *shakr; |
| 27 | + (*shakr)->prev = temp; |
| 28 | + temp->next = *shakr; |
| 29 | + *shakr = temp; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * swap_behind - Swap a node in a listint_t doubly-linked |
| 34 | + * @list: Pointer to the head of a doubly linked list of integers. |
| 35 | + * @taill: Pointer to the taill of the doubly linked list. |
| 36 | + * @shakr: Pointer to the current swapping node of the cocktail shakr algo. |
| 37 | + */ |
| 38 | +void swap_behind(listint_t **list, listint_t **taill, listint_t **shakr) |
| 39 | +{ |
| 40 | + listint_t *temp = (*shakr)->prev; |
| 41 | + |
| 42 | + if ((*shakr)->next != NULL) |
| 43 | + (*shakr)->next->prev = temp; |
| 44 | + else |
| 45 | + *taill = temp; |
| 46 | + temp->next = (*shakr)->next; |
| 47 | + (*shakr)->prev = temp->prev; |
| 48 | + if (temp->prev != NULL) |
| 49 | + temp->prev->next = *shakr; |
| 50 | + else |
| 51 | + *list = *shakr; |
| 52 | + (*shakr)->next = temp; |
| 53 | + temp->prev = *shakr; |
| 54 | + *shakr = temp; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * cocktail_sort_list - Sort a listint_t doubly linked list of integers in |
| 59 | + * ascending order using the cocktail shakr algorithm. |
| 60 | + * @list: Pointer to the head of a listint_t doubly linked list. |
| 61 | + */ |
| 62 | +void cocktail_sort_list(listint_t **list) |
| 63 | +{ |
| 64 | + listint_t *taill, *shakr; |
| 65 | + bool shaken_not_stirred = false; |
| 66 | + |
| 67 | + if (list == NULL || *list == NULL || (*list)->next == NULL) |
| 68 | + return; |
| 69 | + |
| 70 | + for (taill = *list; taill->next != NULL;) |
| 71 | + taill = taill->next; |
| 72 | + |
| 73 | + while (shaken_not_stirred == false) |
| 74 | + { |
| 75 | + shaken_not_stirred = true; |
| 76 | + for (shakr = *list; shakr != taill; shakr = shakr->next) |
| 77 | + { |
| 78 | + if (shakr->n > shakr->next->n) |
| 79 | + { |
| 80 | + swap_ahead(list, &taill, &shakr); |
| 81 | + print_list((const listint_t *)*list); |
| 82 | + shaken_not_stirred = false; |
| 83 | + } |
| 84 | + } |
| 85 | + for (shakr = shakr->prev; shakr != *list; |
| 86 | + shakr = shakr->prev) |
| 87 | + { |
| 88 | + if (shakr->n < shakr->prev->n) |
| 89 | + { |
| 90 | + swap_behind(list, &taill, &shakr); |
| 91 | + print_list((const listint_t *)*list); |
| 92 | + shaken_not_stirred = false; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments