Skip to content
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ This project and everyone participating in it are governed by the [Contributor C
</tr>
<tr>
<td align="center"><a href="https://github.com/VishMaster17"><img src="https://avatars.githubusercontent.com/u/74035077?v=4?s=100" width="100px;" alt="VishMaster17"/><br /><sub><b>VishMaster17</b></sub></a><br /><a href="https://github.com/mpotane/PythonScriptPH/commits?author=VishMaster17" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/Itzudii"><img src="https://avatars.githubusercontent.com/u/145835542?v=4" width="100px;" alt="Itzudii"/><br /><sub><b>Itzudii</b></sub></a><br /><a href="https://github.com/mpotane/PythonScriptPH/commits?author=Itzudii" title="Documentation">📖</a></td>
</tr>

</tbody>
</table>

Expand Down
34 changes: 27 additions & 7 deletions algorithms/Sorting/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -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))
26 changes: 26 additions & 0 deletions algorithms/Sorting/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -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))
13 changes: 0 additions & 13 deletions algorithms/Sorting/insertion_sort.py3

This file was deleted.

38 changes: 38 additions & 0 deletions algorithms/Sorting/merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing import Tuple,List


def merge(a:List[int],x1:int,y1:int,x2:int,y2:int)->None:
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))
33 changes: 33 additions & 0 deletions algorithms/Sorting/quickt_sort.py
Original file line number Diff line number Diff line change
@@ -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))
34 changes: 26 additions & 8 deletions algorithms/Sorting/selection_sort.py
Original file line number Diff line number Diff line change
@@ -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]<l[minx]:
l[minx],l[j]=l[j],l[minx]


print(l)
from typing import Tuple,List

def selectionsort(a:List[int],size:int)->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))