diff --git a/0-bubble_sort.c b/0-bubble_sort.c new file mode 100644 index 0000000..f6adaf2 --- /dev/null +++ b/0-bubble_sort.c @@ -0,0 +1,30 @@ +#include "sort.h" + +/** + * bubble_sort - function that sorts an array of integers in ascending order + * @array: the arrey to be sorted + * @size: the size of the array + * + * + */ +void bubble_sort(int *array, size_t size) +{ + size_t x, y; + int num; + + if (!array || size < 2) + return; + for (y = 0; y < size - 1; y++) + { + for (x = 0; x < size - y - 1; x++) + { + if (array[x] > array[x + 1]) + { + num = array[x]; + array[x] = array[x + 1]; + array[x + 1] = num; + print_array(array, size); + } + } + } +} diff --git a/1-main.c b/1-main.c deleted file mode 100644 index 31b7409..0000000 --- a/1-main.c +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include -#include "sort.h" - -/** - * create_listint - Creates a doubly linked list from an array of integers - * - * @array: Array to convert to a doubly linked list - * @size: Size of the array - * - * Return: Pointer to the first element of the created list. NULL on failure - */ -listint_t *create_listint(const int *array, size_t size) -{ - listint_t *list; - listint_t *node; - int *tmp; - - list = NULL; - while (size--) - { - node = malloc(sizeof(*node)); - if (!node) - return (NULL); - tmp = (int *)&node->n; - *tmp = array[size]; - node->next = list; - node->prev = NULL; - list = node; - if (list->next) - list->next->prev = list; - } - return (list); -} - -/** - * main - Entry point - * - * Return: Always 0 - */ -int main(void) -{ - listint_t *list; - int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7}; - size_t n = sizeof(array) / sizeof(array[0]); - - list = create_listint(array, n); - if (!list) - return (1); - print_list(list); - printf("\n"); - insertion_sort_list(&list); - printf("\n"); - print_list(list); - return (0); -} diff --git a/100-main.c b/100-main.c new file mode 100644 index 0000000..82b69e2 --- /dev/null +++ b/100-main.c @@ -0,0 +1,21 @@ +#include +#include +#include "sort.h" + +/** + * main - Entry point + * + * Return: Always 0 + */ +int main(void) +{ + int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7}; + size_t n = sizeof(array) / sizeof(array[0]); + + print_array(array, n); + printf("\n"); + shell_sort(array, n); + printf("\n"); + print_array(array, n); + return (0); +} diff --git a/100-shell_sort.c b/100-shell_sort.c new file mode 100644 index 0000000..a133252 --- /dev/null +++ b/100-shell_sort.c @@ -0,0 +1,42 @@ +#include "sort.h" + +/** + * shell_sort - the entry poin + * @array: the array to be sorted + * @size:the size of the array + * + * Description: a function that sorts an array of integers in ascending order + * using the Shell sort algorithm + * + * + */ +void shell_sort(int *array, size_t size) +{ + size_t y, x, gap = 1; + int value; + + while (gap <= size / 3) + { + gap = gap * 3 + 1; + } + + while (gap > 0) + { + for (x = gap; x < size; x++) + { + value = array[x]; + y = x; + + while (y >= gap && array[y - gap] > value) + { + array[y] = array[y - gap]; + y -= gap; + } + + array[y] = value; + } + print_array(array, size); + /* alculate the next gap using the Knuth sequence */ + gap = (gap - 1) / 3; + } +} diff --git a/print_array.c b/print_array.c new file mode 100644 index 0000000..f122001 --- /dev/null +++ b/print_array.c @@ -0,0 +1,23 @@ +#include +#include + +/** + * print_array - Prints an array of integers + * + * @array: The array to be printed + * @size: Number of elements in @array + */ +void print_array(const int *array, size_t size) +{ + size_t i; + + i = 0; + while (array && i < size) + { + if (i > 0) + printf(", "); + printf("%d", array[i]); + ++i; + } + printf("\n"); +} diff --git a/print_list.c b/print_list.c new file mode 100644 index 0000000..b42e26b --- /dev/null +++ b/print_list.c @@ -0,0 +1,23 @@ +#include +#include "sort.h" + +/** + * print_list - Prints a list of integers + * + * @list: The list to be printed + */ +void print_list(const listint_t *list) +{ + int i; + + i = 0; + while (list) + { + if (i > 0) + printf(", "); + printf("%d", list->n); + ++i; + list = list->next; + } + printf("\n"); +} diff --git a/shell b/shell new file mode 100755 index 0000000..9337547 Binary files /dev/null and b/shell differ diff --git a/sort.h b/sort.h index 88ffbab..62e8cf4 100644 --- a/sort.h +++ b/sort.h @@ -22,6 +22,7 @@ void print_list(const listint_t *list); void bubble_sort(int *array, size_t size); void insertion_sort_list(listint_t **list); void quick_sort(int *array, size_t size); +void shell_sort(int *array, size_t size); /****** that returns ints *******/ int lomuto_pertition(int *array, int low, int hi, size_t size); diff --git a/test.c b/test.c new file mode 100644 index 0000000..b647e81 --- /dev/null +++ b/test.c @@ -0,0 +1,35 @@ +#include +#include +#include "sort.h" + +/** + * main - Entry point + * + * Return: Always 0 + */ +int main(void) +{ + shell_sort(NULL, 0); + return (0); +} +#include +#include +#include "sort.h" + +/** + * main - Entry point + * + * Return: Always 0 + */ +int main(void) +{ + int array[] = {1}; + size_t n = sizeof(array) / sizeof(array[0]);/* 1 */ + + print_array(array, n); + printf("\n"); + shell_sort(array, n); + printf("\n"); + print_array(array, n); + return (0); +}