क्विकसॉर्ट आधार म्हणून एक मूल्य निवडतो. त्याला पिवट म्हणतात. त्यानुसार यादी विभागून प्रत्येक भागावर हेच काम पुन्हा करतो. इथे नेहमी पहिलंच मूल्य पिवट असतं. दोन्ही भाग साधारण सारखे असतील तर वेळ Θ(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