Home > other >  To solve the python programming
To solve the python programming

Time:09-27


The Apply mergesort to sort the list E, X, A, M, P, L, E in alphabetical order.

CodePudding user response:

Merge sort method was applied in alphabetical order table will list E, X, A, M, P, L, E sort:
 
Def merge (a, b) :
"'
Merger of two ordered list combined into a new order list and return
A: param a:
B: param:
C: return:
"'
C=[]
H=j=0
While j & lt; Len (a) and h & lt; Len (b) :
If a [j] [j] c.a. ppend (a)
J +=1
The else:
C.a. ppend (b [h])
H +=1

If j==len (a) :
For I in b/h: :
C.a. ppend (I)
The else:
For I in a [j:] :
C.a. ppend (I)
Return c


Def merge_sort (list) :
"'
List first continuously (recursive calls) about classification, until can't divide
Then each recursive call after the results are divided into the merge merge sort operations and return step by step a
: param list:
: return:
"'
If len (list) & lt;=1:
After the return list # cannot be divided starts back step by step a
Middle=int (len (list)/2) # around every time by dividing partition
Left=merge_sort (the list [: middle]) # recursive call
Right=merge_sort (the list [middle:]) # recursive call
Result=merge (left, right) # results after each layer merge sort a
Return the result # and return a layer up


If __name__=="__main__ ':
Letters=[' E ', 'X', 'A', 'M', 'P', 'L', 'E']
Print (merge_sort (letters))


  • Related