Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!
tweet

# quicksort implementation

By: a guest on Jul 21st, 2013  |  syntax: Python  |  size: 0.69 KB  |  views: 35  |  expires: Never
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]))
clone this paste RAW Paste Data
Top