Skip to content

Commit dab981e

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

12 files changed

+344
-0
lines changed

0-O

+3
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

+36
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

+19
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

+3
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

+69
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

+54
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

+3
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

+19
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

+37
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

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
O(nlog(n))
2+
O(nlog(n))
3+
O(n^2)

3-main.c

+19
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+
quick_sort(array, n);
16+
printf("\n");
17+
print_array(array, n);
18+
return (0);
19+
}

3-quick_sort.c

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include "sort.h"
2+
3+
int lomutoPartition(int *array, int low, int high, size_t size);
4+
void lomutoSort(int *array, int start, int end, size_t size);
5+
6+
/**
7+
* quick_sort - Sorts an array of integers in ascending order,
8+
* using the Quick sort algorithm.
9+
*
10+
* @array: The array to be arranged.
11+
* @size: Size of the array.
12+
*/
13+
void quick_sort(int *array, size_t size)
14+
{
15+
if (size < 2)
16+
return;
17+
lomutoSort(array, 0, size - 1, size);
18+
}
19+
20+
/**
21+
* lomutoPartition - array partition
22+
*
23+
* @array: array to sort
24+
* @low: first position
25+
* @high: last position
26+
* @size: array size
27+
*
28+
* Return: int pivot index
29+
*/
30+
int lomutoPartition(int *array, int low, int high, size_t size)
31+
{
32+
int pivot, index, forIndex, temp;
33+
34+
pivot = array[high];
35+
index = low - 1;
36+
for (forIndex = low; forIndex < high; forIndex++)
37+
{
38+
if (pivot >= array[forIndex])
39+
{
40+
index += 1;
41+
if (index < forIndex)
42+
{
43+
temp = array[index];
44+
array[index] = array[forIndex];
45+
array[forIndex] = temp;
46+
print_array(array, size);
47+
}
48+
}
49+
}
50+
51+
if (array[index + 1] > pivot)
52+
{
53+
temp = array[index + 1];
54+
array[index + 1] = array[high];
55+
array[high] = temp;
56+
print_array(array, size);
57+
}
58+
return (index + 1);
59+
}
60+
61+
/**
62+
* lomutoSort - sorts an array of integers recursively
63+
*
64+
* @array: array to sort
65+
* @start: first position
66+
* @end: last position
67+
* @size: array size
68+
*/
69+
void lomutoSort(int *array, int start, int end, size_t size)
70+
{
71+
int partition_index;
72+
73+
if (start < end)
74+
{
75+
partition_index = lomutoPartition(array, start, end, size);
76+
lomutoSort(array, start, partition_index - 1, size);
77+
lomutoSort(array, partition_index + 1, end, size);
78+
}
79+
}

0 commit comments

Comments
 (0)