1
+ import time
2
+
1
3
def selection (A ):
2
4
"""
3
5
Algo name: Selection Sort
@@ -14,7 +16,7 @@ def selection(A):
14
16
if A [j ] < A [position ]:
15
17
position = j
16
18
A [i ], A [position ] = A [position ], A [i ]
17
- print ( "Sorted using Selection Sort: " , A )
19
+
18
20
19
21
###############################################################
20
22
@@ -36,8 +38,6 @@ def insertion(A):
36
38
A [position ] = A [position - 1 ]
37
39
position = position - 1
38
40
A [position ] = cvalue
39
-
40
- print ("Sorted using Insertion Sort: " , A )
41
41
42
42
###############################################################
43
43
@@ -55,7 +55,7 @@ def bubble(A):
55
55
for i in range (0 , p ):
56
56
if A [i ] > A [i + 1 ]:
57
57
A [i ], A [i + 1 ] = A [i + 1 ], A [i ]
58
- print ( "Sorted using Bubble Sort: " , A )
58
+
59
59
60
60
###############################################################
61
61
@@ -81,7 +81,6 @@ def shell(A):
81
81
A [j + gap ] = temp
82
82
i = i + 1
83
83
gap = gap // 2
84
- print ("Sorted using Shell Sort: " , A )
85
84
86
85
###############################################################
87
86
@@ -97,11 +96,9 @@ def merge(A, l, m, r):
97
96
98
97
returns Sorted partial array A
99
98
"""
100
-
101
- i = l
102
- j = m + 1
103
- k = l
99
+ i = l ; j = m + 1 ; k = l
104
100
B = [0 ] * (r + 1 )
101
+
105
102
while i <= m and j <= r :
106
103
if A [i ] < A [j ]:
107
104
B [k ] = A [i ]
@@ -113,12 +110,10 @@ def merge(A, l, m, r):
113
110
114
111
while i <= m :
115
112
B [k ] = A [i ]
116
- i = i + 1
117
- k = k + 1
113
+ i = i + 1 ; k = k + 1
118
114
while j <= r :
119
115
B [k ] = A [j ]
120
- j = j + 1
121
- k = k + 1
116
+ j = j + 1 ; k = k + 1
122
117
for x in range (l , r + 1 ):
123
118
A [x ] = B [x ]
124
119
@@ -140,30 +135,99 @@ def mergesort(A, left, right):
140
135
merge (A , left , mid , right )
141
136
142
137
138
+ ###############################################################
139
+
140
+ def partition (A , low , high ):
141
+ pivot = A [low ]
142
+ i = low + 1 ; j = high
143
+
144
+ while True :
145
+ while i <= j and A [i ] <= pivot :
146
+ i = i + 1
147
+ while i <= j and A [i ] > pivot :
148
+ j = j - 1
149
+
150
+ if i <= j :
151
+ A [i ], A [j ] = A [j ], A [i ]
152
+ else :
153
+ break
154
+ A [low ], A [j ] = A [j ], A [low ]
155
+ return j
156
+
157
+ def quicksort (A , low , high ):
158
+ if low < high :
159
+ p = partition (A , low , high )
160
+ quicksort (A , low , p - 1 )
161
+ quicksort (A , p + 1 , high )
162
+
143
163
###############################################################
144
164
def switch_case (choice ):
165
+
145
166
if choice == 1 :
146
- selection (A ),
167
+ start = time .time ()
168
+ selection (A )
169
+ end = time .time ()
170
+ print ("Sorted using Selection Sort: " , A )
171
+ print ('Time required: ' , (end - start ) * 10 ** 6 , "nanoseconds" )
147
172
elif choice == 2 :
173
+ start = time .time ()
148
174
insertion (A )
175
+ end = time .time ()
176
+ print ("Sorted using Insertion Sort: " , A )
177
+ print ('Time required: ' , (end - start ) * 10 ** 6 , "nanoseconds" )
149
178
elif choice == 3 :
179
+ start = time .time ()
150
180
bubble (A )
181
+ end = time .time ()
182
+ print ("Sorted using Bubble Sort: " , A )
183
+ print ('Time required: ' , (end - start ) * 10 ** 6 , "nanoseconds" )
151
184
elif choice == 4 :
185
+ start = time .time ()
152
186
shell (A )
187
+ end = time .time ()
188
+ print ("Sorted using Shell Sort: " , A )
189
+ print ('Time required: ' , (end - start ) * 10 ** 6 , "nanoseconds" )
153
190
elif choice == 5 :
191
+ start = time .time ()
154
192
mergesort (A , 0 , len (A )- 1 )
193
+ end = time .time ()
155
194
print ("Sorted using Merge Sort: " , A )
195
+ print ('Time required: ' , (end - start ) * 10 ** 6 , "nanoseconds" )
196
+ elif choice == 6 :
197
+ start = time .time ()
198
+ quicksort (A , 0 , len (A )- 1 )
199
+ end = time .time ()
200
+ print ("Sorted using Quick Sort: " , A )
201
+ print ('Time required: ' , (end - start ) * 10 ** 6 , "nanoseconds" )
156
202
157
203
###############################################################
158
204
159
- A = list (map (int , input ("Enter Array elements: " ).split ()))
160
-
161
- print ("Select Sorting Method" )
162
- print ("1. Selection Sort" )
163
- print ("2. Insertion Sort" )
164
- print ("3. Bubble Sort" )
165
- print ("4. Shell Sort" )
166
- print ("5. Merge Sort" )
167
-
168
- choice = int (input ("Enter your choice: " ))
169
- switch_case (choice )
205
+ def options ():
206
+ print ("Select Sorting Method" )
207
+ print ("1. Selection Sort" )
208
+ print ("2. Insertion Sort" )
209
+ print ("3. Bubble Sort" )
210
+ print ("4. Shell Sort" )
211
+ print ("5. Merge Sort" )
212
+ print ("6. Quick Sort" )
213
+ print ("#. Exit" )
214
+
215
+ while True :
216
+ x = input ("Enter your own array values? [y/n]: " )
217
+ if x == 'y' :
218
+ A = list (map (int , input ("Enter values: " ).split ()))
219
+ options ()
220
+ else :
221
+ filename = "num.txt"
222
+ my_file = open (filename , "r" )
223
+ content = my_file .read ()
224
+ A = list (map (int , content .split ()))
225
+ my_file .close ()
226
+ print ("Reading values from" , filename )
227
+ options ()
228
+
229
+ choice = int (input ("Enter your choice: " ))
230
+ if choice != '#' :
231
+ switch_case (choice )
232
+ else :
233
+ break
0 commit comments