Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. # Python program for implementation of Quicksort Sort
  2.  
  3. # This function takes last element as pivot, places
  4. # the pivot element at its correct position in sorted
  5. # array, and places all smaller (smaller than pivot)
  6. # to left of pivot and all greater elements to right
  7. # of pivot
  8. def partition(arr,low,high):
  9. i = ( low-1 ) # index of smaller element
  10. pivot = arr[high] # pivot
  11.  
  12. for j in range(low , high):
  13.  
  14. # If current element is smaller than or
  15. # equal to pivot
  16. if arr[j] <= pivot:
  17.  
  18. # increment index of smaller element
  19. i = i+1
  20. arr[i],arr[j] = arr[j],arr[i]
  21.  
  22. arr[i+1],arr[high] = arr[high],arr[i+1]
  23. return ( i+1 )
  24.  
  25. # The main function that implements QuickSort
  26. # arr[] --> Array to be sorted,
  27. # low --> Starting index,
  28. # high --> Ending index
  29.  
  30. # Function to do Quick sort
  31. def quickSort(arr,low,high):
  32. # print (arr)
  33. if low < high:
  34.  
  35. # pi is partitioning index, arr[p] is now
  36. # at right place
  37. pi = partition(arr,low,high)
  38.  
  39. # Separately sort elements before
  40. # partition and after partition
  41. quickSort(arr, low, pi-1)
  42. quickSort(arr, pi+1, high)
  43.  
  44.  
  45. # Driver code to test above
  46. arr = [10, 7, 8, 9, 1, 5]
  47. n = len(arr)
  48. quickSort(arr,0,n-1)
  49. print ("Sorted array is:")
  50. for i in range(n):
  51. print (arr[i], end=" ")
  52.  
  53. # This code is contributed by Mohit Kumra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement