Skip to content

Commit 58c6449

Browse files
committed
merge added ++++
1 parent 56c4376 commit 58c6449

File tree

2 files changed

+69
-47
lines changed

2 files changed

+69
-47
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/test.py
2-
/test2.py
2+
/test2.py
3+
/seed.py

algo_visualizer.py

Lines changed: 67 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,12 @@ def start(self,T=0.2):
244244

245245

246246
elif self.st['merge'] is True:
247-
self.data=self.mergesort(self.data)
247+
self.mergesort(self.data,0,self.N-1)
248248
self.display(self.N,self.data,['green' for _ in range(self.N)])
249249

250-
pass
251-
252250
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)])
254253

255254
elif self.st['bucket'] is True:
256255
pass
@@ -259,52 +258,74 @@ def start(self,T=0.2):
259258
'''show messege box'''
260259
pass
261260

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)])
286272

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
292276

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)
295296

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---------------
306327

307-
#---------------------------------------------------------------------------
328+
308329

309330
win = Style(theme='cyborg').master
310331
obj = window(win, 'Sorting Algorithm Visualizer')

0 commit comments

Comments
 (0)