diff --git a/Python/sorting/bubble_sort.py b/Python/sorting/bubble_sort.py new file mode 100644 index 0000000..a88d33d --- /dev/null +++ b/Python/sorting/bubble_sort.py @@ -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) diff --git a/Python/sorting/selection_sort.py b/Python/sorting/selection_sort.py new file mode 100644 index 0000000..bd27b63 --- /dev/null +++ b/Python/sorting/selection_sort.py @@ -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) diff --git a/Python/sorting/shell_sort.py b/Python/sorting/shell_sort.py new file mode 100644 index 0000000..48812d7 --- /dev/null +++ b/Python/sorting/shell_sort.py @@ -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)