From a96c824bb4b32736b3ab68d78033fa0c38918581 Mon Sep 17 00:00:00 2001 From: Dheeraj Pilla Date: Tue, 10 May 2022 13:00:26 +0530 Subject: [PATCH] added shell sort --- basic_algo/shell_sort.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 basic_algo/shell_sort.py diff --git a/basic_algo/shell_sort.py b/basic_algo/shell_sort.py new file mode 100644 index 0000000..e2c5d5a --- /dev/null +++ b/basic_algo/shell_sort.py @@ -0,0 +1,27 @@ +# creating an empty list +mylist = [] + +# number of elements as input +n = int(input("Please enter number of elements you want to give for sorting: ")) + +# iterating till the range +for i in range(0, n): + mylist.append(int(input())) # adding the element + +def shellsort(mylist): + len_list = len(mylist) + gap = len_list // 2 + while gap > 0: + a = gap + while a < len_list: + temp = mylist[a] + b = a - gap + while b >= 0 and mylist[b] >= temp: + mylist[b+gap] = mylist[b] + b = b - gap + mylist[b + gap] = temp + a = a + 1 + gap = gap // 2 + +shellsort(mylist) +print(mylist) \ No newline at end of file