ക്വിക്സോർട്ട് ഒരു മൂല്യത്തെ അടിസ്ഥാനമായി തിരഞ്ഞെടുക്കുന്നു. ഇതാണ് പിവറ്റ്. അതിന്റെ അടിസ്ഥാനത്തിൽ പട്ടികയെ രണ്ടായി തിരിച്ച് ഓരോ ഭാഗത്തും ഇതേ ജോലി ആവർത്തിക്കും. ഇവിടെ എപ്പോഴും ആദ്യ മൂല്യമാണ് പിവറ്റ്. രണ്ടു ഭാഗങ്ങളും ഏകദേശം ഒരേ വലുപ്പമാണെങ്കിൽ സമയം Θ(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