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 Sort/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
username: aayushakrrana
fullname: AAYUSH KUMAR RANA
36 changes: 36 additions & 0 deletions Sort/mergesort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def mergeSort(alist):
print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]

mergeSort(lefthalf)
mergeSort(righthalf)

i=0
j=0
k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1
k=k+1

while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1

while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
print("Merging ",alist)

list_of_elem = [54,26,93,17,77,31,44,55,20]
mergeSort(list_of_elem)
print(list_of_elem)
13 changes: 13 additions & 0 deletions Sort/quicksort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def quicksort(arr):
less = []
equal = []
greater = []

if len(array) > 1:
pivot = array[-1]
for x in array:
if x < pivot: less.append(x)
if x == pivot: equal.append(x)
if x > pivot: greater.append(x)

return quicksort(less) + equal + quicksort(greater)
15 changes: 15 additions & 0 deletions Sort/selection_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#Time complexity O(n^2) Space complexity O(n)

def selection_sort(arr):
for i in range(len(arr)):
position_of_max = i
for j in range(i+1,len(arr)):
if arr[j]>arr[position_of_max]:
position_of_max = j
temp = arr[i]
arr[i] = arr[position_of_max]
arr[position_of_max] = temp
return arr

arr = [13,4,12,1,5]
print selection_sort(arr)