-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2.1.py
More file actions
99 lines (85 loc) · 3.15 KB
/
ex2.1.py
File metadata and controls
99 lines (85 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 14 17:11:58 2019
@author: astro
"""
x = int(input("Select the type of sorting (1 - Bubble; 2 - Selection; 3 - Quicksort; 4 - Merge): "))
A = [9, -3, 5, 2, 6, 8, -6, 1, 3]
arr = A
# Code for Bubble sort
if(x==1):
def bubbleSort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1): #we'll run loop till (n-i-1) coz first part is sorted and 2 is unsorted
if arr[j] > arr[j+1] : #If element at first place is greater than swap
arr[j],arr[j+1] = arr[j+1],arr[j]
bubbleSort(arr)
print (arr)
# Code for selection sort
if(x==2):
for i in range(len(A)):
min_idx = i #minimum index number
for j in range(i+1, len(A)): #
if A[min_idx] > A[j]: #condition to check minimum element
min_idx = j #update the index of minimum element
A[i], A[min_idx] = A[min_idx], A[i]
print (A)
# Code for Quicksort
if(x==3):
def partition(arr,low,high): # array , start (low) and end (high) index of the segment that need to be sorted (becoz we don't want to create any new array)
i = ( low-1 ) #first element which will be 0=start-1
pivot = arr[high]# we have chose pivot end index of the segment
for j in range(low , high): #for loop for the array
if arr[j] < pivot: #swap if element is lesser than pivot
i = i+1
arr[i],arr[j] = arr[j],arr[i]
arr[i+1],arr[high] = arr[high],arr[i+1]
return ( i+1 )
def quickSort(arr,low,high): #function for quicksort
if low < high: #
pi = partition(arr,low,high) #calling partition
quickSort(arr, low, pi-1) #recursive calls to sort the segment left of the partition index (low to till pi-1 )
quickSort(arr, pi+1, high) #recursive calls to sort the segment right or towards the higher indices of the partition index (pi+1 to till high )
n = len(arr)
quickSort(arr,0,n-1)
print ("Sorted array is:")
for i in range(n):
print ("%d" %arr[i]),
# Code for Merge sort
if(x==4):
def mergeSort(arr): # function for merge sort
if len(arr) >1:
mid = len(arr)//2 # Divide the array
L = arr[:mid] #left array till mid
R = arr[mid:] #right array till mid
mergeSort(L) #recursive calls to merge
mergeSort(R) #recursive calls to merge
i = j = k = 0 # i,j will mark the index of the smallest "unpicked" in L and R respectively while k will mark the index of the position that needs to be filled in array (arr)
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i+=1
else:
arr[k] = R[j]
j+=1
k+=1
while i < len(L):
arr[k] = L[i]
i+=1
k+=1
while j < len(R):
arr[k] = R[j]
j+=1
k+=1
def printList(arr):
for i in range(len(arr)):
print(arr[i],end=" ")
print()
if __name__ == '__main__':
print ("Given array is", end="\n")
printList(arr)
mergeSort(arr)
print("Sorted array is: ", end="\n")
printList(arr)