diff --git a/Sort/Readme.md b/Sort/Readme.md new file mode 100644 index 0000000..90f6a6f --- /dev/null +++ b/Sort/Readme.md @@ -0,0 +1,2 @@ +username: aayushakrrana +fullname: AAYUSH KUMAR RANA diff --git a/Sort/mergesort.py b/Sort/mergesort.py new file mode 100644 index 0000000..4c8e5b6 --- /dev/null +++ b/Sort/mergesort.py @@ -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) diff --git a/Sort/quicksort.py b/Sort/quicksort.py new file mode 100644 index 0000000..8579e51 --- /dev/null +++ b/Sort/quicksort.py @@ -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) diff --git a/Sort/selection_sort.py b/Sort/selection_sort.py new file mode 100644 index 0000000..39939ba --- /dev/null +++ b/Sort/selection_sort.py @@ -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)