- 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 to Perform the Shaker Sort
Shaker sort is used to sort the given data. Shaker sort, unlike Bubble sort, orders the array in both directions. The worst complexity of this algorithm is O(n^2).
Algorithm
Begin ShakerSort() function has ‘arr’ the array of data and ‘n’ the number of values, in the argument list. // Implement Sorting algorithm using nested for loops. The parent loop will run on ‘i’ from 0 to n-1 and contains two loops inside. The first loop will run on ‘j’ from i+1 to n-1 and use swap() if a[j] < a[j-1]. Decrement n. The second loop will run on 'k' from m-1 to i+1 and use swap() if a[k] < a[k-1]. Increment i. End
Example Code
#include<iostream> using namespace std; void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } void ShakerSort(int a[], int m) { int i, j, k; for(i = 0; i < m;) { for(j = i+1; j < m; j++) { if(a[j] < a[j-1]) swap(&a[j], &a[j-1]); } m--; for(k = m-1; k > i; k--) { if(a[k] < a[k-1]) swap(&a[k], &a[k-1]); } i++; } } int main() { int n, i; cout<<"\nEnter the number of data element to be sorted: "; cin>>n; int a[n]; for(i = 0; i < n; i++) { cout<<"Enter element "<<i+1<<": "; cin>>a[i]; } ShakerSort(a, n); cout<<"\nSorted Data "; for (i = 0; i < n; i++) cout<<"->"<<a[i]; return 0; }
Output
Enter the number of data element to be sorted: 4 Enter element 1: 3 Enter element 2: 1 Enter element 3: 7 Enter element 4: 6 Sorted Data ->1->3->6->7
- Related Articles
- C++ Program to Perform Stooge Sort
- C# program to perform Quick sort using Recursion
- C++ Program to Perform Quick Sort on Large Number of Elements
- 8085 Program to perform sorting using selection sort
- 8085 Program to perform sorting using bubble sort
- How to perform Merge Sort using C#?
- 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
- Program to perform bubble sort in ascending order in 8085 Microprocessor
- Program to perform bubble sort based on choice in 8085 Microprocessor
- Program to perform selection sort in ascending order in 8085 Microprocessor

Advertisements