From 4550ec314fbcf6d5086c8a2310a9d79cd94a2200 Mon Sep 17 00:00:00 2001 From: Uditya Patel Date: Fri, 13 Mar 2026 16:01:52 +0530 Subject: [PATCH 1/3] add_new_sorting_algos_and_optimized --- algorithms/Sorting/bubble_sort.py | 34 +++++++++++++++++++----- algorithms/Sorting/insertion_sort.py | 26 ++++++++++++++++++ algorithms/Sorting/insertion_sort.py3 | 13 --------- algorithms/Sorting/merge_sort.py | 38 +++++++++++++++++++++++++++ algorithms/Sorting/quickt_sort.py | 33 +++++++++++++++++++++++ algorithms/Sorting/selection_sort.py | 34 ++++++++++++++++++------ 6 files changed, 150 insertions(+), 28 deletions(-) create mode 100644 algorithms/Sorting/insertion_sort.py delete mode 100644 algorithms/Sorting/insertion_sort.py3 create mode 100644 algorithms/Sorting/merge_sort.py create mode 100644 algorithms/Sorting/quickt_sort.py diff --git a/algorithms/Sorting/bubble_sort.py b/algorithms/Sorting/bubble_sort.py index 03d12b0..c76acae 100644 --- a/algorithms/Sorting/bubble_sort.py +++ b/algorithms/Sorting/bubble_sort.py @@ -1,8 +1,28 @@ -l=list(map(int,input().split())) -for i in range(len(l)): - for j in range(len(l)-i-1): - if l[j]>l[j+1]: - l[j],l[j+1]=l[j+1],l[j] - +from typing import Tuple,List + +def bubblesort(a:list,size:int) -> Tuple[List[int],int]: + step = 0 + isSwap = False + while isSwap: + for i in range(size-1): + if a[i] > a[i+1]: + a[i],a[i+1] = a[i+1],a[i] + isSwap = True + step+=1 + + return a,step -print(l) + +if __name__ == '__main__': + + import numpy as np + import time as t + a = list(range(1,100)) + np.random.shuffle(a) + size = len(a) + t0 = t.time() + sorted_array,steps = bubblesort(a,size) + t1 = t.time() + print("Sorted array:", sorted_array) + print("Total comparisons:", steps) + print("Execution time (s):", round(t1 - t0, 6)) \ No newline at end of file diff --git a/algorithms/Sorting/insertion_sort.py b/algorithms/Sorting/insertion_sort.py new file mode 100644 index 0000000..9935152 --- /dev/null +++ b/algorithms/Sorting/insertion_sort.py @@ -0,0 +1,26 @@ +from typing import Tuple,List + +def insertionsort(a:list,size:int) -> Tuple[List[int],int]: + step = 0 + for i in range(1, size): + key = a[i] + j = i - 1 + while j >= 0 and a[j] > key: + a[j + 1] = a[j] + j -= 1 + a[j + 1] = key + return a,step + + +if __name__ == '__main__': + import numpy as np + a = list(range(1,100)) + np.random.shuffle(a) + size = len(a) + import time as t + t0 = t.time() + sorted_array,steps = insertionsort(a,size) + t1 = t.time() + print("Sorted array:", sorted_array) + print("Total comparisons:", steps) + print("Execution time (s):", round(t1 - t0, 6)) \ No newline at end of file diff --git a/algorithms/Sorting/insertion_sort.py3 b/algorithms/Sorting/insertion_sort.py3 deleted file mode 100644 index 10fb3f3..0000000 --- a/algorithms/Sorting/insertion_sort.py3 +++ /dev/null @@ -1,13 +0,0 @@ -def insertion_sort(l): - for i in range(1,len(l)): - temp=l[i] - j=i-1 - while(j>=0 and tempNone: + i = x1 + j = x2 + while True: + if j >= y2: + break + if a[i] >= a[j]: + a.insert(i,a.pop(j)) + j+=1 + else: + i+=1 + +def mergesort(a:List[int],low:int,high:int)->Tuple[int,int]: + + if len(a[low:high]) <= 1: + return low,high + mid = low+(abs(high-low)//2) + x1,y1 = mergesort(a,low,mid) + x2,y2 = mergesort(a,mid,high) + merge(a,x1,y1,x2,y2) + return low,high + + + +if __name__ == '__main__': + import numpy as np + a = list(range(1,100)) + np.random.shuffle(a) + size = len(a) + import time as t + t0 = t.time() + sorted_array = mergesort(a,0,len(a)) + t1 = t.time() + print("Sorted array:", a) + print("Execution time (s):", round(t1 - t0, 6)) \ No newline at end of file diff --git a/algorithms/Sorting/quickt_sort.py b/algorithms/Sorting/quickt_sort.py new file mode 100644 index 0000000..96930bc --- /dev/null +++ b/algorithms/Sorting/quickt_sort.py @@ -0,0 +1,33 @@ + +from typing import Tuple,List + +def partition(arr:List[int],low:int,high:int): + pivot = arr[high] + swapi = low + for k in range(low,high): + if arr[k] <= pivot: + arr[k],arr[swapi] = arr[swapi],arr[k] + swapi+=1 + arr[high],arr[swapi] = arr[swapi],arr[high] + return swapi + + +def quicksort(arr:List[int],low:int,high:int): + if low < high: + index = partition(arr,low,high) + quicksort(arr,low,index-1) + quicksort(arr,index,high) + + + +if __name__ == '__main__': + import numpy as np + a = list(range(1,100)) + np.random.shuffle(a) + size = len(a) + import time as t + t0 = t.time() + quicksort(a,0,len(a)-1) + t1 = t.time() + print("Sorted array:", a) + print("Execution time (s):", round(t1 - t0, 6)) \ No newline at end of file diff --git a/algorithms/Sorting/selection_sort.py b/algorithms/Sorting/selection_sort.py index 6059e64..52d2f2f 100644 --- a/algorithms/Sorting/selection_sort.py +++ b/algorithms/Sorting/selection_sort.py @@ -1,9 +1,27 @@ -l=list(map(int,input().split())) -for i in range(len(l)): - minx=i - for j in range(i+1,len(l)): - if l[j]Tuple[list[int],int]: + step = 0 + for i in range(size): + small = i + for j in range(i,size): + if a[j] < a[small]: + small = j + step+=1 + a[i],a[small] = a[small],a[i] + return a,step + + +if __name__ == '__main__': + import numpy as np + a = list(range(1,100)) + np.random.shuffle(a) + size = len(a) + import time as t + t0 = t.time() + sorted_array,steps = selectionsort(a,size) + t1 = t.time() + print("Sorted array:", sorted_array) + print("Total comparisons:", steps) + print("Execution time (s):", round(t1 - t0, 6)) \ No newline at end of file From 402e451f4283748a30c570b378aae0e7aae14c9f Mon Sep 17 00:00:00 2001 From: Uditya Patel Date: Fri, 13 Mar 2026 16:12:11 +0530 Subject: [PATCH 2/3] add_name_on_contributer_in_md_file --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index b82422c..4ee84b6 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,9 @@ This project and everyone participating in it are governed by the [Contributor C VishMaster17
VishMaster17

📖 + + Itzudii
Itzudii

📖 + From eecb86adc939e6afa48c95675256910af48916b1 Mon Sep 17 00:00:00 2001 From: Uditya Patel Date: Fri, 13 Mar 2026 16:14:21 +0530 Subject: [PATCH 3/3] add_name_in_contributers_again --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 4ee84b6..ee531c3 100644 --- a/README.md +++ b/README.md @@ -67,10 +67,9 @@ This project and everyone participating in it are governed by the [Contributor C VishMaster17
VishMaster17

📖 - - Itzudii
Itzudii

📖 +