Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. def counting_s(minval, maxval, array):
  2. ans = [0] * (maxval - minval + 1)
  3. for i in array:
  4. ans[i-minval] = ans[i-minval] + 1
  5. result = []
  6. for j in range(maxval - minval + 1):
  7. result += [j + minval] * ans[j]
  8. return result
  9.  
  10.  
  11. def hs(aList):
  12. length = len(aList) - 1
  13. leastParent = round(length / 2)
  14. for i in range(leastParent, -1, -1):
  15. moveDown(aList, i, length)
  16. for i in range(length, 0, -1):
  17. if aList[0] > aList[i]:
  18. swap(aList, 0, i)
  19. moveDown(aList, 0, i - 1)
  20.  
  21.  
  22. def moveDown(aList, first, last):
  23. largest = 2 * first + 1
  24. while largest <= last:
  25. if (largest < last) and (aList[largest] < alist[largest + 1]):
  26. largest += 1
  27. if aList[largest] > aList[first]:
  28. swap(aList, largest, first)
  29. first = largest
  30. largest = 2 * first + 1
  31. else:
  32. return
  33.  
  34.  
  35. def swap(A, x, y):
  36. A[x], A[y] = A[y], A[x]
  37.  
  38. worklist = []
  39. rn = input().split()
  40. minval = int(rn[0])
  41. maxval = int(rn[1])
  42. while True:
  43. try:
  44. for el in input().split():
  45. worklist.append(int(el))
  46. except:
  47. break
  48. if len(worklist) / (maxval - minval) > 0.05:
  49. worklist = counting_s(minval, maxval, worklist)
  50. else:
  51. hs(worklist)
  52.  
  53. print(' '.join([str(c) for c in worklist]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement