Skip to content
Merged

new #44

Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions Python/sorting/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from collections import Counter

def bubble_sort(arr, freq):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if (freq[arr[j]], -arr[j]) < (freq[arr[j+1]], -arr[j+1]):
arr[j], arr[j+1] = arr[j+1], arr[j]

if __name__ == "__main__":
arr = [4,6,2,4,3,2,2,6]
freq = Counter(arr)
bubble_sort(arr,freq)
print(arr)
16 changes: 16 additions & 0 deletions Python/sorting/selection_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from collections import Counter

def selection_sort(arr, freq):
n = len(arr)
for i in range(n):
max_idx = i
for j in range(i+1, n):
if (freq[arr[j]], -arr[j]) > (freq[arr[max_idx]], -arr[max_idx]):
max_idx = j
arr[i], arr[max_idx] = arr[max_idx], arr[i]

if __name__ == "__main__":
arr = [4,6,2,4,3,2,2,6]
freq = Counter(arr)
selection_sort(arr,freq)
print(arr)
20 changes: 20 additions & 0 deletions Python/sorting/shell_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from collections import Counter

def shell_sort(arr, freq):
n=len(arr)
gap=n//2
while gap>0:
for i in range(gap,n):
temp=arr[i]
j=i
while j>=gap and (freq[arr[j-gap]],-arr[j-gap])<(freq[temp],-temp):
arr[j]=arr[j-gap]
j-=gap
arr[j]=temp
gap//=2

if __name__=="__main__":
arr=[4,6,2,4,3,2,2,6]
freq=Counter(arr)
shell_sort(arr,freq)
print(arr)