fork download
  1. # The QuickSort function implementation
  2. def quickSort(arr, low, high):
  3. if low < high:
  4. # pi is the partition return index of pivot
  5. # Choose the pivot
  6. pivot = arr[high]
  7.  
  8. # Index of smaller element and indicates
  9. # the right position of pivot found so far
  10. i = low - 1
  11.  
  12. # Traverse arr[low..high] and move all smaller
  13. # elements to the left side. Elements from low to
  14. # i are smaller after every iteration
  15. for j in range(low, high):
  16. if arr[j] < pivot:
  17. i += 1
  18. arr[i], arr[j] = arr[j], arr[i]
  19.  
  20. # Move pivot after smaller elements and
  21. # return its position
  22. arr[i + 1], arr[high] = arr[high], arr[i + 1]
  23. pi = i + 1
  24.  
  25. # Recursion calls for smaller elements
  26. # and greater or equals elements
  27. quickSort(arr, low, pi - 1)
  28. quickSort(arr, pi + 1, high)
  29.  
  30. # Main driver code
  31. if __name__ == "__main__":
  32. arr = [10, 7, 8, 9, 1, 5]
  33. n = len(arr)
  34.  
  35. quickSort(arr, 0, n - 1)
  36.  
  37. for val in arr:
  38. print val,
Success #stdin #stdout 0.02s 6972KB
stdin
Standard input is empty
stdout
1 5 7 8 9 10