Two words of gossip
The sort described in this article refers to the sort based on the interchange. So, supposedly, this article should be called a dynamic display based on the sort of exchange, but this is too much of a mouthful.
Effect Show
The final effect is as follows.
Implementation method
It should be explained that the graphical interface is implemented here through Pygame, and the program is written using Python 3.5. The advantage of using Pygame is that it is very free (and of course very cumbersome).
We need to write four files altogether: draw.py,sort_show.py,sort.py,main.py.
1.draw.py
Here, we first write a draw class in draw.py to implement the basic flow of the Pygame build interface. It is necessary to note that there are some things in this class that we do not use here.
#-*-Coding:utf-8-*-"" "Created on Tue Dec 10:24:50 2015@author:super Zhang" "" Import pygamefrom pygame.locals Import *class Draw: "" "to provide a general structure for using Pygame STRUCTURE:self.mainloop () |-self.run _init () | |-self. S_pre_run_init () | |-self. S_update () | |-self.run () |-sef. S_event (Event) | |-self. S_keydown_event (Key) | |-self. S_keyup_event (Key) | |-self. S_mousebutton_event (Button) | |-self. S_mouse_move_event () |-self. S_time () |-self. S_move () |-self. S_update () |-self. S_draw () |-self. S_load_img () method:self. M_screen_mode (mode,screen_size) self. M_draw_text (Size,text,pos,text_color) self. M_mouse_pos () self. M_load_img (Img_file) "" "Def __init__ (self): self. Screen_size= (720,480) self.screen_mode=0 def m_screen_mode (self,mode,screen_size): "" "usually called in __init__" "" Self.screen_mode=mode self. Screen_size=screen_size def mainloop (self): Self.run_init () while True:self.run () def Run_ Init (self): Pygame.init () self. S_pre_run_init () Self.screen=pygame.display.set_mode (self. screen_size,self.screen_mode,32) self. S_update () def s_pre_run_init (self): Pass def Run (self): to event in Pygame.event.get (): I F event.type==quit:pygame.quit () else:self. S_event (event) self. S_mouse_move_event () self. S_time () self. S_move () self. S_update () def s_event (self,event): if Event.type==keydown:self. S_keydown_event (Event.key) elif event.type==keyup:self. S_keyup_event (Event.key) elif event.type==mousebuttondown:self. S_mousebutton_event (Event.button) def s_keydown_event (Self,key): Pass Def S_keyup_event (Self,key): Pass def s_mousebutton_event (Self,button): Pass def s_mouse_move_event (self) : Pass def s_time (self): Pass Def-s_move (self): Pass Def-s_update (self): Self.screen. Fill ((0,0,0)) self. S_draw () self. S_load_img () pygame.display.update () def s_load_img (self): Pass def S_draw (self): Pass def M _draw_text (Self,size,text,pos,text_color): #for further use Cur_font=pygame.font.sysfont ("Arial", size) t Ext_fmt=cur_font.render (Text,1,text_color) Self.screen.blit (Text_fmt,pos) def m_mouse_pos (self): return P Ygame.mouse.get_pos () def m_load_img (self,img_file): Return Pygame.image.load (img_file) if __name__== "__main__": D=draw () D.mainloop ()
2.sort_show.py
Here is the main class of the dynamic display sort, written in sort_show.py
Import pygamefrom pygame.locals import *from Draw import Drawclass sortshow (Draw):d EF __init__ (self,lst,lst_change):D Raw.__init__ (self) self.lst=lstself.lst_change=lst_changeself.len=len (LST) Self.max=max (LST) self.step= 0self.whole_step=len (Lst_change) self.time_delay=100#msself.stop_flag=truedef S_draw (self):p ygame.time.delay ( Self.time_delay) Self._change_lst () Self._cal_rect () Self._draw_rect () Self._put_text () def _change_lst (self): if not Self.stop_flag:if Self.step<self.whole_step:changing=self.lst_change[self.step]i=changing[0]j=changing[1] Flag=changing[2]if flag==1:self.lst[i],self.lst[j]=self.lst[j],self.lst[i]if Flag==2:key=self.lst[j]del self.lst[ J]self.lst[i+1:i+1]=[key] #move keyself.step=self.step+1def _cal_rect (self): Self.rects=[]x_div=720.0/self.leny_ Div=480.0/self.max/1.5for i in range (Self.len): width=x_div-1height=self.lst[i]*y_divlength=i*x_divtop=480- Heightrect=[length,top,width,height]self.rects+=[rect]changing=self.lst_change[self.step-1]p1=changing[0]p2= Changing[1]for I In [P1,p2]:width=x_div-1height=self.lst[i]*y_divlength=i*x_divtop=480-heightrect=[length,top,width,height] Self.rects+=[rect]def _draw_rect (self): to rect in self.rects[:-2]:p ygame.draw.rect (Self.screen, (255,255,255), rect , 0) #rect: left,top,width,heightif self.step<self.whole_step:for rect in self.rects[-2:]:p ygame.draw.rect ( Self.screen, (0,0,255), rect,0) def _put_text (self): self. M_draw_text ("1.press ESC to Quit", (20,20), (0xff,0xff,0x0)) self. M_draw_text ("2.press T to set Delay_time", (20,40), (0xff,0xff,0x0))-Self. M_draw_text ("3.press s to start or stop", (20,60), (0xff,0xff,0x0)) def s_keydown_event (Self,key): if Key==k_escape: Exit () if Key==k_t:self._set_time_delay () if Key==k_s:self.stop_flag=not (Self.stop_flag) def _set_time_delay (self): Try:print ("-*-delay_time set-*-") Time_delay=int (Input ("Please input delay Time\ndefalut Vlaue ' 100ms\ntime (ms):")) Self.time_delay=time_delayfinally:print ("-*-finish-*-")
3.sort.py
Next, we write bubble sort in sort.py (can also write other sort), here the sort to output two elements of each exchange subscript
Import randomdef ge (min_value,max_value):d EF tmp (num): Lst=[]for i in range (num): Value=random.randint (min_value,max_ Value) lst.append (value) return Lstreturn tmpdef bubble_sort (LST): Lst1=lst[:]res=[]num=len (Lst1) for I in Range (num-1): #from 1 to Numfor J in Range (num-1,i,-1): #from num-1 to I+1tmp=[j-1,j,0]if lst1[j-1]>lst1[j]:lst1[j-1],lst1[j]=lst1[j ],lst1[j-1]tmp[2]=1res.append (TMP) return resif __name__== "__main__": Lst=ge (0,999) (+) Changing=bubble_sort (LST) print (changing)
4.main.py
Finally in main.py, call sort.py and sort_show.py
Import Sortimport sort_showlst=sort.ge (0,999) changing=sort.bubble_sort (LST) s=sort_show. Sortshow (lst,changing) S.mainloop ()
Run the main.py to get the effect of the beginning of the article.
Dynamic display of [Python] sorting