From cde04a257839f23c611372aef5f3f75fe6faf68f Mon Sep 17 00:00:00 2001 From: Tuncay Coklu Date: Sat, 27 Oct 2018 15:03:30 +0300 Subject: [PATCH 1/3] Shell sort added --- sorting/shell.py | 23 +++++++++++++++++++++++ tests.py | 9 +++++++++ 2 files changed, 32 insertions(+) create mode 100644 sorting/shell.py diff --git a/sorting/shell.py b/sorting/shell.py new file mode 100644 index 0000000..953ce9c --- /dev/null +++ b/sorting/shell.py @@ -0,0 +1,23 @@ +import numpy + +def shellSort(input_list): + + gap = len(input_list) // 2 + while gap > 0: + + for i in range(gap, len(input_list)): + temp = input_list[i] + j = i +# Sort the sub list for this gap + while j >= gap and input_list[j - gap] > temp: + input_list[j] = input_list[j - gap] + j = j-gap + input_list[j] = temp +# Reduce the gap for the next element + gap = gap//2 + +# 20 random numbers generated +nums = numpy.random.randint(100, size=20).tolist() +# 20 random numbers sorted +shellSort(nums) +print(nums) \ No newline at end of file diff --git a/tests.py b/tests.py index 270c350..f19758b 100644 --- a/tests.py +++ b/tests.py @@ -55,3 +55,12 @@ print "Bucket Sort incorrect" except: print "Bucketsort function errored or is incomplete" + +try: + from shell import shell + if(shell(list(nums)) == sortedNums): + print "Shell Sort success" + else: + print "Shell Sort incorrect" +except: + print "Shell sort function errored or is incomplete" \ No newline at end of file From 902d700299368db72299afc9f2cf49ce36fcdb9f Mon Sep 17 00:00:00 2001 From: Tuncay Coklu Date: Sat, 27 Oct 2018 15:08:01 +0300 Subject: [PATCH 2/3] Shell sort modified --- sorting/shell.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/sorting/shell.py b/sorting/shell.py index 953ce9c..0ee368e 100644 --- a/sorting/shell.py +++ b/sorting/shell.py @@ -1,23 +1,18 @@ import numpy -def shellSort(input_list): +def shellSort(numbers): - gap = len(input_list) // 2 + gap = len(numbers) // 2 while gap > 0: - for i in range(gap, len(input_list)): - temp = input_list[i] + for i in range(gap, len(numbers)): + temp = numbers[i] j = i # Sort the sub list for this gap - while j >= gap and input_list[j - gap] > temp: - input_list[j] = input_list[j - gap] + while j >= gap and numbers[j - gap] > temp: + numbers[j] = numbers[j - gap] j = j-gap - input_list[j] = temp + numbers[j] = temp # Reduce the gap for the next element gap = gap//2 - -# 20 random numbers generated -nums = numpy.random.randint(100, size=20).tolist() -# 20 random numbers sorted -shellSort(nums) -print(nums) \ No newline at end of file + return numbers From c1d16218130e8d7925e1a5d4a30a839fdc62aa13 Mon Sep 17 00:00:00 2001 From: Tuncay Coklu Date: Sat, 27 Oct 2018 15:10:26 +0300 Subject: [PATCH 3/3] Add to Contributors list --- sorting/shell.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/sorting/shell.py b/sorting/shell.py index 0ee368e..585e07c 100644 --- a/sorting/shell.py +++ b/sorting/shell.py @@ -1,5 +1,3 @@ -import numpy - def shellSort(numbers): gap = len(numbers) // 2