কম্পিউটার

স্টুজ সাজানোর জন্য C++ প্রোগ্রাম


প্রদত্ত ডেটা সাজানোর জন্য Stooge Sort ব্যবহার করা হয়। এটি একটি পুনরাবৃত্ত বাছাই অ্যালগরিদম। স্টুজ সর্ট অ্যারেকে দুটি ওভারল্যাপিং অংশে বিভক্ত করে, প্রতিটি 2/3 এবং তিনটি ধাপে I তারপর II এবং আবার I অংশ সাজিয়ে অ্যারেটিকে সাজান৷ এই অ্যালগরিদমের সবচেয়ে খারাপ ক্ষেত্রে সময় জটিলতা হল O(n^2.7095)৷

অ্যালগরিদম

Begin
   Take input of data.
   Call StoogeSort() function with ‘a’ the array of data and ‘n’ the number of values, in the argument list.
   Implement Sorting using recursive approach.
   Divide the array into first 2/3 element as part I and last 2/3 as part II.
   Then send the first, second and again first part into StoogeSort().
   If the length is not further breakable then swap element at the start and end if a[end] < a[start].
   Return to main and display the result.
End.

উদাহরণ কোড

#include<iostream>
using namespace std;
void StoogeSort(int a[],int start, int end) {
   int temp;
   if(end-start+1 > 2) {
      temp = (end-start+1)/3;
      StoogeSort(a, start, end-temp);
      StoogeSort(a, start+temp, end);
      StoogeSort(a, start, end-temp);
   }
   if(a[end] < a[start]) {
      temp = a[start];
      a[start] = a[end];
      a[end] = temp;
    }
}
int main() {
   int m, i;
   cout<<"\nEnter the number of data element to be sorted: ";
   cin>>m;
   int arr[m];
   for(i = 0; i < m; i++) {
      cout<<"Enter element "<<i+1<<": ";
      cin>>arr[i];
   }
   StoogeSort(arr, 0, m-1);
   cout<<"\nSorted Data ";
   for (i = 0; i < m; i++)
      cout<<"->"<<arr[i];
   return 0;
}

আউটপুট

Enter the number of data element to be sorted: 4
Enter element 1: 6
Enter element 2: 7
Enter element 3: 3
Enter element 4: 2
Sorted Data ->2->3->6->7

  1. মার্জ সাজানোর জন্য সি++ প্রোগ্রাম

  2. হিপ সাজানোর জন্য C++ প্রোগ্রাম

  3. বুদবুদ সাজানোর জন্য C++ প্রোগ্রাম

  4. শেকার সাজানোর জন্য C++ প্রোগ্রাম