

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program for Stooge Sort
<p style="">In this article, we will learn about the solution to the problem statement given below.</p><p style=""><strong>Problem statement</strong> − We are given an array, we need to sort it using stooge sort.</p><h2>Algorithm</h2><pre class="result notranslate">1. Check if value at index 0 is greater than value at last index,then swap them. 2. sort the initial 2/3rd of the array. 3. sort the last 2/3rd of the array. 4. sort the initial 2/3rd again to confirm.</pre><p>Now let’s observe the solution in the implementation below −</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/2WIfmmxY" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate" style="">def stoogesort(arr, l, h): if l >= h: return # swap if arr[l]>arr[h]: t = arr[l] arr[l] = arr[h] arr[h] = t # more than 2 elements if h-l+1 > 2: t = (int)((h-l+1)/3) # sort first 2/3 elements stoogesort(arr, l, (h-t)) # sort last 2/3 elements stoogesort(arr, l+t, (h)) # sort first 2/3 elements again stoogesort(arr, l, (h-t)) # main arr = [1,4,2,3,6,5,8,7] n = len(arr) stoogesort(arr, 0, n-1) print ("Sorted sequence is:") for i in range(0, n): print(arr[i], end = " ")</pre><h2>Output</h2><pre class="result notranslate">Sorted sequence is: 1 2 3 4 5 6 7 8</pre><p style=""><img src="https://www.tutorialspoint.com/assets/questions/media/33017/stooge_sort.jpg" class="fr-fic fr-dib" width="474" height="194"></p><p>All the variables are declared in the local scope and their references are seen in the figure above.</p><h2>Conclusion −</h2><p>In this article, we have learned about how we can make a Python Program for Stooge Sort</p>
- Related Questions & Answers
- Java Program for Stooge Sort
- C++ Program to Perform Stooge Sort
- Python Program for Insertion Sort
- Python Program for Selection Sort
- Python Program for Bubble Sort
- Python Program for Cocktail Sort
- Python Program for Counting Sort
- Python Program for Cycle Sort
- Python Program for Gnome Sort
- Python Program for Heap Sort
- Python Program for Merge Sort
- Python Program for Odd-Even Sort / Brick Sort
- Python Program for Binary Insertion Sort
- Python Program for Iterative Merge Sort
- Python Program for Iterative Quick Sort
Advertisements