Skip to content

Commit 1b21b4f

Browse files
committed
function that sorts a doubly linked list of integers in ascen…
…ding order using the Cocktail shaker sort algorithm
1 parent 57d98ba commit 1b21b4f

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

101-cocktail_sort_list.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}

0 commit comments

Comments
 (0)