কম্পিউটার টিউটোরিয়াল

সাইকেল সোর্ট (Cycle Sort)

সাইকেল সোর্ট একটি ইন-প্লেস (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
  1. জাভাস্ক্রিপ্টে রেডিক্স সর্ট (Radix Sort) বাস্তবায়ন

  2. জাভাস্ক্রিপ্ট Sort() মেথড

  3. হ্যামিল্টোনিয়ান সাইকেল

  4. সাইকেল সর্টের পাইথন প্রোগ্রাম