ક્વિકસૉર્ટ એક મૂલ્યને આધાર બનાવે છે. તેને પિવટ કહે છે. તેના આધારે યાદીને વહેંચીને બંને ભાગ પર આ જ કામ ફરી કરે છે. અહીં હંમેશાં પહેલું મૂલ્ય પિવટ બને છે. બંને ભાગ લગભગ સરખા હોય તો સમય Θ(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. ત્રીજી પાર્ટીમેન્ટ શું પેદા કરશે?
શરૂઆતમાં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