Skip to content

Commit 5034c37

Browse files
committed
add files
1 parent 7e9fdb8 commit 5034c37

9 files changed

+271
-0
lines changed

0-bubble_sort.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "sort.h"
2+
3+
/**
4+
* bubble_sort - sorts array in ascending order.
5+
* @array: array of integers to be sorted.
6+
* @size: The size of a given array.
7+
*/
8+
9+
void bubble_sort(int *array, size_t size)
10+
{
11+
size_t i, j, tmp;
12+
13+
for (i = 0 ; i < size ; i++)
14+
{
15+
for (j = 0 ; j < size - i - 1 ; j++)
16+
{
17+
if (array[j] > array[j + 1])
18+
{
19+
tmp = array[j];
20+
array[j] = array[j + 1];
21+
array[j + 1] = tmp;
22+
print_array(array, size);
23+
}
24+
}
25+
}
26+
}

0-main.c

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

1-insertion_sort_list.c

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "sort.h"
2+
3+
/**
4+
* insertion_sort_list - sorts a doubly linked list of integers in ascending order.
5+
* @list: the list to be sorted.
6+
*/
7+
8+
void insertion_sort_list(listint_t **list)
9+
{
10+
listint_t *current, *tmp;
11+
12+
if (list == NULL || *list == NULL)
13+
return;
14+
15+
current = (*list)->next;
16+
while (current != NULL)
17+
{
18+
tmp = current;
19+
current = current->next;
20+
21+
while (tmp && tmp->prev && tmp->prev->n > tmp->n)
22+
{
23+
tmp->prev->next = tmp->next;
24+
if (tmp->next != NULL)
25+
tmp->next->prev = tmp->prev;
26+
27+
tmp->next = tmp->prev;
28+
tmp->prev = tmp->prev->prev;
29+
tmp->next->prev = tmp;
30+
31+
if (tmp->prev != NULL)
32+
tmp->prev->next = tmp;
33+
else
34+
*list = tmp;
35+
print_list(*list);
36+
}
37+
}
38+
}

1-main.c

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

2-main.c

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

2-selection_sort.c

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "sort.h"
2+
3+
/**
4+
* selection_sort - sorts an array of integers in ascending order.
5+
* @array: the array to be sorted.
6+
* @size: the size of the array.
7+
*/
8+
9+
void selection_sort(int *array, size_t size)
10+
{
11+
size_t i, j, ind_min, tmp;
12+
13+
if (!array || size == 1)
14+
return;
15+
16+
for (i = 0 ; i < size - 1 ; i++)
17+
{
18+
ind_min = i;
19+
for (j = i + 1 ; j < size ; j++)
20+
{
21+
if (array[ind_min] > array[j])
22+
{
23+
ind_min = j;
24+
continue;
25+
}
26+
}
27+
if (ind_min != i)
28+
{
29+
tmp = array[i];
30+
array[i] = array[ind_min];
31+
array[ind_min] = tmp;
32+
print_array(array, size);
33+
}
34+
}
35+
}

print_array.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
4+
/**
5+
* print_array - Prints an array of integers
6+
*
7+
* @array: The array to be printed
8+
* @size: Number of elements in @array
9+
*/
10+
void print_array(const int *array, size_t size)
11+
{
12+
size_t i;
13+
14+
i = 0;
15+
while (array && i < size)
16+
{
17+
if (i > 0)
18+
printf(", ");
19+
printf("%d", array[i]);
20+
++i;
21+
}
22+
printf("\n");
23+
}

print_list.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
#include "sort.h"
3+
4+
/**
5+
* print_list - Prints a list of integers
6+
*
7+
* @list: The list to be printed
8+
*/
9+
void print_list(const listint_t *list)
10+
{
11+
int i;
12+
13+
i = 0;
14+
while (list)
15+
{
16+
if (i > 0)
17+
printf(", ");
18+
printf("%d", list->n);
19+
++i;
20+
list = list->next;
21+
}
22+
printf("\n");
23+
}

sort.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef SORT_H
2+
#define SORT_H
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
7+
8+
/**
9+
* struct listint_s - Doubly linked list node
10+
*
11+
* @n: Integer stored in the node
12+
* @prev: Pointer to the previous element of the list
13+
* @next: Pointer to the next element of the list
14+
*/
15+
typedef struct listint_s
16+
{
17+
const int n;
18+
struct listint_s *prev;
19+
struct listint_s *next;
20+
} listint_t;
21+
22+
void print_array(const int *array, size_t size);
23+
void print_list(const listint_t *list);
24+
void bubble_sort(int *array, size_t size);
25+
void insertion_sort_list(listint_t **list);
26+
void selection_sort(int *array, size_t size);
27+
28+
#endif

0 commit comments

Comments
 (0)