Advertisement
karlicoss

Stable quicksort

Apr 5th, 2012
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. def quicksort(list, left, right):
  2.     if left == right - 1:
  3.         return
  4.     pivot = list[randint(left, right)]
  5.  
  6.     lesser = []
  7.     greater = []
  8.     for i in range(left, right):
  9.         if list[i] < pivot:
  10.             lesser.append(list[i])
  11.         else:
  12.             greater.append(list[i])
  13.  
  14.     for i in range(len(lesser)):
  15.         list[left + i] = lesser[i]
  16.     for i in range(len(greator)):
  17.         list[left + len(lesser) + i] = greater[i]
  18.     # тут освобождаем память, занятую lesser и greater.
  19.     quicksort(list, left, left + len(lesser))
  20.     quicksort(list, left + len(lesser), right)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement