সাইকেল সোর্ট একটি ইন-প্লেস (in-place) সোর্টিং অ্যালগরিদম। এটি কম্প্যারিজন-ভিত্তিক এবং অন্যান্য ইন-প্লেস সোর্টিং টেকনিকের চেয়ে দক্ষ। এটি সোর্টিং করার জন্য ন্যূনতম মেমরি রাইট অপারেশন খুঁজে বের করে।
সাইকেল সোর্টের জটিলতা
- টাইম কমপ্লেক্সিটি: O(n²)
- স্পেস কমপ্লেক্সিটি: O(1)
ইনপুট এবং আউটপুট
Input: A list of unsorted data: 23 63 98 74 20 14 36 45 99 78
Output: Array before Sorting: 23 63 98 74 20 14 36 45 99 78
Array after Sorting: 14 20 23 36 45 63 74 78 98 99
অ্যালগরিদম
cycleSort(array, size)
Input: ডেটার অ্যারে, মোট এলিমেন্ট
Output: সর্ট করা অ্যারে
Begin
for start := 0 to n-2 do
key := array[start]
location := start
for i := start+1 to n-1 do
if array[i] < key then location++
done
if location == start then continue
while key == array[location] do location++ done
if location != start then swap array[location] with key
while location != start do
location := start
for i := start+1 to n-1 do if array[i] < key then location++ done
while key == array[location] do location++ done
if key != array[location] swap array[location] and key
done
done
End
C++ উদাহরণ
#include <iostream>
using namespace std;
void swapping(int &a, int &b) { int temp = a; a = b; b = temp; }
void display(int *array, int size) { for(int i=0;i> n;
int arr[n];
cout << "Enter elements:" << endl;
for(int i=0; i> arr[i];
cout << "Array before Sorting: "; display(arr, n);
cycleSort(arr, n);
cout << "Array after Sorting: "; display(arr, n);
}
আউটপুট
Enter the number of elements: 10
Enter elements: 23 63 98 74 20 14 36 45 99 78
Array before Sorting: 23 63 98 74 20 14 36 45 99 78
Array after Sorting: 14 20 23 36 45 63 74 78 98 99