From d838378112573c93b75a78cf802af6d72ce48d9c Mon Sep 17 00:00:00 2001 From: the1Riddle Date: Tue, 26 Sep 2023 09:13:27 +0300 Subject: [PATCH 1/3] corrections --- 0-bubble_sort.c | 2 +- sort.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/0-bubble_sort.c b/0-bubble_sort.c index 8c638b7..f6adaf2 100644 --- a/0-bubble_sort.c +++ b/0-bubble_sort.c @@ -3,7 +3,7 @@ /** * bubble_sort - function that sorts an array of integers in ascending order * @array: the arrey to be sorted - * @size: the size + * @size: the size of the array * * */ diff --git a/sort.h b/sort.h index a609164..b4f7583 100644 --- a/sort.h +++ b/sort.h @@ -12,9 +12,9 @@ */ typedef struct listint_s { - const int n; - struct listint_s *prev; - struct listint_s *next; + const int n; + struct listint_s *prev; + struct listint_s *next; } listint_t; void print_array(const int *array, size_t size); From 1d03ec8b16c767e9459bd1a042bf8c87f55c2be8 Mon Sep 17 00:00:00 2001 From: the1Riddle Date: Wed, 27 Sep 2023 08:59:47 -0700 Subject: [PATCH 2/3] shell sort tested and ready to be maeged to main --- 100-main.c | 21 +++++++++++++++++++++ 100-shell_sort.c | 42 ++++++++++++++++++++++++++++++++++++++++++ print_array.c | 23 +++++++++++++++++++++++ print_list.c | 23 +++++++++++++++++++++++ shell | Bin 0 -> 16952 bytes sort.h | 1 + 6 files changed, 110 insertions(+) create mode 100644 100-main.c create mode 100644 100-shell_sort.c create mode 100644 print_array.c create mode 100644 print_list.c create mode 100755 shell 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 0000000000000000000000000000000000000000..9337547d24b1299110c420695976be8bc1bd5dbe GIT binary patch literal 16952 zcmeHOe{dVc6<%3!;t)r6ASot5qNbsfkXn(0bwZlhkhZ`@w&YkvmW*@) zhXI<@z*HEwnJF#l474e9iU;}!)1fo8|KJcHfwt6{=`iU)r-4aY1t>Muz6z>I+T^(i0;6)}a7`cUnWH(njNDV?_6sjL&S4jm* zcH%FTQ${W?)#oQ;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_YN4*9-xsiv7d7|tQ5}- zia;uD!~=U$xxip1Js8gzdo5H?E`g(GiI}3otsV77bdQkg{n2c{2*mcLP=$#`#w@ux zp2;TCDVw09EE7*g$w1`>lZFV$AOw&N>`6nUOg7#t0!DlYB28&%r{!n{;{9q8_s8Ik zCDVsFKTQkHqpIjY0?tfx1X=`eB@N)}D0gXo5{`c9vGm|K!AyLg$;kT`RV;NmPxu*p zXwSg*d_T(QN0~M8euB9sWUThP)cq+VJ;$;pUSFB(K*nm%_qU9GR#fP;zZU%a*mH3E zd0)oJ_4B^W+5RZ(X-~%Xe80?iLe)!cq&TxZ@1GLjsLO26_tlJ#pv;=chc(>SsWmd7P<-_33;e?a^4~`*g;js?6DbZo?=t6jQe6`+3H3rRaUR@ zAIhj%?8(L%zaN96n6Q8RSei+p^G?<|{tr0p{mPCpq}(w$+{=Brwf@~ofsys+vVR-} zR(sx;Fb3IBA^T(erNjP^DqyU1beQef?NbhW{-1-9|Nmfphdc4Cvgh#^ex<39|G!YI zoGY9s{4GAzf3~moLqKBeGR4xjv;LP+VzuAxH&q!mDzqe?KTMrQhW09I*(vQC_?)v- zpJm3kVOwLd=l$B_^&pX0&y;3l=IQ>O<}TOI_x*fsozJJ!^+$>AcpM)CM_pul-v5oM z8^~Q`WJzq#I3LDVdl6E~K@+(icENTmSHqY%uAlEOYn8oIpX#!vC8~av%j|4)LPCB JRYnd`{0lm?wjBTf literal 0 HcmV?d00001 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); From 1a6bd9aa279b177562b05200f6b54716dacaf4ac Mon Sep 17 00:00:00 2001 From: the1Riddle Date: Wed, 27 Sep 2023 12:13:44 -0700 Subject: [PATCH 3/3] for testing a file --- test.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 test.c 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); +}