ಕ್ವಿಕ್ಸಾರ್ಟ್ ಒಂದು ಮೌಲ್ಯವನ್ನು ಆಧಾರವಾಗಿ ಆಯ್ಕೆ ಮಾಡುತ್ತದೆ. ಇದನ್ನು ಪಿವಟ್ ಎನ್ನುತ್ತಾರೆ. ಅದರ ಆಧಾರದಲ್ಲಿ ಪಟ್ಟಿಯನ್ನು ವಿಭಜಿಸಿ, ಎರಡೂ ಭಾಗಗಳಲ್ಲಿ ಇದೇ ಕೆಲಸವನ್ನು ಮತ್ತೆ ಮಾಡುತ್ತದೆ. ಇಲ್ಲಿ ಯಾವಾಗಲೂ ಮೊದಲ ಮೌಲ್ಯವೇ ಪಿವಟ್. ಎರಡೂ ಭಾಗಗಳು ಸಮತೂಕದಲ್ಲಿದ್ದರೆ ಸಮಯ Θ(n log n). ಪ್ರತಿ ಬಾರಿಯೂ ಅತಿ ಚಿಕ್ಕ ಅಥವಾ ಅತಿ ದೊಡ್ಡ ಮೌಲ್ಯವೇ ಪಿವಟ್ ಆದರೆ ಸಮಯ Θ(n²). ಈ ಆಯ್ಕೆಯಲ್ಲಿ ಮೊದಲೇ ಕ್ರಮದಲ್ಲಿರುವ ಪಟ್ಟಿಗೂ ಹೀಗೆಯೇ ಆಗುತ್ತದೆ.
ಒಂದು ಸರಳ ಉದಾಹರಣೆ
ಪೀಠಾಧಾರವಾಗಿ 2 ಅನ್ನು ಆಯ್ಕೆಮಾಡಿ. 1 ಅನ್ನು ಅದರ ಎಡಕ್ಕೆ ಮತ್ತು 3 ಅನ್ನು ಅದರ ಬಲಕ್ಕೆ ಸರಿಸಿ. ಈಗ ಪ್ರತಿ ಬದಿಯಲ್ಲಿ ಒಂದು ಐಟಂ ಇದೆ.
ಆರಂಭದಲ್ಲಿ3 1 2
ಫಲಿತಾಂಶ1 2 3
lesson.py
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
i += 1
values[2] = values[boundary]
values[boundary] = pivot
i = 0
while i < 3:
print(values[i])
i += 1
ಫಲಿತಾಂಶ
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²) ಕೆಲಸ ಬೇಕಾಗಬಹುದು.
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] ಇಂದ ಆರಂಭಿಸಿ.
ಮೊದಲ ಮೌಲ್ಯವಾದ ೧೫ನ್ನು ಆಧಾರವಾಗಿ ಬಳಸಿ. ಎರಡೂ ತುದಿಗಳಿಂದ ಒಳಮುಖವಾಗಿ ಸ್ಕ್ಯಾನ್ ಮಾಡಿ.
ಎಡ ಸೂಚ್ಯಂಕವನ್ನು ಬಲಕ್ಕೆ ಸರಿಸಿ ಅದರ ಮೌಲ್ಯವು ಆಧಾರಕ್ಕಿಂತ ಕಡಿಮೆ ಅಥವಾ ಸಮನಾಗಿದ್ದರೆ ಮತ್ತು ಸೂಚ್ಯಂಕವು ಮೇಲಿನ ಮಿತಿಗಿಂತ ಕೆಳಗಿದ್ದರೆ. ಬಲ ಸೂಚ್ಯಂಕವನ್ನು ಎಡಕ್ಕೆ ಸರಿಸಿ ಅದರ ಮೌಲ್ಯವು ಆಧಾರಕ್ಕಿಂತ ಹೆಚ್ಚಿದ್ದರೆ.
ನೋವಿನ ಲಕ್ಷಣಗಳು ಸಿಗದೇ ಹೋದಲ್ಲಿ, ಆ ಎರಡು ತಪ್ಪಾದ ಮೌಲ್ಯಗಳನ್ನು ಬದಲಾಯಿಸಿ, ಇನ್ನೂ ಹುಡುಕುತ್ತಾ ಹೋಗುತ್ತವೆ.
ಸೂಚ್ಯಂಕಗಳು ಸಂಧಿಸಿದಾಗ ಅಥವಾ ದಾಟಿದಾಗ, ಮೊದಲ ಮೌಲ್ಯವನ್ನು ಬಲ ಸೂಚ್ಯಂಕದಲ್ಲಿರುವ ಮೌಲ್ಯದೊಂದಿಗೆ ವಿನಿಮಯ ಮಾಡಿ. ಆಧಾರವು ಈಗ ಅದರ ಅಂತಿಮ ಸ್ಥಾನದಲ್ಲಿದೆ.
ಆ ಸ್ಥಾನದ ಮೊದಲು ಮತ್ತು ನಂತರದ ಶ್ರೇಣಿಗಳನ್ನು ಪುನರಾವರ್ತಿತವಾಗಿ ವಿಂಗಡಿಸಿ. ಎರಡಕ್ಕಿಂತ ಕಡಿಮೆ ಅಂಶಗಳನ್ನು ಹೊಂದಿರುವ ಶ್ರೇಣಿಯನ್ನು ಈಗಾಗಲೇ ವಿಂಗಡಿಸಲಾಗಿದೆ.
Python ಭಾಷೆಯಲ್ಲಿ ಕ್ವಿಕ್ ಸಾರ್ಟ್ ಹಂತ ಹಂತವಾಗಿ
Python ಮೂಲ ಕೋಡ್
class QuickSortExample:
def __init__(self):
pass
def quickSort(self, arr, low, high):
if(low < high):
# Find the pivot index of the lower bound of an array
pivot = self.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
self.quickSort(arr, low, pivot - 1)
#Right hand side array
self.quickSort(arr, pivot + 1, high)
return arr
def partition(self, 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 (i.e. i )
# till the piovt value is greater than or equal the left most values
while(arr[i] <= pivot and i < high):
i += 1
# Increment the index value (i.e. j )
# till the piovt value is less than the right most values
while(arr[j] > pivot and j > low):
j -= 1
# 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
# 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
def printArray(self, arr):
# Loop the item and print each item in the console with white space seperated
string = " ".join([str(item) for item in arr])
print(string)
obj = QuickSortExample()
arr = [15, 3, 12, 6, -9, 9, 0]
print("Before Sorting: ")
obj.printArray(arr)
result = obj.quickSort(arr, 0, len(arr) - 1)
print("After Sorting: ")
obj.printArray(result)
ಫಲಿತಾಂಶ
Before Sorting:
15 3 12 6 -9 9 0
After Sorting:
-9 0 3 6 9 12 15