logo
  • Home
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Perform Stooge Sort

Posted on November 15, 2014 by Manish

This is a C++ program to sort the given data using Stooge Sort.

Problem Description

1. Stooge sort is a recursive sorting algorithm.
2. It divides the array into two overlapping parts, 2/3 each.
3. Sort the array in three steps by sorting I then II and again I part.
4. It is fairly inefficient algorithm with worst case time complexity O(n^2.7095).

Problem Solution

1. Recursively divide the divide the array into two parts of size 2/3 of array length.
2. Sort the first part.
3. Sort second part.
4. Again sort the first part.
5. Display the result.
6. Exit.

Program/Source Code

C++ program to implement Shaker Sort.
This program is successfully run on Dev-C++ using TDM-GCC 4.9.2 MinGW compiler on a Windows system.

#include<iostream>
 
using namespace std;
 
// A function implementing stooge sort.
void StoogeSort(int a[],int start, int end)
{
	int temp;
	// Further breaking the array if the Subpart's length is more than 2. 
	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);
	}
 
	// swapping the element at start and end. 
	if(a[end] < a[start])
	{
		temp = a[start];
		a[start] = a[end];
		a[end] = temp;
	}
}
 
int main()
{	
	int n, i;
	cout<<"\nEnter the number of data element to be sorted: ";
	cin>>n;
 
	int arr[n];
	for(i = 0; i < n; i++)
	{
		cout<<"Enter element "<<i+1<<": ";
		cin>>arr[i];
	}
 
	StoogeSort(arr, 0, n-1);
 
	// Printing the sorted data.
	cout<<"\nSorted Data ";
	for (i = 0; i < n; i++)
		cout<<"->"<<arr[i];
 
	return 0;
}
Program Explanation

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

Runtime Test Cases
 
Case 1:(average case)
 
Enter the number of data element to be sorted: 5
Enter element 1: 99
Enter element 2: 45
Enter element 3: 2
Enter element 4: 35
Enter element 5: 121
 
Sorted Data ->2->35->45->99->121
 
 
Case 2:(best case)
 
Enter the number of data element to be sorted: 5
Enter element 1: 2
Enter element 2: 33
Enter element 3: 45
Enter element 4: 102
Enter element 5: 165
 
Sorted Data ->2->33->45->102->165
 
 
case 3: (worst case)
 
Enter the number of data element to be sorted: 5
Enter element 1: 998
Enter element 2: 564
Enter element 3: 32
Enter element 4: 8
Enter element 5: 1
 
Sorted Data ->1->8->32->564->998

Sanfoundry Global Education & Learning Series – C++ Algorithms.
To practice all C++ Algorithms, here is complete set of 1000 C++ Algorithms.

« Prev Page - C++ Program to Implement Shell Sort
» Next Page - C++ Program to Implement Quick Sort Using Randomization
« Java Program to Take a Number and Return List of its Digits
Oracle Database Questions and Answers – Planning Oracle Applications – Approaches, Risks and Standards »

Deep Dive @ Sanfoundry:

  1. C++ Programming Examples on Graph Problems & Algorithms
  2. C Programming Examples on Graph Problems & Algorithms
  3. C# Programming Examples on Arrays
  4. C Programming Examples on Arrays
  5. C Programming Examples using Recursion
  6. C++ Programming Examples on Combinatorial Problems & Algorithms
  7. Java Programming Examples on Combinatorial Problems & Algorithms
  8. C Programming Examples on Combinatorial Problems & Algorithms
  9. C# Programming Examples on Sorting
  10. C Programming Examples on Searching and Sorting
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry