Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. def msort(lst):
  2. if not lst:
  3. return []
  4. if len(lst) == 1:
  5. return lst
  6. else:
  7. first_half = lst[0:len(lst)/2]
  8. second_half = lst[len(lst)/2:]
  9. return merge(msort(first_half), msort(second_half))
  10.  
  11. def merge(lsta, lstb):
  12. """
  13. loop over lsta and lstb to create sorted array
  14. """
  15. new_lst = []
  16. current_a_index = 0
  17. current_b_index = 0
  18.  
  19. print "lsta: ", lsta
  20. print "lstb: ", lstb
  21.  
  22. while len(new_lst) != (len(lsta) + len(lstb)):
  23. if len(lsta) == current_a_index:
  24. new_lst.append(lstb[current_b_index])
  25. current_b_index += 1
  26. elif len(lstb) == current_b_index:
  27. new_lst.append(lsta[current_a_index])
  28. current_a_index += 1
  29. elif lsta[current_a_index] < lstb[current_b_index]:
  30. new_lst.append(lsta[current_a_index])
  31. current_a_index += 1
  32. elif lsta[current_a_index] > lstb[current_b_index]:
  33. new_lst.append(lstb[current_b_index])
  34. current_b_index += 1
  35.  
  36. print "new list: ", new_lst
  37. return new_lst
  38.  
  39. ques = [4,3,8,6,7,1,2,9,5]
  40. sol = [1,2,3,4,5,6,7,8,9]
  41. mysol = msort(ques)
  42.  
  43. print ""
  44. print "ques: ", ques
  45. print "sol: ", sol
  46. print "mysol: ", mysol
  47. print "correct: ", sol == mysol
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement