
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Perform Stooge Sort
Stooge Sort is used to sort the given data. It is a recursive sorting algorithm. Stooge Sort divides the array into two overlapping parts, 2/3 each and Sort the array in three steps by sorting I then II and again I part. The worst case time complexity of this algorithm is O(n^2.7095).
Algorithm
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.
Example Code
#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; }
Output
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
- Related Articles
- Python Program for Stooge Sort
- Java Program for Stooge Sort
- C++ Program to Perform the Shaker Sort
- C# program to perform Quick sort using Recursion
- 8085 Program to perform sorting using selection sort
- 8085 Program to perform sorting using bubble sort
- 8085 Program to perform selection sort in ascending order
- 8085 Program to perform selection sort in descending order
- 8085 Program to perform bubble sort in ascending order
- 8085 Program to perform bubble sort in descending order
- Program to perform sorting using selection sort in 8085 Microprocessor
- a 8085 Program to perform bubble sort based on choice
- C++ Program to Perform Quick Sort on Large Number of Elements
- Program to perform bubble sort in ascending order in 8085 Microprocessor
- Program to perform bubble sort based on choice in 8085 Microprocessor

Advertisements