@@ -244,13 +244,12 @@ def start(self,T=0.2):
244
244
245
245
246
246
elif self .st ['merge' ] is True :
247
- self .data = self . mergesort (self .data )
247
+ self .mergesort (self .data , 0 , self . N - 1 )
248
248
self .display (self .N ,self .data ,['green' for _ in range (self .N )])
249
249
250
- pass
251
-
252
250
elif self .st ['quick' ] is True :
253
- pass
251
+ self .quicksort (self .data ,0 ,self .N - 1 )
252
+ self .display (self .N ,self .data ,['green' for _ in range (self .N )])
254
253
255
254
elif self .st ['bucket' ] is True :
256
255
pass
@@ -259,52 +258,74 @@ def start(self,T=0.2):
259
258
'''show messege box'''
260
259
pass
261
260
262
- # print(self.data)
263
-
264
- #----------------------------------------------------------------------
265
- def mergeelements (self ,l ,r ):
266
-
267
- i ,j = 0 ,0
268
- b = []
269
- while i < len (l ) and j < len (r ):
270
- if l [i ]< r [j ]:
271
- b .append (l [i ])
272
- i += 1
273
- # self.display(self.N,b+self.data[len(b):],['green' if x<len(b) else 'purple' for x in range(self.N) ])
274
- # time.sleep(0.2)
275
- else :
276
- b .append (r [j ])
277
- j += 1
278
- # self.display(self.N,b+self.data[len(b):],['green' if x<len(b) else 'purple' for x in range(self.N) ])
279
- # time.sleep(0.2)
280
-
281
- while i < len (l ):
282
- b .append (l [i ])
283
- i += 1
284
- # self.display(self.N,b+self.data[len(b):],['green' if x<len(b) else 'purple' for x in range(self.N) ])
285
- # time.sleep(0.2)
261
+ # -----------merge sort-------------------------------------
262
+
263
+ def mergesort (self ,a ,front ,last ):
264
+ if front < last :
265
+ mid = (front + last )// 2
266
+
267
+ self .mergesort (a ,front ,mid )
268
+ self .mergesort (a ,mid + 1 ,last )
269
+
270
+
271
+ self .display (self .N ,self .data ,['blue' for _ in range (self .N )])
286
272
287
- while j < len (r ):
288
- b .append (r [j ])
289
- j += 1
290
- # self.display(self.N,b+self.data[len(b):],['green' if x<len(b) else 'purple' for x in range(self.N) ])
291
- # time.sleep(0.2)
273
+ rj = mid + 1
274
+ if a [mid ]<= a [mid + 1 ]:
275
+ return
292
276
293
- return b
294
-
277
+ while front <= mid and rj <= last :
278
+ if a [front ]<= a [rj ]:
279
+ front += 1
280
+ else :
281
+ temp = a [rj ]
282
+ i = rj
283
+ while i != front :
284
+ a [i ]= a [i - 1 ]
285
+ self .display (self .N ,self .data ,['purple' if x == i else 'blue' for x in range (self .N )])
286
+ time .sleep (0.1 )
287
+ i -= 1
288
+ a [front ]= temp
289
+
290
+ front += 1
291
+ mid += 1
292
+ rj += 1
293
+
294
+ self .display (self .N ,self .data ,['blue' for _ in range (self .N )])
295
+ time .sleep (0.2 )
295
296
296
- def mergesort (self ,a ):
297
- size = len (a )
298
- if size < 2 :
299
- return a
300
- mid = size // 2
301
- l = a [:mid ]
302
- r = a [mid :]
303
- l = self .mergesort (l )
304
- r = self .mergesort (r )
305
- return self .mergeelements (l ,r )
297
+ #--------------------------------------------------quick sort---------------
298
+
299
+ def partition (self ,a ,i ,j ):
300
+ '''
301
+ a -> is the array
302
+ i -> index from front
303
+ j -> index from back'''
304
+
305
+ l = i # left index
306
+
307
+ pivot = a [i ]
308
+
309
+ while i < j :
310
+ while i < len (a ) and a [i ]<= pivot :
311
+ i += 1
312
+ while a [j ]> pivot :
313
+ j -= 1
314
+ if i < j :
315
+ a [i ],a [j ]= a [j ],a [i ]
316
+
317
+ a [j ],a [l ]= a [l ],a [j ]
318
+ return j
319
+
320
+ def quicksort (self ,a ,i ,j ):
321
+ if i < j :
322
+ x = self .partition (a ,i ,j )
323
+
324
+ self .quicksort (a ,i ,x - 1 )
325
+ self .quicksort (a ,x + 1 ,j )
326
+ #--------------------------------------------------quick sort---------------
306
327
307
- #---------------------------------------------------------------------------
328
+
308
329
309
330
win = Style (theme = 'cyborg' ).master
310
331
obj = window (win , 'Sorting Algorithm Visualizer' )
0 commit comments