क्विकसॉर्ट आधार म्हणून एक मूल्य निवडतो. त्याला पिवट म्हणतात. त्यानुसार यादी विभागून प्रत्येक भागावर हेच काम पुन्हा करतो. इथे नेहमी पहिलंच मूल्य पिवट असतं. दोन्ही भाग साधारण सारखे असतील तर वेळ Θ(n log n) असतो. दर वेळी सर्वांत लहान किंवा सर्वांत मोठं मूल्य पिवट निघालं तर वेळ Θ(n²) होतो. या नियमानुसार आधीच क्रमाने लावलेल्या यादीतही हेच घडतं.
एक सोपे उदाहरण
केंद्रबिंदू म्हणून 2 निवडा. 1 त्याच्या डावीकडे आणि 3 त्याच्या उजवीकडे हलवा. प्रत्येक बाजूला आता एक वस्तू आहे.
सुरुवातीला3 1 2
निकाल1 2 3
Main.java
public class Main {
public static void main(String[] args) {
int[] values = {3, 1, 2};
int pivot = values[2];
int boundary = 0;
int i = 0;
while (i < 2) {
if (values[i] < pivot) {
int 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) {
System.out.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²)
Java मध्ये क्विक सॉर्ट
Java सोर्स कोड
class QuickSortExample {
public void quickSort(int[] arr, int low, int high) {
if(low < high) {
int pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
}
private int partition(int[] arr, int low, int high) {
int pivot = arr[low];
int i = low;
int j = high;
while(i < j) {
while(arr[i] <= pivot && i < high) {
i++;
}
while(arr[j] > pivot && j > low) {
j--;
}
if(i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
arr[low] = arr[j];
arr[j] = pivot;
return j;
}
public void printArray(int[] arr) {
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}
public static void main(String[] args) {
QuickSortExample obj = new QuickSortExample();
int[] arr = { 15, 3, 12, 6, -9, 9, 0 };
System.out.print("Before Sorting: ");
obj.printArray(arr);
obj.quickSort(arr, 0, arr.length - 1);
System.out.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] पासून सुरुवात करा.
पहिलं मूल्य 15 हे पिवट ठेवा. दोन्ही टोकांकडून आतल्या दिशेने तपासत जा.
डावा इंडेक्स वरच्या मर्यादेपेक्षा लहान आहे आणि तिथलं मूल्य पिवटपेक्षा लहान किंवा त्याएवढं आहे, तोपर्यंत तो उजवीकडे सरकवा. उजव्या इंडेक्सवरचं मूल्य पिवटपेक्षा मोठं आहे तोपर्यंत तो इंडेक्स डावीकडे सरकवा.
दोन्ही इंडेक्स अजून एकमेकांपर्यंत पोहोचले नसतील तर चुकीच्या बाजूला असलेल्या दोन मूल्यांच्या जागा बदला. मग तपासणी पुढे चालू ठेवा.
दोन्ही इंडेक्स एकाच जागी आले किंवा एकमेकांना ओलांडून गेले की पहिलं मूल्य आणि उजव्या इंडेक्सवरचं मूल्य यांच्या जागा बदला. आता पिवट त्याच्या अंतिम जागी आहे.
त्या जागेच्या आधीच्या आणि नंतरच्या भागांवर हीच पद्धत पुन्हा वापरा. दोनपेक्षा कमी घटक असलेला भाग आधीच क्रमात असतो.
Java मध्ये क्विक सॉर्ट टप्प्याटप्प्याने समजून घ्या
Java सोर्स कोड
class QuickSortExample {
public void quickSort(int[] arr, int low, int high) {
if(low < high) {
// Find the pivot index of the lower bound of an array
int 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);
}
}
private int partition(int[] arr, int low, int high) {
// Pick a lowest bound element as an pivot value
int pivot = arr[low];
int i = low;
int 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) {
int 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(int[] arr) {
// Loop the item and print each item in the console with white space seperated
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}
public static void main(String[] args) {
QuickSortExample obj = new QuickSortExample();
int[] arr = { 15, 3, 12, 6, -9, 9, 0 };
System.out.print("Before Sorting: ");
obj.printArray(arr);
obj.quickSort(arr, 0, arr.length - 1);
System.out.print("After Sorting: ");
obj.printArray(arr);
}
}
आउटपुट
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15