- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program for the Gnome Sort?
Gnome sort is a sorting algorithm which is similar to Insertion sort, except that moving an element to its proper place is accomplished by a series of swaps, as in bubble sort.
Input: 53421 Output: 12345
Explanation
Sorting algorithm that moving an element to its proper place is accomplished by a series of swaps, as in bubble sort. It is simply requiring loops.
Example
#include <iostream> using namespace std; int main() { int temp; int arr[] = { 5, 3, 4, 2, 1 }; int n=5; int i; i = 0; while (i < n) { if (i == 0 || arr[i - 1] <= arr[i]) i++; else { temp = arr[i-1]; arr[i - 1] = arr[i]; arr[i] = temp; i = i - 1; } } for (i = 0;i < n;i++) { cout<<arr[i]<<"\t"; } }
- Related Articles
- C++ Program for Gnome Sort?
- Python Program for Gnome Sort
- Java Program for Gnome Sort
- C/C++ Program for the Odd-Even Sort (Brick Sort)?
- C++ Program for the Cocktail Sort?
- C++ Program for the Comb Sort?
- C/C++ Program for Odd-Even Sort (Brick Sort)?
- C++ Program for Cocktail Sort?
- C++ Program for Comb Sort?
- C++ Program for Pigeonhole Sort?
- C Program for Selection Sort?
- C++ Program for Cycle Sort?
- C Program for Radix Sort
- C++ Program for the Recursive Bubble Sort?
- C++ Program for Recursive Bubble Sort?

Advertisements