কুইকসর্ট একটি মানকে ভিত্তি হিসেবে বেছে নেয়। এর নাম পিভট। পিভট অনুযায়ী তালিকা ভাগ করে দুই অংশে আবার একই কাজ করে। এখানে প্রথম মানটাই সব সময় পিভট। অংশ দুটো কাছাকাছি মাপের হলে সময় Θ(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] দিয়ে শুরু করুন।
প্রথম মান 15-কে পিভট ধরুন। দুই প্রান্ত থেকে মাঝের দিকে এগিয়ে দেখুন।
বাঁ দিকের সূচক ওপরের সীমার নিচে থাকলে এবং সেখানকার মান পিভটের চেয়ে ছোট বা সমান হলে সূচকটিকে ডানে এগিয়ে নিন। ডান দিকের সূচকে মান পিভটের চেয়ে বড় থাকা পর্যন্ত সেই সূচকটি বাঁয়ে সরান।
সূচক দুটো এখনও মুখোমুখি না হলে ভুল দিকে থাকা মান দুটোর জায়গা বদলান। তারপর খোঁজ চালিয়ে যান।
সূচক দুটো একই জায়গায় এলে বা পরস্পরকে পেরিয়ে গেলে প্রথম মানটির সঙ্গে ডান সূচকের মানটির জায়গা বদলান। পিভট এখন তার ঠিক জায়গায় আছে।
ওই জায়গার আগের ও পরের অংশে একই সাজানোর পদ্ধতি আবার চালান। কোনো অংশে দুটির কম আইটেম থাকলে সেটি এমনিতেই সাজানো।
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