Quicksort एल्गोरिदम इम्प्लीमेंट करना
इस अभ्यास में, आप संख्याओं की एक लिस्ट को sort करने के लिए quicksort एल्गोरिदम इम्प्लीमेंट करेंगे.
पहले चरण में, आप partition() फंक्शन इम्प्लीमेंट करेंगे, जो पिवट को प्रोसेस करने के बाद उसका इंडेक्स लौटाता है, ताकि पिवट के बाएँ वाले सभी एलिमेंट पिवट से छोटे हों और पिवट के दाएँ वाले सभी एलिमेंट पिवट से बड़े हों.
दूसरे चरण में, आप quicksort() फंक्शन इम्प्लीमेंट करेंगे, जो partition() फंक्शन को कॉल करेगा.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Data Structures और Algorithms
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
def partition(my_list, first_index, last_index):
pivot = my_list[first_index]
left_pointer = first_index + 1
right_pointer = last_index
while True:
# Iterate until the value pointed by left_pointer is greater than pivot or left_pointer is greater than last_index
while ____ < ____ and ____ < ____:
left_pointer += 1
while my_list[right_pointer] > pivot and right_pointer >= first_index:
right_pointer -= 1
if left_pointer >= right_pointer:
break
# Swap the values for the elements located at the left_pointer and right_pointer
my_list[left_pointer], my_list[right_pointer] = ____, ____
my_list[first_index], my_list[right_pointer] = my_list[right_pointer], my_list[first_index]
return right_pointer