Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # program to implement quick sort in python
- def quick_Sort(data, first, last):
- # enter the block only if the number of elements is greater than 0
- if first < last:
- pivot = first
- left_mark = first
- right_mark = last
- #
- while left_mark < right_mark:
- # while the data at the left mark is less than pivot keep incrementing the left_mark
- while data[left_mark] <= data[pivot] and left_mark < right_mark:
- left_mark += 1
- # while the data at the right mark is greater than pivot keep decrementing the right_mark
- while data[right_mark] >= data[pivot] and left_mark <= right_mark:
- right_mark -= 1
- # swap if right_mark is greater than left_mark
- if right_mark > left_mark:
- data[left_mark], data[right_mark] = data[right_mark], data[left_mark]
- # finally swap the pivot with the right_mark
- data[pivot], data[right_mark] = data[right_mark], data[pivot]
- # make the recursive calls
- quick_Sort(data, first, right_mark-1)
- quick_Sort(data, right_mark+1, last)
- # #### main function
- # enter spaced input
- myList1 = list(map(int, input("Enter the numbers (space seperated) to be sorted : ").strip().split(' ')))
- quick_Sort(myList1, 0, len(myList1) - 1)
- print("The sorted list after applying Quick Sort is : ")
- print(myList1)
Add Comment
Please, Sign In to add comment