Skip to content

Commit

Permalink
feat: add shell sort and selection sort in Python (doocs#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlei995 authored May 9, 2022
1 parent e3c1d5e commit 4be97ba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions basic/sorting/SelectionSort/SelectionSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def selectionSort(arr):
n = len(arr)
for i in range(n - 1):
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j
arr[min_index], arr[i] = arr[i], arr[min_index]


arr = [26, 11, 99, 33, 69, 77, 55, 56, 67]
selectionSort(arr)
print(arr)
17 changes: 17 additions & 0 deletions basic/sorting/ShellSort/ShellSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def shellSort(arr):
n = len(arr)
gap = int(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 = int(gap / 2)


arr = [12, 34, 54, 2, 3]
shellSort(arr)
print(arr)

0 comments on commit 4be97ba

Please sign in to comment.