ક્વિકસૉર્ટ એક મૂલ્યને આધાર બનાવે છે. તેને પિવટ કહે છે. તેના આધારે યાદીને વહેંચીને બંને ભાગ પર આ જ કામ ફરી કરે છે. અહીં હંમેશાં પહેલું મૂલ્ય પિવટ બને છે. બંને ભાગ લગભગ સરખા હોય તો સમય Θ(n log n) છે. દર વખતે સૌથી નાનું કે સૌથી મોટું મૂલ્ય પિવટ બને તો સમય Θ(n²) થાય છે. આ નિયમમાં પહેલેથી ગોઠવાયેલી યાદી સાથે પણ એવું થાય છે.
એક સરળ ઉદાહરણ
પાઇવોટ તરીકે 2 પસંદ કરો. 1 ને તેની ડાબી બાજુએ અને 3 ને તેની જમણી બાજુએ ખસેડો. દરેક બાજુ હવે એક વસ્તુ છે.
શરૂઆતમાં3 1 2
પરિણામ1 2 3
lesson.cpp
#include <iostream>
int main() {
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) {
std::cout << values[i] << '\n';
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²)
C++માં ક્વિક સૉર્ટ
C++ સોર્સ કોડ
#include <iostream>
using namespace std;
class QuickSortExample
{
// Access specifier
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;
}
// Access specifier
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);
}
}
void printArray(const int arr[], size_t size) {
for(size_t i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main(int argc, const char * argv[])
{
// Declare an object of class QuickSortExample
QuickSortExample obj;
int arr[] = { 15, 3, 12, 6, -9, 9, 0 };
const size_t SIZE = sizeof(arr) / sizeof(*arr);
cout << "Before Sorting: ";
obj.printArray(arr, SIZE);
obj.quickSort(arr, 0, (SIZE - 1));
cout << "After Sorting: ";
obj.printArray(arr, SIZE);
return 0;
}
આઉટપુટ
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]થી શરૂઆત કરો.
પહેલી કિંમત, ૧૫, પિવેટ તરીકે વાપરો. બંને અંતમાંથી વચ્ચેનું સ્કેન કરો.
ડાબો ઇન્ડેક્સ ઉપલી મર્યાદાથી નીચે હોય અને ત્યાંનું મૂલ્ય પિવટથી નાનું કે સરખું હોય ત્યાં સુધી તેને જમણે ખસેડો. જમણા ઇન્ડેક્સ પરનું મૂલ્ય પિવટથી મોટું હોય ત્યાં સુધી તે ઇન્ડેક્સને ડાબે ખસેડો.
બંને ઇન્ડેક્સ હજી મળ્યા ન હોય તો ખોટી બાજુએ રહેલાં બે મૂલ્યોની જગ્યા બદલો. પછી આગળ તપાસો.
બંને ઇન્ડેક્સ મળે કે એકબીજાને વટાવી જાય ત્યારે પહેલા મૂલ્ય અને જમણા ઇન્ડેક્સ પરના મૂલ્યની જગ્યા બદલો. હવે પિવટ તેની અંતિમ જગ્યાએ છે.
એ જગ્યાની ડાબી અને જમણી બાજુના ભાગ પર આ જ રીત ફરી લાગુ કરો. બે કરતાં ઓછી વસ્તુ ધરાવતો ભાગ પહેલેથી જ ક્રમમાં હોય છે.
C++માં ક્વિક સૉર્ટ પગલે પગલે સમજો
C++ સોર્સ કોડ
#include <iostream>
using namespace std;
class QuickSortExample
{
// Access specifier
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;
}
// Access specifier
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);
}
}
void printArray(const int arr[], size_t size) {
// Loop the item and print each item in the console with white space seperated
for(size_t i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main(int argc, const char * argv[])
{
// Declare an object of class geeks
QuickSortExample obj;
int arr[] = { 15, 3, 12, 6, -9, 9, 0 };
const size_t SIZE = sizeof(arr) / sizeof(*arr);
cout << "Before Sorting: ";
obj.printArray(arr, SIZE);
obj.quickSort(arr, 0, (SIZE - 1));
cout << "After Sorting: ";
obj.printArray(arr, SIZE);
return 0;
}
આઉટપુટ
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15