Skip to content

Commit 4b0707f

Browse files
committed
Insertion sort
1 parent 63c99b2 commit 4b0707f

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

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

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "sort.h"
2+
3+
/**
4+
* swap_nodes - Swap two nodes in a listint_t doubly-linked list.
5+
* @h: A pointer to the head of the doubly-linked list.
6+
* @n1: A pointer to the first node to swap.
7+
* @n2: The second node to swap.
8+
*/
9+
void swap_nodes(listint_t **h, listint_t **n1, listint_t *n2)
10+
{
11+
(*n1)->next = n2->next;
12+
if (n2->next != NULL)
13+
n2->next->prev = *n1;
14+
n2->prev = (*n1)->prev;
15+
n2->next = *n1;
16+
if ((*n1)->prev != NULL)
17+
(*n1)->prev->next = n2;
18+
else
19+
*h = n2;
20+
(*n1)->prev = n2;
21+
*n1 = n2->prev;
22+
}
23+
24+
/**
25+
* insertion_sort_list - Sorts a doubly linked list of integers
26+
* using the insertion sort algorithm.
27+
* @list: A pointer to the head of a doubly-linked list of integers.
28+
*
29+
* Description: Prints the list after each swap.
30+
*/
31+
void insertion_sort_list(listint_t **list)
32+
{
33+
listint_t *iter, *insert, *tmp;
34+
35+
if (list == NULL || *list == NULL || (*list)->next == NULL)
36+
return;
37+
38+
for (iter = (*list)->next; iter != NULL; iter = tmp)
39+
{
40+
tmp = iter->next;
41+
insert = iter->prev;
42+
while (insert != NULL && iter->n < insert->n)
43+
{
44+
swap_nodes(list, &insert, iter);
45+
print_list((const listint_t *)*list);
46+
}
47+
}
48+
}

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

insertion

16.7 KB
Binary file not shown.

sort.h

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void print_list(const listint_t *list);
3535

3636
/*sorting algorithms */
3737
void bubble_sort(int *array, size_t size);
38+
void insertion_sort_list(listint_t **list);
3839
void merge_sort(int *array, size_t size);
3940
void heap_sort(int *array, size_t size);
4041
void radix_sort(int *array, size_t size);

0 commit comments

Comments
 (0)