Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

quicksort implementation

By: a guest on Jul 21st, 2013  |  syntax: Python  |  size: 0.69 KB  |  hits: 33  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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]))