Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dab981e

Browse files
Add mandatory files solution
1 parent 79d1ac5 commit dab981e

File tree

12 files changed

+344
-0
lines changed

12 files changed

+344
-0
lines changed

‎0-O

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
O(n)
2+
O(n^2)
3+
O(n^2)

‎0-bubble_sort.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "sort.h"
2+
3+
/**
4+
* bubble_sort - Sorts an array of integers in ascending order using,
5+
* the Bubble sort algorithm.
6+
*
7+
* @array: The array to be sorted.
8+
* @size: Size of the array.
9+
*/
10+
void bubble_sort(int *array, size_t size)
11+
{
12+
size_t upperFor, innerFor, sorted;
13+
int holder;
14+
15+
if (size < 2)
16+
return;
17+
18+
for (upperFor = 0; upperFor < size - 1; upperFor++)
19+
{
20+
sorted = 1;
21+
for (innerFor = 0; innerFor < size - upperFor - 1; innerFor++)
22+
{
23+
if (array[innerFor] > array[innerFor + 1])
24+
{
25+
sorted = 0;
26+
holder = array[innerFor];
27+
array[innerFor] = array[innerFor + 1];
28+
array[innerFor + 1] = holder;
29+
print_array(array, size);
30+
}
31+
32+
}
33+
if (sorted == 1)
34+
break;
35+
}
36+
}

‎0-main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "sort.h"
2+
3+
/**
4+
* main - Entry point
5+
*
6+
* Return: Always 0
7+
*/
8+
int main(void)
9+
{
10+
int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};
11+
size_t n = sizeof(array) / sizeof(array[0]);
12+
13+
print_array(array, n);
14+
printf("\n");
15+
bubble_sort(array, n);
16+
printf("\n");
17+
print_array(array, n);
18+
return (0);
19+
}

‎1-O

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
O(n)
2+
O(n^2)
3+
O(n^2)

‎1-insertion_sort_list.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "sort.h"
2+
3+
void swapNodes(listint_t **Fnode, listint_t **Snode);
4+
5+
/**
6+
* insertion_sort_list - Sorts a doubly linked list of integers in
7+
* ascending order using the insertion sort algorithm.
8+
*
9+
* @list: The doubly linked list.
10+
*/
11+
void insertion_sort_list(listint_t **list)
12+
{
13+
listint_t *holder = *list;
14+
15+
if ((*list)->next == NULL)
16+
return;
17+
18+
while (holder->next != NULL)
19+
{
20+
if ((*list)->n > (*list)->next->n)
21+
{
22+
(*list)->prev = (*list)->next;
23+
holder = (*list)->next;
24+
if (holder->next != NULL)
25+
holder->next->prev = *list;
26+
(*list)->next = holder->next;
27+
holder->prev = NULL;
28+
holder->next = *list;
29+
*list = holder;
30+
print_list(*list);
31+
}
32+
33+
holder = (*list)->next;
34+
while (holder->next != NULL)
35+
{
36+
if (holder->n > holder->next->n)
37+
{
38+
/* swap */
39+
swapNodes(&holder, &holder->next);
40+
print_list(*list);
41+
holder = *list;
42+
break;
43+
}
44+
holder = holder->next;
45+
}
46+
}
47+
}
48+
49+
/**
50+
* swapNodes - Swap two lintint_t type Nodes.
51+
*
52+
* @F_node: First node.
53+
* @S_node: Second node.
54+
*/
55+
void swapNodes(listint_t **F_node, listint_t **S_node)
56+
{
57+
/* Swap two nodes */
58+
listint_t *Fnode = *F_node, *Snode = *S_node;
59+
60+
(Fnode->prev)->next = Snode;
61+
if (Snode->next != NULL)
62+
(Snode->next)->prev = Fnode;
63+
64+
Fnode->next = Snode->next;
65+
Snode->prev = Fnode->prev;
66+
67+
Snode->next = Fnode;
68+
Fnode->prev = Snode;
69+
}

‎1-main.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "sort.h"
2+
3+
/**
4+
* create_listint - Creates a doubly linked list from an array of integers
5+
*
6+
* @array: Array to convert to a doubly linked list
7+
* @size: Size of the array
8+
*
9+
* Return: Pointer to the first element of the created list. NULL on failure
10+
*/
11+
listint_t *create_listint(const int *array, size_t size)
12+
{
13+
listint_t *list;
14+
listint_t *node;
15+
int *tmp;
16+
17+
list = NULL;
18+
while (size--)
19+
{
20+
node = malloc(sizeof(*node));
21+
if (!node)
22+
return (NULL);
23+
tmp = (int *)&node->n;
24+
*tmp = array[size];
25+
node->next = list;
26+
node->prev = NULL;
27+
list = node;
28+
if (list->next)
29+
list->next->prev = list;
30+
}
31+
return (list);
32+
}
33+
34+
/**
35+
* main - Entry point
36+
*
37+
* Return: Always 0
38+
*/
39+
int main(void)
40+
{
41+
listint_t *list;
42+
int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};
43+
size_t n = sizeof(array) / sizeof(array[0]);
44+
45+
list = create_listint(array, n);
46+
if (!list)
47+
return (1);
48+
print_list(list);
49+
printf("\n");
50+
insertion_sort_list(&list);
51+
printf("\n");
52+
print_list(list);
53+
return (0);
54+
}

‎2-O

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
O(n^2)
2+
O(n^2)
3+
O(n^2)

‎2-main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "sort.h"
2+
3+
/**
4+
* main - Entry point
5+
*
6+
* Return: Always 0
7+
*/
8+
int main(void)
9+
{
10+
int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};
11+
size_t n = sizeof(array) / sizeof(array[0]);
12+
13+
print_array(array, n);
14+
printf("\n");
15+
selection_sort(array, n);
16+
printf("\n");
17+
print_array(array, n);
18+
return (0);
19+
}

‎2-selection_sort.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "sort.h"
2+
3+
/**
4+
* selection_sort - Sorts an array of integers in ascending order,
5+
* using Selection sort algorithm.
6+
*
7+
* @array: The array to be sorted.
8+
* @size: Size of the array.
9+
*/
10+
void selection_sort(int *array, size_t size)
11+
{
12+
size_t upperFor, innerFor, lowerInt_index;
13+
int lowerInt;
14+
15+
if (size < 2)
16+
return;
17+
18+
for (upperFor = 0; upperFor < size - 2; upperFor++)
19+
{
20+
lowerInt = array[upperFor];
21+
lowerInt_index = upperFor;
22+
for (innerFor = upperFor + 1; innerFor < size; innerFor++)
23+
{
24+
if (lowerInt > array[innerFor])
25+
{
26+
lowerInt = array[innerFor];
27+
lowerInt_index = innerFor;
28+
}
29+
}
30+
if (array[upperFor] != lowerInt)
31+
{
32+
array[lowerInt_index] = array[upperFor];
33+
array[upperFor] = lowerInt;
34+
print_array(array, size);
35+
}
36+
}
37+
}

‎3-O

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
O(nlog(n))
2+
O(nlog(n))
3+
O(n^2)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /