1+ import time
2+ 13def selection (A ):
24 """
35 Algo name: Selection Sort
@@ -14,7 +16,7 @@ def selection(A):
1416 if A [j ] < A [position ]:
1517 position = j
1618 A [i ], A [position ] = A [position ], A [i ]
17- print ( "Sorted using Selection Sort: " , A )
19+ 1820
1921###############################################################
2022
@@ -36,8 +38,6 @@ def insertion(A):
3638 A [position ] = A [position - 1 ]
3739 position = position - 1
3840 A [position ] = cvalue
39- 40- print ("Sorted using Insertion Sort: " , A )
4141
4242###############################################################
4343
@@ -55,7 +55,7 @@ def bubble(A):
5555 for i in range (0 , p ):
5656 if A [i ] > A [i + 1 ]:
5757 A [i ], A [i + 1 ] = A [i + 1 ], A [i ]
58- print ( "Sorted using Bubble Sort: " , A )
58+ 5959
6060###############################################################
6161
@@ -81,7 +81,6 @@ def shell(A):
8181 A [j + gap ] = temp
8282 i = i + 1
8383 gap = gap // 2
84- print ("Sorted using Shell Sort: " , A )
8584
8685###############################################################
8786
@@ -97,11 +96,9 @@ def merge(A, l, m, r):
9796
9897 returns Sorted partial array A
9998 """
100- 101- i = l
102- j = m + 1
103- k = l
99+ i = l ; j = m + 1 ; k = l
104100 B = [0 ] * (r + 1 )
101+ 105102 while i <= m and j <= r :
106103 if A [i ] < A [j ]:
107104 B [k ] = A [i ]
@@ -113,12 +110,10 @@ def merge(A, l, m, r):
113110
114111 while i <= m :
115112 B [k ] = A [i ]
116- i = i + 1
117- k = k + 1
113+ i = i + 1 ; k = k + 1
118114 while j <= r :
119115 B [k ] = A [j ]
120- j = j + 1
121- k = k + 1
116+ j = j + 1 ; k = k + 1
122117 for x in range (l , r + 1 ):
123118 A [x ] = B [x ]
124119
@@ -140,30 +135,99 @@ def mergesort(A, left, right):
140135 merge (A , left , mid , right )
141136
142137
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+ 143163###############################################################
144164def switch_case (choice ):
165+ 145166 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" )
147172 elif choice == 2 :
173+ start = time .time ()
148174 insertion (A )
175+ end = time .time ()
176+ print ("Sorted using Insertion Sort: " , A )
177+ print ('Time required: ' , (end - start ) * 10 ** 6 , "nanoseconds" )
149178 elif choice == 3 :
179+ start = time .time ()
150180 bubble (A )
181+ end = time .time ()
182+ print ("Sorted using Bubble Sort: " , A )
183+ print ('Time required: ' , (end - start ) * 10 ** 6 , "nanoseconds" )
151184 elif choice == 4 :
185+ start = time .time ()
152186 shell (A )
187+ end = time .time ()
188+ print ("Sorted using Shell Sort: " , A )
189+ print ('Time required: ' , (end - start ) * 10 ** 6 , "nanoseconds" )
153190 elif choice == 5 :
191+ start = time .time ()
154192 mergesort (A , 0 , len (A )- 1 )
193+ end = time .time ()
155194 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" )
156202
157203###############################################################
158204
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