Advertisement
Guest User

quicksort implementation

a guest
Jul 21st, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. def qsort2(array):
  2.     array,pI = partition(array)
  3.     return partition(array[:pI])[0]+[array[pI]]+partition(array[pI+1:])[0]
  4.  
  5. def partition(array):
  6.     if len(array) > 1:
  7.         pivot = array[0]
  8.         pivIndex = 0
  9.         i,j = 1,1
  10.         while j < len(array) - 1:
  11.             if array[j] < pivot:
  12.                 temp = array[i]
  13.                 array[i] = array[j]
  14.                 array[j] = temp
  15.                 i += 1
  16.             j += 1
  17.         temp = array[i-1]
  18.         array[i-1] = array[pivIndex]
  19.         array[pivIndex] = temp
  20.         pivIndex = i-1
  21.         return array,pivIndex
  22.     else:
  23.         return array
  24.  
  25. print([3,8,2,5,1,4,7,6])
  26. print(qsort2([3,8,2,5,1,4,7,6]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement