Sorting Algorithms Examples: Shaker Sort in C++

Are you eager to examine brilliant sorting algorithms examples? You are at the right place. Here, you will find an excellent C++ example that will help you to complete your own task. Before taking a look at the example, you should explore the theoretical framework. That way, you will be able to see how theory reflects in practice.

All of our samples, including sorting algorithms examples, can be called top-notch. Why are they so high-quality? We do our best to find incredible experts that are able to complete such samples. If someone would like to be our expert, he or she will need to try hard to pass all of our tests. Even if the person has graduated from university and possesses a high-level degree in the specific subject, we test his or her knowledge in the discipline anyway. While testing our experts, we also pay attention to their speed of execution. We try to select experts that can deal with difficult tasks faster than others.

We pay a great deal of attention to the speed of execution, because we offer students the ability to order specific samples within their requirements and deadline. For this reason, we need our experts to be ready even for urgent orders, because as a rule, students need to receive examples as soon as possible. If you are among those students that need to get a specific example, contact us right away! We’ll be glad to provide you with immediate help. However, you should first look through our example and decide whether you need more assignment help.

There is a more effective form of the “Bubble” sort – “Shaker” sort. It uses a narrowing of the limits of the array in which there is a permutation. Also, the inner loops pass through the array in one direction and then in another direction, raising the lightest element up and dropping the heaviest element on the bottom during the one iteration of the outer loop.

The source code of the program:

#include <iostream>
using namespace std;

void swap(int *arr, int i)
	int tmp;
	tmp = arr[i];
	arr[i] = arr[i - 1];
	arr[i - 1] = tmp;

void shakerSort(int *arr, int size)
	int left = 1;
	int right = size - 1;
	while (left <= right) { for (int i = right; i >= left; i--)
		if (arr[i - 1] > arr[i]) swap(arr, i);

		for (int i = left; i <= right; i++) if (arr[i - 1] > arr[i]) swap(arr, i);

		cout << "\nIteration: " << left - 1;

int main()
	int size = 0;
	cout << "Array size: "; cin >> size;
	int *A = new int[size];

	for (int k = 0; k < size; k++)
		A[k] = size - k; // descending input
		cout << A[k] << " | ";

	shakerSort(A, size); // sorting

	cout << "\nSorted array::\n";
	for (int k = 0; k < size; k++)
		cout << A[k] << " | ";
	cout << endl;

	return 0;

Let’s take a look at the function that contains the “Shaker” sort (two-way “Bubble” sort). It’s called “shakerSort”. Due to the variables called “left” and “right”, the part of the array which is being sorted will shrink with every step of the cycle. This has a positive impact on the time of the program work. And due to the two nested “for” loops in one iteration of the outer loop, minimum and maximum values take their correct positions in the array.

The result of the program:

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.