क्विकसॉर्ट आधार म्हणून एक मूल्य निवडतो. त्याला पिवट म्हणतात. त्यानुसार यादी विभागून प्रत्येक भागावर हेच काम पुन्हा करतो. इथे नेहमी पहिलंच मूल्य पिवट असतं. दोन्ही भाग साधारण सारखे असतील तर वेळ Θ(n log n) असतो. दर वेळी सर्वांत लहान किंवा सर्वांत मोठं मूल्य पिवट निघालं तर वेळ Θ(n²) होतो. या नियमानुसार आधीच क्रमाने लावलेल्या यादीतही हेच घडतं.
एक सोपे उदाहरण
केंद्रबिंदू म्हणून 2 निवडा. 1 त्याच्या डावीकडे आणि 3 त्याच्या उजवीकडे हलवा. प्रत्येक बाजूला आता एक वस्तू आहे.
सुरुवातीला3 1 2
निकाल1 2 3
Lesson.kt
fun main() {
var values = intArrayOf(3, 1, 2)
var pivot = values[2]
var boundary = 0
var i = 0
while (i < 2) {
if (values[i] < pivot) {
var 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) {
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²)
Kotlin मध्ये क्विक सॉर्ट
Kotlin सोर्स कोड
fun quickSort(arr: IntArray, low: Int, high: Int) {
if(low < high) {
val pivot: Int = partition(arr, low, high)
quickSort(arr, low, pivot - 1)
quickSort(arr, pivot + 1, high)
}
}
fun partition(arr: IntArray, low: Int, high: Int): Int {
val pivot = arr[low]
var i: Int = low
var j: Int = high
while(i < j) {
while(arr[i] <= pivot && i < high) {
i++
}
while(arr[j] > pivot && j > low) {
j--
}
if(i < j) {
val temp: Int = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
}
arr[low] = arr[j]
arr[j] = pivot
return j
}
fun printArray(arr: IntArray) {
val lastIndex: Int = arr.size - 1
for (i in 0..lastIndex) {
val num = arr[i]
print("$num ")
}
println("")
}
fun main(args: Array<String>) {
val arr: IntArray = intArrayOf(15, 3, 12, 6, -9, 9, 0)
print("Before Sorting: ")
printArray(arr)
quickSort(arr, 0, arr.size - 1)
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 हे पिवट ठेवा. दोन्ही टोकांकडून आतल्या दिशेने तपासत जा.
डावा इंडेक्स वरच्या मर्यादेपेक्षा लहान आहे आणि तिथलं मूल्य पिवटपेक्षा लहान किंवा त्याएवढं आहे, तोपर्यंत तो उजवीकडे सरकवा. उजव्या इंडेक्सवरचं मूल्य पिवटपेक्षा मोठं आहे तोपर्यंत तो इंडेक्स डावीकडे सरकवा.
दोन्ही इंडेक्स अजून एकमेकांपर्यंत पोहोचले नसतील तर चुकीच्या बाजूला असलेल्या दोन मूल्यांच्या जागा बदला. मग तपासणी पुढे चालू ठेवा.
दोन्ही इंडेक्स एकाच जागी आले किंवा एकमेकांना ओलांडून गेले की पहिलं मूल्य आणि उजव्या इंडेक्सवरचं मूल्य यांच्या जागा बदला. आता पिवट त्याच्या अंतिम जागी आहे.
त्या जागेच्या आधीच्या आणि नंतरच्या भागांवर हीच पद्धत पुन्हा वापरा. दोनपेक्षा कमी घटक असलेला भाग आधीच क्रमात असतो.
Kotlin मध्ये क्विक सॉर्ट टप्प्याटप्प्याने समजून घ्या
Kotlin सोर्स कोड
fun quickSort(arr: IntArray, low: Int, high: Int) {
if(low < high) {
// Find the pivot index of the lower bound of an array
val pivot: Int = 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)
}
}
fun partition(arr: IntArray, low: Int, high: Int): Int {
// Pick a lowest bound element as an pivot value
val pivot = arr[low]
var i: Int = low
var j: Int = high
while(i < j) {
// Increment the index value (i.e. i )
// till the left most values should be less than or equal to the pivot value
while(arr[i] <= pivot && i < high) {
i++
}
// Increment the index value (i.e. j )
// till the right most values should be greater than to the pivot value
while(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) {
val temp: Int = 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
}
fun printArray(arr: IntArray) {
val lastIndex: Int = arr.size - 1
// Loop the item and print each item in the console with white space seperated
for (i in 0..lastIndex) {
val num = arr[i]
print("$num ")
}
println("")
}
fun main(args: Array<String>) {
val arr: IntArray = intArrayOf(15, 3, 12, 6, -9, 9, 0)
print("Before Sorting: ")
printArray(arr)
quickSort(arr, 0, arr.size - 1)
print("After Sorting: ")
printArray(arr)
}
आउटपुट
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15