ಕ್ವಿಕ್ಸಾರ್ಟ್ ಒಂದು ಮೌಲ್ಯವನ್ನು ಆಧಾರವಾಗಿ ಆಯ್ಕೆ ಮಾಡುತ್ತದೆ. ಇದನ್ನು ಪಿವಟ್ ಎನ್ನುತ್ತಾರೆ. ಅದರ ಆಧಾರದಲ್ಲಿ ಪಟ್ಟಿಯನ್ನು ವಿಭಜಿಸಿ, ಎರಡೂ ಭಾಗಗಳಲ್ಲಿ ಇದೇ ಕೆಲಸವನ್ನು ಮತ್ತೆ ಮಾಡುತ್ತದೆ. ಇಲ್ಲಿ ಯಾವಾಗಲೂ ಮೊದಲ ಮೌಲ್ಯವೇ ಪಿವಟ್. ಎರಡೂ ಭಾಗಗಳು ಸಮತೂಕದಲ್ಲಿದ್ದರೆ ಸಮಯ Θ(n log n). ಪ್ರತಿ ಬಾರಿಯೂ ಅತಿ ಚಿಕ್ಕ ಅಥವಾ ಅತಿ ದೊಡ್ಡ ಮೌಲ್ಯವೇ ಪಿವಟ್ ಆದರೆ ಸಮಯ Θ(n²). ಈ ಆಯ್ಕೆಯಲ್ಲಿ ಮೊದಲೇ ಕ್ರಮದಲ್ಲಿರುವ ಪಟ್ಟಿಗೂ ಹೀಗೆಯೇ ಆಗುತ್ತದೆ.
ಒಂದು ಸರಳ ಉದಾಹರಣೆ
ಪೀಠಾಧಾರವಾಗಿ 2 ಅನ್ನು ಆಯ್ಕೆಮಾಡಿ. 1 ಅನ್ನು ಅದರ ಎಡಕ್ಕೆ ಮತ್ತು 3 ಅನ್ನು ಅದರ ಬಲಕ್ಕೆ ಸರಿಸಿ. ಈಗ ಪ್ರತಿ ಬದಿಯಲ್ಲಿ ಒಂದು ಐಟಂ ಇದೆ.
ಆರಂಭದಲ್ಲಿ3 1 2
ಫಲಿತಾಂಶ1 2 3
Lesson.groovy
def values = [3, 1, 2]
def pivot = values[2]
def boundary = 0
def i = 0
while (i < 2) {
if (values[i] < pivot) {
def 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. 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] ಇಂದ ಆರಂಭಿಸಿ.
ಮೊದಲ ಮೌಲ್ಯವಾದ ೧೫ನ್ನು ಆಧಾರವಾಗಿ ಬಳಸಿ. ಎರಡೂ ತುದಿಗಳಿಂದ ಒಳಮುಖವಾಗಿ ಸ್ಕ್ಯಾನ್ ಮಾಡಿ.
ಎಡ ಸೂಚ್ಯಂಕವನ್ನು ಬಲಕ್ಕೆ ಸರಿಸಿ ಅದರ ಮೌಲ್ಯವು ಆಧಾರಕ್ಕಿಂತ ಕಡಿಮೆ ಅಥವಾ ಸಮನಾಗಿದ್ದರೆ ಮತ್ತು ಸೂಚ್ಯಂಕವು ಮೇಲಿನ ಮಿತಿಗಿಂತ ಕೆಳಗಿದ್ದರೆ. ಬಲ ಸೂಚ್ಯಂಕವನ್ನು ಎಡಕ್ಕೆ ಸರಿಸಿ ಅದರ ಮೌಲ್ಯವು ಆಧಾರಕ್ಕಿಂತ ಹೆಚ್ಚಿದ್ದರೆ.
ನೋವಿನ ಲಕ್ಷಣಗಳು ಸಿಗದೇ ಹೋದಲ್ಲಿ, ಆ ಎರಡು ತಪ್ಪಾದ ಮೌಲ್ಯಗಳನ್ನು ಬದಲಾಯಿಸಿ, ಇನ್ನೂ ಹುಡುಕುತ್ತಾ ಹೋಗುತ್ತವೆ.
ಸೂಚ್ಯಂಕಗಳು ಸಂಧಿಸಿದಾಗ ಅಥವಾ ದಾಟಿದಾಗ, ಮೊದಲ ಮೌಲ್ಯವನ್ನು ಬಲ ಸೂಚ್ಯಂಕದಲ್ಲಿರುವ ಮೌಲ್ಯದೊಂದಿಗೆ ವಿನಿಮಯ ಮಾಡಿ. ಆಧಾರವು ಈಗ ಅದರ ಅಂತಿಮ ಸ್ಥಾನದಲ್ಲಿದೆ.
ಆ ಸ್ಥಾನದ ಮೊದಲು ಮತ್ತು ನಂತರದ ಶ್ರೇಣಿಗಳನ್ನು ಪುನರಾವರ್ತಿತವಾಗಿ ವಿಂಗಡಿಸಿ. ಎರಡಕ್ಕಿಂತ ಕಡಿಮೆ ಅಂಶಗಳನ್ನು ಹೊಂದಿರುವ ಶ್ರೇಣಿಯನ್ನು ಈಗಾಗಲೇ ವಿಂಗಡಿಸಲಾಗಿದೆ.
Groovy ಭಾಷೆಯಲ್ಲಿ ಕ್ವಿಕ್ ಸಾರ್ಟ್ ಹಂತ ಹಂತವಾಗಿ
Groovy ಮೂಲ ಕೋಡ್
class QuickSortExample {
public void quickSort(arr, low, high) {
if(low < high) {
// Find the pivot index of the lower bound of an array
def pivot = this.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
this.quickSort(arr, low, pivot - 1)
// Right hand side array
this.quickSort(arr, pivot + 1, high)
}
}
public int partition(arr, low, high) {
// Pick a lowest bound element as an pivot value
def pivot = arr[low]
def i = low
def 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] <= pivot && i < high) {
i++
}
// Decrement the index value of "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) {
def 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
}
public void printArray(arr) {
def lastIndex = arr.length - 1
// Loop the item and print each item in the console with white space seperated
for (i in 0..lastIndex) {
def num = arr[i]
print(num + " ")
}
println("")
}
public static void main(String[] args) {
def obj = new QuickSortExample()
def arr = [15, 3, 12, 6, -9, 9, 0].toArray()
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