क्विकसॉर्ट एक मान को आधार बनाता है, जिसे पिवट कहते हैं। उसके हिसाब से सूची को बाँटकर दोनों हिस्सों पर यही तरीका दोहराता है। यहाँ पहला मान हमेशा पिवट है। हिस्से लगभग बराबर हों तो समय Θ(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