क्विकसॉर्ट आधार म्हणून एक मूल्य निवडतो. त्याला पिवट म्हणतात. त्यानुसार यादी विभागून प्रत्येक भागावर हेच काम पुन्हा करतो. इथे नेहमी पहिलंच मूल्य पिवट असतं. दोन्ही भाग साधारण सारखे असतील तर वेळ Θ(n log n) असतो. दर वेळी सर्वांत लहान किंवा सर्वांत मोठं मूल्य पिवट निघालं तर वेळ Θ(n²) होतो. या नियमानुसार आधीच क्रमाने लावलेल्या यादीतही हेच घडतं.
एक सोपे उदाहरण
केंद्रबिंदू म्हणून 2 निवडा. 1 त्याच्या डावीकडे आणि 3 त्याच्या उजवीकडे हलवा. प्रत्येक बाजूला आता एक वस्तू आहे.
सुरुवातीला3 1 2
निकाल1 2 3
lesson.py
values = [3, 1, 2]
pivot = values[2]
boundary = 0
i = 0
while i < 2:
if values[i] < pivot:
saved = values[i]
values[i] = values[boundary]
values[boundary] = saved
boundary += 1
i += 1
values[2] = values[boundary]
values[boundary] = pivot
i = 0
while i < 3:
print(values[i])
i += 1
आउटपुट
1
2
3
रोजच्या आयुष्यातील उदाहरणे
एका पार्सलच्या वजनाशी तुलना करून इतर पार्सलांचे गट करा
तीन पार्सलची वजनं अनुक्रमे 8, 2 आणि 5 किलो आहेत. 5 किलो हा आधार धरा. कोणतं पार्सल हलक्या गटात आणि कोणतं जड गटात जाईल?
सुरुवातीला8 2 5
निकाल2 • 5 • 8
दोन हलके आणि आठ जड असतात. केंद्रबिंदू आता दोन गटांना वेगळे करतो.
समान किमतीची पुस्तकं क्रमाने लावा
प्रत्येक पुस्तकाची किंमत 4 आहे. त्रिकोणी विभाजनामुळे काय होईल?
सुरुवातीला4 4 4
निकाल∅ • 4 4 4 • ∅
समान गटात प्रत्येक पुस्तक असते. कोणत्याही बाह्य गटाला जास्त काम करण्याची गरज नाही.
चांगली विभागणी न करणारा पिव्हट ओळखा
तारखा 1,2,3 आणि 4 क्रमाने आहेत. नेहमी शेवटची तारीख निवडा. पहिल्या विभाजनात काय होते?
सुरुवातीला1 2 3 4
निकाल1 2 3 • 4 • ∅
एका गटाकडे अजूनही तीन वस्तू असतात. या नमुन्याची पुनरावृत्ती केल्याने तुलना वाया जाते.
लागणारे काम आणि मर्यादा
प्रत्येक वेळी साधारण समान भाग पडले तर बहुतेकदा O(n log n) काम पुरतं. सतत सर्वांत लहान किंवा सर्वांत मोठं मूल्य पिव्हट घेतल्यास O(n²) काम लागू शकतं.
Before Sorting:
15 3 12 6 -9 9 0
After Sorting:
-9 0 3 6 9 12 15
जाहिराती
उदाहरण टप्प्याटप्प्याने पाहा
[15, 3, 12, 6, -9, 9, 0] पासून सुरुवात करा.
पहिलं मूल्य 15 हे पिवट ठेवा. दोन्ही टोकांकडून आतल्या दिशेने तपासत जा.
डावा इंडेक्स वरच्या मर्यादेपेक्षा लहान आहे आणि तिथलं मूल्य पिवटपेक्षा लहान किंवा त्याएवढं आहे, तोपर्यंत तो उजवीकडे सरकवा. उजव्या इंडेक्सवरचं मूल्य पिवटपेक्षा मोठं आहे तोपर्यंत तो इंडेक्स डावीकडे सरकवा.
दोन्ही इंडेक्स अजून एकमेकांपर्यंत पोहोचले नसतील तर चुकीच्या बाजूला असलेल्या दोन मूल्यांच्या जागा बदला. मग तपासणी पुढे चालू ठेवा.
दोन्ही इंडेक्स एकाच जागी आले किंवा एकमेकांना ओलांडून गेले की पहिलं मूल्य आणि उजव्या इंडेक्सवरचं मूल्य यांच्या जागा बदला. आता पिवट त्याच्या अंतिम जागी आहे.
त्या जागेच्या आधीच्या आणि नंतरच्या भागांवर हीच पद्धत पुन्हा वापरा. दोनपेक्षा कमी घटक असलेला भाग आधीच क्रमात असतो.
Python मध्ये क्विक सॉर्ट टप्प्याटप्प्याने समजून घ्या
Python सोर्स कोड
class QuickSortExample:
def __init__(self):
pass
def quickSort(self, arr, low, high):
if(low < high):
# Find the pivot index of the lower bound of an array
pivot = self.partition(arr, low, high)
# Apply Divide and conquer strategy
# to sort the left side and right side of an array
# respective to the pivot position
# Left hand side array
self.quickSort(arr, low, pivot - 1)
#Right hand side array
self.quickSort(arr, pivot + 1, high)
return arr
def partition(self, arr, low, high):
# Pick a lowest bound element as an pivot value
pivot = arr[low]
i = low
j = high
while(i < j):
# Increment the index value (i.e. i )
# till the piovt value is greater than or equal the left most values
while(arr[i] <= pivot and i < high):
i += 1
# Increment the index value (i.e. j )
# till the piovt value is less than the right most values
while(arr[j] > pivot and j > low):
j -= 1
# Interchange the values of present
# in the index i and j of an array only if i is less than j
if(i < j):
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
# Interchange the element in j's poisition to the lower bound of an array
# and place the Pivot element to the j's position
arr[low] = arr[j]
arr[j] = pivot
return j
def printArray(self, arr):
# Loop the item and print each item in the console with white space seperated
string = " ".join([str(item) for item in arr])
print(string)
obj = QuickSortExample()
arr = [15, 3, 12, 6, -9, 9, 0]
print("Before Sorting: ")
obj.printArray(arr)
result = obj.quickSort(arr, 0, len(arr) - 1)
print("After Sorting: ")
obj.printArray(result)
आउटपुट
Before Sorting:
15 3 12 6 -9 9 0
After Sorting:
-9 0 3 6 9 12 15