m2skills

qs python

Sep 10th, 2017
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. # program to implement quick sort in python
  2.  
  3. def quick_Sort(data, first, last):
  4.  
  5.     # enter the block only if the number of elements is greater than 0
  6.     if first < last:
  7.         pivot = first
  8.         left_mark = first
  9.         right_mark = last
  10.        
  11.         #
  12.         while left_mark < right_mark:
  13.  
  14.             # while the data at the left mark is less than pivot keep incrementing the left_mark
  15.             while data[left_mark] <= data[pivot] and left_mark < right_mark:
  16.                 left_mark += 1
  17.  
  18.             # while the data at the right mark is greater than pivot keep decrementing the right_mark
  19.             while data[right_mark] >= data[pivot] and left_mark <= right_mark:
  20.                 right_mark -= 1
  21.  
  22.             # swap if right_mark is greater than left_mark
  23.             if right_mark > left_mark:
  24.                 data[left_mark], data[right_mark] = data[right_mark], data[left_mark]
  25.  
  26.  
  27.             # finally swap the pivot with the right_mark
  28.             data[pivot], data[right_mark] = data[right_mark], data[pivot]
  29.  
  30.  
  31.         # make the recursive calls
  32.         quick_Sort(data, first, right_mark-1)
  33.        
  34.         quick_Sort(data, right_mark+1, last)
  35.  
  36.  
  37. # #### main function
  38.  
  39. # enter spaced input
  40. myList1 = list(map(int, input("Enter the numbers (space seperated) to be sorted : ").strip().split(' ')))
  41. quick_Sort(myList1, 0, len(myList1) - 1)
  42. print("The sorted list after applying Quick Sort is : ")
  43. print(myList1)
Add Comment
Please, Sign In to add comment