Skip to content

Commit 107cb37

Browse files
committed
adding neccessarry files
1 parent beccef7 commit 107cb37

33 files changed

+280
-1027
lines changed

0-O

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

0-bubble_sort.c

+16-32
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,30 @@
11
#include "sort.h"
2-
3-
/**
4-
* swap_ints - Swap two integers in an array.
5-
* @a: The first integer to swap.
6-
* @b: The second integer to swap.
7-
*/
8-
void swap_ints(int *a, int *b)
9-
{
10-
int tmp;
11-
12-
tmp = *a;
13-
*a = *b;
14-
*b = tmp;
15-
}
16-
172
/**
18-
* bubble_sort - Sort an array of integers in ascending order.
19-
* @array: An array of integers to sort.
20-
* @size: The size of the array.
21-
*
22-
* Description: Prints the array after each swap.
3+
* bubble_sort - sorts array using bubble sort algorithm
4+
* @array: the array to be sorted
5+
* @size: size of array
6+
* Return: void
237
*/
248
void bubble_sort(int *array, size_t size)
259
{
26-
size_t i, len = size;
27-
bool bubbly = false;
28-
29-
if (array == NULL || size < 2)
30-
return;
10+
size_t i, k/*, hld = size*/;
11+
int temp;
3112

32-
while (bubbly == false)
13+
if (array)
3314
{
34-
bubbly = true;
35-
for (i = 0; i < len - 1; i++)
15+
for (i = 0; i < size; i++)
16+
{
17+
for (k = 0; k < size - 1; k++)
3618
{
37-
if (array[i] > array[i + 1])
19+
if (array[k] > array[k + 1])
3820
{
39-
swap_ints(array + i, array + i + 1);
21+
temp = array[k];
22+
array[k] = array[k + 1];
23+
array[k + 1] = temp;
4024
print_array(array, size);
41-
bubbly = false;
4225
}
4326
}
44-
len--;
27+
/*hld--;*/
28+
}
4529
}
4630
}

1-O

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

1-insertion_sort_list.c

+27-41
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,38 @@
11
#include "sort.h"
2-
32
/**
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-
17-
if ((*n1)->prev != NULL)
18-
(*n1)->prev->next = n2;
19-
20-
else
21-
*h = n2;
22-
23-
(*n1)->prev = n2;
24-
*n1 = n2->prev;
25-
}
26-
27-
/**
28-
* insertion_sort_list - Sorts a doubly linked list of integers
29-
* using the insertion sort algorithm.
30-
* @list: A pointer to the head of a doubly-linked list of integers.
31-
*
32-
* Description: Prints the list after each swap.
3+
* insertion_sort_list - sorts doubly linked list using insertion sort
4+
* @list: doubly linked list
5+
* Return: void
336
*/
347
void insertion_sort_list(listint_t **list)
358
{
36-
listint_t *iter, *insert, *tmp;
37-
38-
if (list == NULL || *list == NULL || (*list)->next == NULL)
39-
return;
9+
listint_t *ptr, *precede, *current, *nexus, *hold;
4010

41-
for (iter = (*list)->next; iter != NULL; iter = tmp)
11+
if (list)
4212
{
43-
tmp = iter->next;
44-
insert = iter->prev;
13+
ptr = *list;
14+
while (ptr)
15+
{
16+
current = ptr;
17+
ptr = ptr->next;
4518

46-
while (insert != NULL && iter->n < insert->n)
19+
while (current->prev && current->n < current->prev->n)
4720
{
48-
swap_nodes(list, &insert, iter);
49-
print_list((const listint_t *)*list);
21+
hold = current->prev;
22+
nexus = current->next;
23+
precede = hold->prev;
24+
current->prev = precede;
25+
if (precede)
26+
precede->next = current;
27+
else
28+
*list = current;
29+
current->next = hold;
30+
hold->next = nexus;
31+
hold->prev = current;
32+
if (nexus)
33+
nexus->prev = hold;
34+
print_list(*list);
5035
}
5136
}
52-
}
37+
}
38+
}

100-shell_sort.c

-50
This file was deleted.

1000-sort_deck.c

-114
This file was deleted.

101-O

-3
This file was deleted.

0 commit comments

Comments
 (0)