Skip to content

Commit cd3d548

Browse files
authored
Merge pull request #25 from anuragmuxui/anuragmuxui-contributions
Bubble sort
2 parents eacdc0b + 84f9ae6 commit cd3d548

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

Python/sorting/bubble_sort.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from collections import Counter
2+
3+
def bubble_sort(arr, freq):
4+
n = len(arr)
5+
for i in range(n):
6+
for j in range(0, n-i-1):
7+
if (freq[arr[j]], -arr[j]) < (freq[arr[j+1]], -arr[j+1]):
8+
arr[j], arr[j+1] = arr[j+1], arr[j]
9+
10+
if __name__ == "__main__":
11+
arr = [4,6,2,4,3,2,2,6]
12+
freq = Counter(arr)
13+
bubble_sort(arr,freq)
14+
print(arr)

Python/sorting/selection_sort.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from collections import Counter
2+
3+
def selection_sort(arr, freq):
4+
n = len(arr)
5+
for i in range(n):
6+
max_idx = i
7+
for j in range(i+1, n):
8+
if (freq[arr[j]], -arr[j]) > (freq[arr[max_idx]], -arr[max_idx]):
9+
max_idx = j
10+
arr[i], arr[max_idx] = arr[max_idx], arr[i]
11+
12+
if __name__ == "__main__":
13+
arr = [4,6,2,4,3,2,2,6]
14+
freq = Counter(arr)
15+
selection_sort(arr,freq)
16+
print(arr)

0 commit comments

Comments
 (0)