Skip to content

Commit 9d29e4e

Browse files
committed
sorting_algorithms
1 parent 25c9fe5 commit 9d29e4e

File tree

4 files changed

+223
-0
lines changed

4 files changed

+223
-0
lines changed

1000-sort_deck.c

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include "deck.h"
2+
3+
/**
4+
* *_strcmp - compare two strings
5+
*@s1: pointer 1
6+
*@s2: pointer 2
7+
*Return: An integer + - or 0
8+
*/
9+
10+
int _strcmp(const char *s1, const char *s2)
11+
{
12+
int i = 0;
13+
14+
while (*(s1 + i) && *(s2 + i))
15+
{
16+
if (s1[i] != s2[i])
17+
break;
18+
i++;
19+
}
20+
return (s1[i] - s2[i]);
21+
}
22+
23+
/**
24+
* get_card_value - Getting the int card value to work with
25+
* @card: Actual card to get value
26+
* Return: Card's Value
27+
*
28+
*/
29+
30+
int get_card_value(deck_node_t *card)
31+
{
32+
int value = atoi(card->card->value);
33+
34+
if (value != 0)
35+
return (value);
36+
if (_strcmp(card->card->value, "Jack") == 0)
37+
return (11);
38+
if (_strcmp(card->card->value, "Queen") == 0)
39+
return (12);
40+
if (_strcmp(card->card->value, "King") == 0)
41+
return (13);
42+
return (0);
43+
}
44+
45+
/**
46+
* swap_nodes - Swap nodes in a double linked list
47+
* @prev: pointer to prev node
48+
* @actual: pointer o actual node
49+
* @head: to the head of the deck
50+
* Return: Nothing
51+
*/
52+
53+
void swap_nodes(deck_node_t **prev, deck_node_t *actual,
54+
deck_node_t **head)
55+
{
56+
(*prev)->next = actual->next;
57+
if (actual->next != NULL)
58+
actual->next->prev = *prev;
59+
actual->prev = (*prev)->prev;
60+
actual->next = *prev;
61+
if ((*prev)->prev != NULL)
62+
(*prev)->prev->next = actual;
63+
else
64+
*head = actual; /* prev node is NULL*/
65+
(*prev)->prev = actual;
66+
*prev = actual->prev;
67+
}
68+
69+
/**
70+
* insertion_sort_list - sorts a deck of cards
71+
* in ascending order using the Insertion sort algorithm
72+
* @list: list to look into
73+
* @option: sort by value if 0, otherwise sort by kind
74+
* Return: Nothing
75+
*/
76+
77+
void insertion_sort_list(deck_node_t **list, int option)
78+
{
79+
deck_node_t *actual, *prev;
80+
81+
if (list == NULL || *list == NULL)
82+
return;
83+
for (actual = (*list)->next; actual != NULL; actual = actual->next)
84+
{
85+
prev = actual->prev;
86+
if (!option)
87+
while (actual->prev != NULL && get_card_value(actual) <
88+
get_card_value(actual->prev))
89+
swap_nodes(&prev, actual, list);
90+
else
91+
while (actual->prev != NULL && actual->card->kind <
92+
actual->prev->card->kind)
93+
swap_nodes(&prev, actual, list);
94+
}
95+
}
96+
97+
/**
98+
* sort_deck - sorts a deck of cards.
99+
* @deck: Deck to sort
100+
* Return: Nothing
101+
*
102+
*/
103+
void sort_deck(deck_node_t **deck)
104+
{
105+
if (deck == NULL || *deck == NULL)
106+
return;
107+
insertion_sort_list(deck, 0);
108+
insertion_sort_list(deck, 1);
109+
}

deck.h

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#ifndef _DECK_H
2+
#define _DECK_H
3+
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
#include <string.h>
7+
8+
#define TOTAL 52
9+
10+
/**
11+
* enum kind_e - Enumeration
12+
* @SPADE: SPADE value
13+
* @HEART: HEART value
14+
* @CLUB: CLUB value
15+
* @DIAMOND: DIAMOND value
16+
*/
17+
typedef enum kind_e
18+
{
19+
SPADE = 0,
20+
HEART,
21+
CLUB,
22+
DIAMOND
23+
} kind_t;
24+
25+
/**
26+
* struct card_s - Playing card
27+
*
28+
* @value: Value of the card
29+
* From "Ace" to "King"
30+
* @kind: Kind of the card
31+
*/
32+
typedef struct card_s
33+
{
34+
const char *value;
35+
const kind_t kind;
36+
} card_t;
37+
38+
/**
39+
* struct deck_node_s - Deck of card
40+
*
41+
* @card: Pointer to the card of the node
42+
* @prev: Pointer to the previous node of the list
43+
* @next: Pointer to the next node of the list
44+
*/
45+
typedef struct deck_node_s
46+
{
47+
const card_t *card;
48+
struct deck_node_s *prev;
49+
struct deck_node_s *next;
50+
} deck_node_t;
51+
52+
void sort_deck(deck_node_t **deck);
53+
54+
#endif /*_DECK_H*/

main/1000-main.c

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include "deck.h"
4+
5+
void print_deck(const deck_node_t *deck)
6+
{
7+
size_t i;
8+
char kinds[4] = {'S', 'H', 'C', 'D'};
9+
10+
i = 0;
11+
while (deck)
12+
{
13+
if (i)
14+
printf(", ");
15+
printf("{%s, %c}", deck->card->value, kinds[deck->card->kind]);
16+
if (i == 12)
17+
printf("\n");
18+
i = (i + 1) % 13;
19+
deck = deck->next;
20+
}
21+
}
22+
23+
deck_node_t *init_deck(const card_t cards[52])
24+
{
25+
deck_node_t *deck;
26+
deck_node_t *node;
27+
size_t i;
28+
29+
i = 52;
30+
deck = NULL;
31+
while (i--)
32+
{
33+
node = malloc(sizeof(*node));
34+
if (!node)
35+
return (NULL);
36+
node->card = &cards[i];
37+
node->next = deck;
38+
node->prev = NULL;
39+
if (deck)
40+
deck->prev = node;
41+
deck = node;
42+
}
43+
return (deck);
44+
}
45+
46+
int main(void)
47+
{
48+
card_t cards[52] = {
49+
{"Jack", CLUB}, {"4", HEART}, {"3", HEART}, {"3", DIAMOND}, {"Queen", HEART}, {"5", HEART}, {"5", SPADE}, {"10", HEART}, {"6", HEART}, {"5", DIAMOND}, {"6", SPADE}, {"9", HEART}, {"7", DIAMOND}, {"Jack", SPADE}, {"Ace", DIAMOND}, {"9", CLUB}, {"Jack", DIAMOND}, {"7", SPADE}, {"King", DIAMOND}, {"10", CLUB}, {"King", SPADE}, {"8", CLUB}, {"9", SPADE}, {"6", CLUB}, {"Ace", CLUB}, {"3", SPADE}, {"8", SPADE}, {"9", DIAMOND}, {"2", HEART}, {"4", DIAMOND}, {"6", DIAMOND}, {"3", CLUB}, {"Queen", CLUB}, {"10", SPADE}, {"8", DIAMOND}, {"8", HEART}, {"Ace", SPADE}, {"Jack", HEART}, {"2", CLUB}, {"4", SPADE}, {"2", SPADE}, {"2", DIAMOND}, {"King", CLUB}, {"Queen", SPADE}, {"Queen", DIAMOND}, {"7", CLUB}, {"7", HEART}, {"5", CLUB}, {"10", DIAMOND}, {"4", CLUB}, {"King", HEART}, {"Ace", HEART},
50+
};
51+
deck_node_t *deck;
52+
53+
deck = init_deck(cards);
54+
print_deck(deck);
55+
printf("\n");
56+
sort_deck(&deck);
57+
printf("\n");
58+
print_deck(deck);
59+
return (0);
60+
}

output/deck

17.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)