Skip to content

Shell Sort Exercise #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions algorithms/6_ShellSort/shell_sort_exercise.md
Original file line number Diff line number Diff line change
@@ -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)
32 changes: 32 additions & 0 deletions algorithms/6_ShellSort/shell_sort_exercise_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def shell_sort(arr):
n = len(arr)
div = 2
gap = n//div
while gap > 0:
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:
index_to_delete.append(j)
arr[j] = arr[j-gap]
j -= gap
arr[j] = temp
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)
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}')