ಕ್ವಿಕ್ಸಾರ್ಟ್ ಒಂದು ಮೌಲ್ಯವನ್ನು ಆಧಾರವಾಗಿ ಆಯ್ಕೆ ಮಾಡುತ್ತದೆ. ಇದನ್ನು ಪಿವಟ್ ಎನ್ನುತ್ತಾರೆ. ಅದರ ಆಧಾರದಲ್ಲಿ ಪಟ್ಟಿಯನ್ನು ವಿಭಜಿಸಿ, ಎರಡೂ ಭಾಗಗಳಲ್ಲಿ ಇದೇ ಕೆಲಸವನ್ನು ಮತ್ತೆ ಮಾಡುತ್ತದೆ. ಇಲ್ಲಿ ಯಾವಾಗಲೂ ಮೊದಲ ಮೌಲ್ಯವೇ ಪಿವಟ್. ಎರಡೂ ಭಾಗಗಳು ಸಮತೂಕದಲ್ಲಿದ್ದರೆ ಸಮಯ Θ(n log n). ಪ್ರತಿ ಬಾರಿಯೂ ಅತಿ ಚಿಕ್ಕ ಅಥವಾ ಅತಿ ದೊಡ್ಡ ಮೌಲ್ಯವೇ ಪಿವಟ್ ಆದರೆ ಸಮಯ Θ(n²). ಈ ಆಯ್ಕೆಯಲ್ಲಿ ಮೊದಲೇ ಕ್ರಮದಲ್ಲಿರುವ ಪಟ್ಟಿಗೂ ಹೀಗೆಯೇ ಆಗುತ್ತದೆ.
ಒಂದು ಸರಳ ಉದಾಹರಣೆ
ಪೀಠಾಧಾರವಾಗಿ 2 ಅನ್ನು ಆಯ್ಕೆಮಾಡಿ. 1 ಅನ್ನು ಅದರ ಎಡಕ್ಕೆ ಮತ್ತು 3 ಅನ್ನು ಅದರ ಬಲಕ್ಕೆ ಸರಿಸಿ. ಈಗ ಪ್ರತಿ ಬದಿಯಲ್ಲಿ ಒಂದು ಐಟಂ ಇದೆ.
ಆರಂಭದಲ್ಲಿ3 1 2
ಫಲಿತಾಂಶ1 2 3
lesson.rb
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
end
i += 1
end
values[2] = values[boundary]
values[boundary] = pivot
i = 0
while i < 3
puts values[i]
i += 1
end
ಫಲಿತಾಂಶ
1
2
3
ದಿನನಿತ್ಯದ ಉದಾಹರಣೆಗಳು
ಒಂದು ಪಾರ್ಸೆಲ್ನ ತೂಕಕ್ಕೆ ಹೋಲಿಸಿ ಉಳಿದವನ್ನು ಬೇರ್ಪಡಿಸುವುದು
ಮೂರು ಪಾರ್ಸೆಲ್ಗಳ ತೂಕ ಕ್ರಮವಾಗಿ 8, 2 ಮತ್ತು 5 ಕಿಲೋ. 5 ಕಿಲೋವನ್ನು ಆಧಾರವಾಗಿ ತೆಗೆದುಕೊಳ್ಳಿ. ಯಾವ ಪಾರ್ಸೆಲ್ ಹಗುರವಾದ ಗುಂಪಿಗೆ, ಯಾವುದು ಭಾರವಾದ ಗುಂಪಿಗೆ ಸೇರುತ್ತದೆ?
ಆರಂಭದಲ್ಲಿ8 2 5
ಫಲಿತಾಂಶ2 • 5 • 8
ಎರಡು ಹಗುರವಾಗಿದೆ ಮತ್ತು ಎಂಟು ಭಾರವಾಗಿದೆ. ಆಧಾರವು ಈಗ ಎರಡು ಗುಂಪುಗಳನ್ನು ಬೇರ್ಪಡಿಸುತ್ತದೆ.
ಒಂದೇ ಬೆಲೆಯ ಪುಸ್ತಕಗಳನ್ನು ಜೋಡಿಸುವುದು
ಪ್ರತಿ ಪುಸ್ತಕದ ಬೆಲೆ 4. 3-ಮಾರ್ಗದ ವಿಭಜನೆಯು ಏನು ಉತ್ಪಾದಿಸುತ್ತದೆ?
ಆರಂಭದಲ್ಲಿ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²)
Ruby ಭಾಷೆಯಲ್ಲಿ ಕ್ವಿಕ್ ಸಾರ್ಟ್
Ruby ಮೂಲ ಕೋಡ್
class QuickSortExample
def quickSort(arr, low, high)
if(low < high)
pivot = partition(arr, low, high)
quickSort(arr, low, pivot - 1)
quickSort(arr, pivot + 1, high)
end
end
def partition(arr, low, high)
pivot = arr[low]
i = low
j = high
while(i < j)
while arr[i].to_i <= pivot.to_i and i < high
i += 1
end
while arr[j].to_i > pivot.to_i and j > low
j -= 1
end
if(i < j)
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
end
end
arr[low] = arr[j]
arr[j] = pivot
return j
end
def printArray(arr)
for i in 0...arr.length do
print(arr[i].to_s + " ")
end
puts("")
end
end
obj = QuickSortExample.new
arr = [15, 3, 12, 6, -9, 9, 0]
print("Before Sorting: ")
obj.printArray(arr)
obj.quickSort(arr, 0, arr.length - 1)
print("After Sorting: ")
obj.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] ಇಂದ ಆರಂಭಿಸಿ.
ಮೊದಲ ಮೌಲ್ಯವಾದ ೧೫ನ್ನು ಆಧಾರವಾಗಿ ಬಳಸಿ. ಎರಡೂ ತುದಿಗಳಿಂದ ಒಳಮುಖವಾಗಿ ಸ್ಕ್ಯಾನ್ ಮಾಡಿ.
ಎಡ ಸೂಚ್ಯಂಕವನ್ನು ಬಲಕ್ಕೆ ಸರಿಸಿ ಅದರ ಮೌಲ್ಯವು ಆಧಾರಕ್ಕಿಂತ ಕಡಿಮೆ ಅಥವಾ ಸಮನಾಗಿದ್ದರೆ ಮತ್ತು ಸೂಚ್ಯಂಕವು ಮೇಲಿನ ಮಿತಿಗಿಂತ ಕೆಳಗಿದ್ದರೆ. ಬಲ ಸೂಚ್ಯಂಕವನ್ನು ಎಡಕ್ಕೆ ಸರಿಸಿ ಅದರ ಮೌಲ್ಯವು ಆಧಾರಕ್ಕಿಂತ ಹೆಚ್ಚಿದ್ದರೆ.
ನೋವಿನ ಲಕ್ಷಣಗಳು ಸಿಗದೇ ಹೋದಲ್ಲಿ, ಆ ಎರಡು ತಪ್ಪಾದ ಮೌಲ್ಯಗಳನ್ನು ಬದಲಾಯಿಸಿ, ಇನ್ನೂ ಹುಡುಕುತ್ತಾ ಹೋಗುತ್ತವೆ.
ಸೂಚ್ಯಂಕಗಳು ಸಂಧಿಸಿದಾಗ ಅಥವಾ ದಾಟಿದಾಗ, ಮೊದಲ ಮೌಲ್ಯವನ್ನು ಬಲ ಸೂಚ್ಯಂಕದಲ್ಲಿರುವ ಮೌಲ್ಯದೊಂದಿಗೆ ವಿನಿಮಯ ಮಾಡಿ. ಆಧಾರವು ಈಗ ಅದರ ಅಂತಿಮ ಸ್ಥಾನದಲ್ಲಿದೆ.
ಆ ಸ್ಥಾನದ ಮೊದಲು ಮತ್ತು ನಂತರದ ಶ್ರೇಣಿಗಳನ್ನು ಪುನರಾವರ್ತಿತವಾಗಿ ವಿಂಗಡಿಸಿ. ಎರಡಕ್ಕಿಂತ ಕಡಿಮೆ ಅಂಶಗಳನ್ನು ಹೊಂದಿರುವ ಶ್ರೇಣಿಯನ್ನು ಈಗಾಗಲೇ ವಿಂಗಡಿಸಲಾಗಿದೆ.
Ruby ಭಾಷೆಯಲ್ಲಿ ಕ್ವಿಕ್ ಸಾರ್ಟ್ ಹಂತ ಹಂತವಾಗಿ
Ruby ಮೂಲ ಕೋಡ್
class QuickSortExample
def quickSort(arr, low, high)
if(low < high)
# Find the pivot index of the lower bound of an array
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)
end
end
def partition(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 of "i"
# till the left most values should be less than or equal to the pivot value
while arr[i].to_i <= pivot.to_i and i < high
i += 1
end
# Decrement the index value of "j"
# till the right most values should be greater than to the pivot value
while arr[j].to_i > pivot.to_i and j > low
j -= 1
end
# 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
end
end
# 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
end
def printArray(arr)
# Loop the item and print each item in the console with white space seperated
for i in 0...arr.length do
print(arr[i].to_s + " ")
end
puts("")
end
end
obj = QuickSortExample.new
arr = [15, 3, 12, 6, -9, 9, 0]
print("Before Sorting: ")
obj.printArray(arr)
obj.quickSort(arr, 0, arr.length - 1)
print("After Sorting: ")
obj.printArray(arr)
ಫಲಿತಾಂಶ
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15