+ 1
How to choose pivot element in quick sort ? if i took pivot = sequence[0] it gives recursion error
def quick_sort(sequence): length = len(sequence) if len(sequence) <= 1: return sequence else: pivot = sequence.pop() item_greater = [] item_lower = [] for item in sequence: if item > pivot: item_greater.append(item) else: item_lower.append(item) return quick_sort(item_lower) + [pivot] + quick_sort(item_greater) print(quick_sort([1,6,8,3,5,9,0])) my_output :- y [0, 1, 3, 5, 6, 8, 9] desired output :- [0, 1, 3, 5, 6, 8, 9] # if i choose pivot as pivot = sequence[0] it's generates recursion error # and what is this <'y' > and why it shows in my output ??
1 Réponse
- 1
Very simply code:
array = [1,6,8,3,5,9,0]
array.sort() #Yes, it is THIS easy!
print(array)
Too easy? Maybe 🤣🤣🤣
Happy programming! :)