# I have three sort. How to calculate each of these, the number of shipments and comparisons?

An explanation of what a shipment and comparison:
At each step of the iteration, two elements are compared - this COMPARISON
and if, for example, the left > right, they swap places (or Vice versa, depends on the type of sort) is the FORWARDING.
That is, forwarding is when the two elements are swapped.

``````#include <iostream>
#include <cstdlib>
#include <locale>
#include <conio.h>
#define SIZE 10

using namespace std;

void random_array(int array[SIZE]);
void bubble_sort(int array[SIZE]);
void select_sort(int array[SIZE]);
void shaker_sort(int array[SIZE]);

int main()
{
setlocale(LC_ALL, "rus");

int array[SIZE];

random_array(array);
bubble_sort(array);
for (int i = 0; i <= SIZE-1; i++)
cout << array[i] << " ";
cout << "\n\n";

random_array(array);
select_sort(array);
for (int i = 0; i <= SIZE-1; i++)
cout << array[i] << " ";
cout << "\n\n";

random_array(array);
shaker_sort(array);
for (int i=0; i <= SIZE-1; i++)
cout << array[i] << " ";
cout << "\n\n";

getch();
return 0;
}

void random_array(int array[SIZE])
{
cout << "original array" << endl;
for (int i=0; i<SIZE; i++)
{
array[i] = rand()%100;
cout << array[i] << " ";
}
}

void bubble_sort(int array[SIZE])
{
int temp;
cout << "\puzirkova sorting" << endl;
for (int i = 0; i < SIZE-1; i++)
for (int j = SIZE-2; j >= i; j--)
if (array[j] > array[j+1]) // if the previous element is greater the following
{
temp = array[j]; //
array[j] = array[j+1]; // rearrange them
array[j+1] = temp; //
}
}

void select_sort(int array[SIZE])
{
int min, temp;
cout << "\sortirovka the method of direct selection" << endl;
for (int i = 0; i < SIZE-1; i++)
{
min = i; // index of minimum element
for (int j = i+1; j < SIZE; j++)
if (array[ j ] < array[min]) // if current element is less than the minimum
{
min = j; // remember its index
}
temp = array[i]; //
array[i] = array[min]; // rearrange them
array[min] = temp; //
}
}

void shaker_sort(int array[SIZE])
{
int left, right, border, temp;
cout << "\sakerna sorting" << endl;
for (right=SIZE-1, left=0, border=-1; border!=0;) // set left and right borders
{
border = 0;
for (int i=left; i<right; i++) // move from left to right
{
if (array[i] > array[i+1]) // if the current element is greater the following
{
temp = array[i]; //
array[i] = array[i+1]; // rearrange them
array[i+1] = temp; //
border = i; // set the label of the last reshuffle
}
}
right = border; // remember right border
for (int i=right; i>left; i--) // move right to left
{
if (array[i-1] > array[i]) // if the current element is less than the following
{
temp = array[i]; //
array[i] = array[i-1]; // rearrange them
array[i-1] = temp; //
border = i; // set the label of the last reshuffle
}
}
left = border; // remember the border
}
}``````
March 19th 20 at 09:10
March 19th 20 at 09:12
Solution
``````void bubble_sort(int array[SIZE])
{
int transfer = 0;
int comparison = 0;

cout << "\puzirkova sorting" << endl;
for (int i = 0; i < SIZE-1 ; i++)
{
for (int j = SIZE-2; j >= i; j--)
{
comparison++;
if (array[j] > array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
transfer++;
}
}
}

cout << "COMPARING" << comparison << endl;
cout << "FORWARDING" << transfer << endl;
}``````

Find more questions by tags Data structuresAlgorithmsC++