क्विकसॉर्ट एक मान को आधार बनाता है, जिसे पिवट कहते हैं। उसके हिसाब से सूची को बाँटकर दोनों हिस्सों पर यही तरीका दोहराता है। यहाँ पहला मान हमेशा पिवट है। हिस्से लगभग बराबर हों तो समय Θ(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