क्विकसॉर्ट एक मान को आधार बनाता है, जिसे पिवट कहते हैं। उसके हिसाब से सूची को बाँटकर दोनों हिस्सों पर यही तरीका दोहराता है। यहाँ पहला मान हमेशा पिवट है। हिस्से लगभग बराबर हों तो समय Θ(n log n) है। पिवट हर बार सबसे छोटा या सबसे बड़ा हो तो समय Θ(n²) तक पहुँच जाता है। इस नियम में पहले से सजी सूची के साथ भी ऐसा होता है।
एक आसान उदाहरण
धुरी के रूप में 2 चुनें। 1 को इसके बाएँ और 3 को इसके दाएँ की ओर ले जाएँ। प्रत्येक पक्ष के पास अब एक वस्तु है।
शुरुआत में3 1 2
नतीजा1 2 3
main.go
package main
import "fmt"
func main() {
values := []int{3, 1, 2}
pivot := values[2]
boundary := 0
i := 0
for 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
for i < 3 {
fmt.Println(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²) काम लग सकता है।
समय की जटिलता
सबसे अच्छा मामला: Θ(n log n)
औसत मामला: Θ(n log n)
सबसे खराब मामला: Θ(n²)
Go में क्विक सॉर्ट
Go का सोर्स कोड
package main
import (
"fmt"
"strconv"
)
func quickSort(arr []int, low, high int) {
if low < high {
var pivot = partition(arr, low, high)
quickSort(arr, low, pivot - 1)
quickSort(arr, pivot + 1, high)
}
}
func partition(arr []int, low, high int) int {
var pivot = arr[low]
var i = low
var j = high
for i < j {
for arr[i] <= pivot && i < high {
i++;
}
for arr[j] > pivot && j > low {
j--
}
if i < j {
var temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
}
arr[low] = arr[j]
arr[j] = pivot
return j
}
func printArray(arr []int) {
for i := 0; i < len(arr); i++ {
fmt.Print(strconv.Itoa(arr[i]) + " ")
}
fmt.Println("")
}
func main() {
var arr = []int { 15, 3, 12, 6, -9, 9, 0 }
fmt.Print("Before Sorting: ")
printArray(arr)
quickSort(arr, 0, len(arr) - 1)
fmt.Print("After Sorting: ")
printArray(arr)
}
आउटपुट
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 का उपयोग करें, जैसा कि पिवोट है। दोनों सिरों से अंदर की ओर स्कैन करें।
जब तक बाईं जगह की वैल्यू पिवट से बड़ी न हो और इंडेक्स ऊपरी सीमा से नीचे हो, बायाँ इंडेक्स आगे बढ़ाएँ। जब तक दाईं जगह की वैल्यू पिवट से बड़ी है, दायाँ इंडेक्स बाईं तरफ खिसकाएँ।
अगर दोनों इंडेक्स अभी नहीं मिले हैं, तो गलत तरफ पड़ी दोनों वैल्यू की जगह बदलें और जाँच जारी रखें।
जब सूचकांक मिलते हैं या पार करते हैं, तो दायें सूचकांक में मूल्य के साथ पहले मूल्य का आदान-प्रदान करें। अब पिवोट अपनी अंतिम स्थिति में है।
उस जगह से पहले और उसके बाद के हिस्सों पर यही छँटाई फिर चलाएँ। जिस हिस्से में दो से कम आइटम हैं, वह पहले से क्रम में है।
Go में क्विक सॉर्ट को कदम-दर-कदम समझें
Go का सोर्स कोड
package main
import (
"fmt"
"strconv"
)
func quickSort(arr []int, low, high int) {
if low < high {
// Find the pivot index of the lower bound of an array
var pivot = 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
quickSort(arr, low, pivot - 1)
// Right hand side array
quickSort(arr, pivot + 1, high)
}
}
func partition(arr []int, low, high int) int {
// Pick a lowest bound element as an pivot value
var pivot = arr[low]
var i = low
var j = high
for i < j {
// Increment the index value of "i"
// till the left most values should be less than or equal to the pivot value
for arr[i] <= pivot && i < high {
i++;
}
// Decrement the index value of "j"
// till the right most values should be greater than to the pivot value
for arr[j] > pivot && j > low {
j--
}
// Interchange the values of present
// in the index i and j of an array only if i is less than j
if i < j {
var 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
// Finally return the appropriate index position of the pivot element
return j
}
func printArray(arr []int) {
// Loop the item and print each item in the console with white space seperated
for i := 0; i < len(arr); i++ {
fmt.Print(strconv.Itoa(arr[i]) + " ")
}
fmt.Println("")
}
func main() {
var arr = []int { 15, 3, 12, 6, -9, 9, 0 }
fmt.Print("Before Sorting: ")
printArray(arr)
quickSort(arr, 0, len(arr) - 1)
fmt.Print("After Sorting: ")
printArray(arr)
}
आउटपुट
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15