From c979dab96ab2187a60b90009120e5498498db2a3 Mon Sep 17 00:00:00 2001 From: Master Amitoj Date: Thu, 22 Oct 2020 15:57:12 +0530 Subject: [PATCH 1/3] Shell Sort Exercise --- algorithms/6_ShellSort/desktop.ini | Bin 0 -> 246 bytes algorithms/6_ShellSort/shell_sort.py | 29 ++++++++++++++++ algorithms/6_ShellSort/shell_sort_exercise.md | 18 ++++++++++ .../shell_sort_exercise_solution.py | 32 ++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 algorithms/6_ShellSort/desktop.ini create mode 100644 algorithms/6_ShellSort/shell_sort.py create mode 100644 algorithms/6_ShellSort/shell_sort_exercise.md create mode 100644 algorithms/6_ShellSort/shell_sort_exercise_solution.py diff --git a/algorithms/6_ShellSort/desktop.ini b/algorithms/6_ShellSort/desktop.ini new file mode 100644 index 0000000000000000000000000000000000000000..d375fae1cf888d20b0b85050f528777517aafa4d GIT binary patch literal 246 zcmY+8!3u(45QM*T&^!15Ar_qiPleH`sAF~zu@WjHD|-As|3ebXEVH{a!|sXMai-#8 z%|$_Lt+;Wd;lPlA`zFja53Xd?>MGXUNlAiYMHw_(_pR)uvXrNKXXI?08&3AZgy@&i wdRuzwZe3>1LQTzE-m4;ie7diKJ7>zozY{UGI`*H^XJk5KeIGH>H4fi?0mSkth5!Hn literal 0 HcmV?d00001 diff --git a/algorithms/6_ShellSort/shell_sort.py b/algorithms/6_ShellSort/shell_sort.py new file mode 100644 index 0000000..393a9af --- /dev/null +++ b/algorithms/6_ShellSort/shell_sort.py @@ -0,0 +1,29 @@ +def shell_sort(arr): + n = len(arr) + gap = n//2 + while gap > 0: + for i in range(gap, n): + temp = arr[i] + j = i + while j >= gap and arr[j-gap] > temp: + arr[j] = arr[j-gap] + j -= gap + arr[j] = temp + gap //= 2 + + +if __name__ == '__main__': + # + tests = [ + [11, 9, 29, 7, 2, 15, 28], + [3, 7, 9, 11], + [25, 22, 21, 10, 33, 22], + [29, 15, 28], + [6], + [2, 1, 5, 7, 2, 0, 5] + ] + + for elements in tests: + print(f'Given unsorted array: {elements}') + shell_sort(elements) + print(f'Array after Sorting : {elements}') diff --git a/algorithms/6_ShellSort/shell_sort_exercise.md b/algorithms/6_ShellSort/shell_sort_exercise.md new file mode 100644 index 0000000..657c41a --- /dev/null +++ b/algorithms/6_ShellSort/shell_sort_exercise.md @@ -0,0 +1,18 @@ +# Exercise: Shell Sort + +Sort the elements of a given list using shell sort, but with a slight modification. Remove all the repeating occurances of elements while sorting. + +Traditionally, when comparing two elements in shell sort, we swap if first element is bigger than second, and do nothing otherwise. + + In this modified shell sort with duplicate removal, we will swap if first element is bigger than second, and do nothing if element is smaller, but if values are same, we will delete one of the two elements we are comparing before starting the next pass for the reduced gap. + + + +For example, given the unsorted list `[2, 1, 5, 7, 2, 0, 5, 1, 2, 9, 5, 8, 3]`, after sorting using shell sort without duplicates, the sorted list would be: + +``` +[0, 1, 2, 3, 5, 7, 8, 9] +``` + + + [Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/6_ShellSort/shell_sort_exercise_solution.py) \ No newline at end of file diff --git a/algorithms/6_ShellSort/shell_sort_exercise_solution.py b/algorithms/6_ShellSort/shell_sort_exercise_solution.py new file mode 100644 index 0000000..0ca51b6 --- /dev/null +++ b/algorithms/6_ShellSort/shell_sort_exercise_solution.py @@ -0,0 +1,32 @@ +def shell_sort(arr): + n = len(arr) + div = 2 + gap = n//div + while gap > 0: + IndexToDelete = [] + for i in range(gap, n): + temp = arr[i] + j = i + while j >= gap and arr[j-gap] >= temp: + if arr[j-gap] == temp: + IndexToDelete.append(j) + arr[j] = arr[j-gap] + j -= gap + arr[j] = temp + IndexToDelete=list(set(IndexToDelete)) + IndexToDelete.sort() + if IndexToDelete: + for i in IndexToDelete[-1::-1]: + del arr[i] + div *= 2 + n = len(arr) + gap = n//div + + +if __name__ == '__main__': + elements = [2, 1, 5, 7, 2, 0, 5, 1, 2, 9, 5, 8, 3] + + print(f'Given unsorted list: {elements}') + shell_sort(elements) + print(f'List after Sorting : {elements}') + From 7c6144ed4b9fd6b86ac97628de1075eb379eb873 Mon Sep 17 00:00:00 2001 From: Master Amitoj Date: Sun, 25 Oct 2020 16:11:16 +0530 Subject: [PATCH 2/3] Removed extra files --- algorithms/6_ShellSort/desktop.ini | Bin 246 -> 0 bytes algorithms/6_ShellSort/shell_sort.py | 29 --------------------------- 2 files changed, 29 deletions(-) delete mode 100644 algorithms/6_ShellSort/desktop.ini delete mode 100644 algorithms/6_ShellSort/shell_sort.py diff --git a/algorithms/6_ShellSort/desktop.ini b/algorithms/6_ShellSort/desktop.ini deleted file mode 100644 index d375fae1cf888d20b0b85050f528777517aafa4d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 246 zcmY+8!3u(45QM*T&^!15Ar_qiPleH`sAF~zu@WjHD|-As|3ebXEVH{a!|sXMai-#8 z%|$_Lt+;Wd;lPlA`zFja53Xd?>MGXUNlAiYMHw_(_pR)uvXrNKXXI?08&3AZgy@&i wdRuzwZe3>1LQTzE-m4;ie7diKJ7>zozY{UGI`*H^XJk5KeIGH>H4fi?0mSkth5!Hn diff --git a/algorithms/6_ShellSort/shell_sort.py b/algorithms/6_ShellSort/shell_sort.py deleted file mode 100644 index 393a9af..0000000 --- a/algorithms/6_ShellSort/shell_sort.py +++ /dev/null @@ -1,29 +0,0 @@ -def shell_sort(arr): - n = len(arr) - gap = n//2 - while gap > 0: - for i in range(gap, n): - temp = arr[i] - j = i - while j >= gap and arr[j-gap] > temp: - arr[j] = arr[j-gap] - j -= gap - arr[j] = temp - gap //= 2 - - -if __name__ == '__main__': - # - tests = [ - [11, 9, 29, 7, 2, 15, 28], - [3, 7, 9, 11], - [25, 22, 21, 10, 33, 22], - [29, 15, 28], - [6], - [2, 1, 5, 7, 2, 0, 5] - ] - - for elements in tests: - print(f'Given unsorted array: {elements}') - shell_sort(elements) - print(f'Array after Sorting : {elements}') From aa41e6545504826b98d7f8e29e6c06022c509159 Mon Sep 17 00:00:00 2001 From: Master Amitoj Date: Sun, 25 Oct 2020 16:40:47 +0530 Subject: [PATCH 3/3] Changed to snake_case --- .../6_ShellSort/shell_sort_exercise_solution.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/algorithms/6_ShellSort/shell_sort_exercise_solution.py b/algorithms/6_ShellSort/shell_sort_exercise_solution.py index 0ca51b6..9903870 100644 --- a/algorithms/6_ShellSort/shell_sort_exercise_solution.py +++ b/algorithms/6_ShellSort/shell_sort_exercise_solution.py @@ -3,20 +3,20 @@ def shell_sort(arr): div = 2 gap = n//div while gap > 0: - IndexToDelete = [] + index_to_delete = [] for i in range(gap, n): temp = arr[i] j = i while j >= gap and arr[j-gap] >= temp: if arr[j-gap] == temp: - IndexToDelete.append(j) + index_to_delete.append(j) arr[j] = arr[j-gap] j -= gap arr[j] = temp - IndexToDelete=list(set(IndexToDelete)) - IndexToDelete.sort() - if IndexToDelete: - for i in IndexToDelete[-1::-1]: + index_to_delete=list(set(index_to_delete)) + index_to_delete.sort() + if index_to_delete: + for i in index_to_delete[-1::-1]: del arr[i] div *= 2 n = len(arr)