arikSarkar

QuickSort

Sep 23rd, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. '''
  2. QUICK SORT
  3. 1.  pick the first element of array arr[0] as pivot
  4. 2.  qsort those elements of array which are less than pivot with List Comprehension
  5.    => qsort([x for x in arr[1:] if x<arr[0]])
  6.    
  7. 3.  qsort those elements of array which are larger than pivot with List Comprehension
  8.    => qsort([x for x in arr[1:] if x>=arr[0]])
  9. '''
  10.  
  11. def qsort(arr):
  12.      if len(arr) <= 1:
  13.           return arr
  14.      else:
  15.           return qsort([x for x in arr[1:] if x<arr[0]]) + [arr[0]] + qsort([x for x in arr[1:] if x>=arr[0]])
  16.  
  17.  
  18. print("Enter array elements (e.g, [1 2 3 4 5 6]):\n")
  19. arr = list(map(int, input().split(' ')))
  20. arr = qsort(arr)
  21. print(arr)
Add Comment
Please, Sign In to add comment