Start learning python, format god horse is the cloud, directly on the data structure of the algorithm. After all, read the code to learn the fastest
1 Accept the input of the Py code, later the input of the algorithm import this file
#!/usr/bin/env python#coding=utf-8# stdininput.pyintsortarrays=[]def stdininput (): sortArray=raw_input (" Please input num array the want sort (use, to split every num): ") sortarrays=sortarray.split (', ') for num in Sortarrays: intnum=-1 try: intnum=int (num) except: print "input num array error, error element was given a default value-1 " intsortarrays.append (Intnum)
Bubble Sort: (This implementation is bubbling from the last, you can choose whether to take a big bubble or a small bubble)
#!/usr/bin/env Python#coding=utf-8#bubblesort.py#user can choose sort Style:desc (1) or ASC (2) Import Sysimport Stdininputdef Bubblesort (Sortarray,style): Sortarraylen=len (Sortarray) whilenum=0 if Style==1:while while Num < sortarraylen-1: for num in xrange (sortarraylen-1,whilenum,-1): If Sortarray[num-1]<s Ortarray[num]: sortarray[num-1],sortarray[num]=sortarray[num],sortarray[num-1] Whilenum=whil Enum+1 Else:while Whilenum < sortarraylen-1: for num in xrange (sortarraylen-1,whilenum,-1): If Sortarray[num-1]>sortarray[num]: Sortarray[num-1],sortarray[num]=sortarray[num],sortarr AY[NUM-1] whilenum=whilenum+1if __name__== ' __main__ ': style=1 try:style=int (sys.argv[1]) exce Pt:print "Input argv error, use default desc sort" Stdininput.stdininput () Bubblesort (Stdininput.intsortarra Ys,style) Print Stdininput.intsoRtarrays