0% found this document useful (0 votes)
11 views3 pages

C++ Cocktail Shaker Sort Examples

The document contains a C program that implements various sorting algorithms, including naive bubble sort, improved bubble sort, naive cocktail shaker sort, and improved cocktail shaker sort. Each sorting function counts the number of comparisons made during the sorting process and prints the sorted array. The main function allows the user to input an array of 15 integers and demonstrates the sorting algorithms on this array.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

C++ Cocktail Shaker Sort Examples

The document contains a C program that implements various sorting algorithms, including naive bubble sort, improved bubble sort, naive cocktail shaker sort, and improved cocktail shaker sort. Each sorting function counts the number of comparisons made during the sorting process and prints the sorted array. The main function allows the user to input an array of 15 integers and demonstrates the sorting algorithms on this array.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <stdio.

h>
#include <stdlib.h>
void print_arr(int *a, int n)
{
int i;
printf("sorted array is : ");
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
printf("\n");
}
void bubblesort_naive(int *a, int n)
{
int comparisons = 0;
int i, j;
for (i = 0; i < n - 1; i++)
{

for (j = 0; j < n - i - 1; j++)


{
comparisons++;
if (a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
print_arr(a, n);
printf("Number of comparisons=%d\n", comparisons);
}
void improved_bubblesort(int *a, int n)
{
int bound = n, j, t, comparisons = 0;
;
do
{
t = 0;
for (j = 0; j < bound - 1; j++)
{
comparisons++;

if (a[j] > a[j + 1])


{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
t = j + 1;
}
}
bound = t;
} while (t > 0);
print_arr(a, n);
printf("number of comparisons=%d\n", comparisons);
}
void naive_cocktailshakersort(int *a, int n)
{
int i, swap, comparisons = 0;
int start = 0, end = n - 1;
do
{
swap = 0;
for (i = start; i < end; i++)
{
comparisons++;
if (a[i] > a[i + 1])
{
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
swap = 1;
}
}
end--;
for (i = end; i > start; i--)
{
comparisons++;
if (a[i] < a[i - 1])
{
int temp = a[i];
a[i] = a[i - 1];
a[i - 1] = temp;
swap = 1;
}
}
start++;

} while (swap > 0);


print_arr(a, n);
printf("number of comparisons=%d\n", comparisons);
}
void improved_cocktailshakersort(int *a, int n)
{
int start = 0, end = n - 1, comparisons = 0;
int t, i;
while (start < end)
{
t = start;
for (i = start; i < end; i++)
{

comparisons++;
if (a[i] > a[i + 1])
{
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
t = i;
}
}
end = t;
t = end;
for (i = end; i > start; i--)
{

comparisons++;
if (a[i] < a[i - 1])
{
int temp = a[i];
a[i] = a[i - 1];
a[i - 1] = temp;
t = i;
}
}
start = t;
}
print_arr(a, n);
printf("number of comparisons=%d\n", comparisons);
}

int main()
{
int *a, *copy, choice;
a = (int *)malloc(15 * sizeof(int));
copy = (int *)malloc(15 * sizeof(int));
printf("Enter 15 array elements\n");
for (int i = 0; i < 15; i++)
scanf("%d", &a[i]);
printf("The unordered array : ");
for (int i = 0; i < 15; i++)
printf("%d\t", a[i]);
printf("\n");

for (int i = 0; i < 15; i++)


copy[i] = a[i];
printf("\nNaive Bubble Sort:\n");
bubblesort_naive(copy, 15);

for (int i = 0; i < 15; i++)


copy[i] = a[i];
printf("\nImproved Bubble Sort:\n");
improved_bubblesort(copy, 15);

for (int i = 0; i < 15; i++)


copy[i] = a[i];
printf("\nNaive Cocktail Shaker Sort:\n");
naive_cocktailshakersort(copy, 15);

for (int i = 0; i < 15; i++)


copy[i] = a[i];
printf("\nImproved Cocktail Shaker Sort:\n");
improved_cocktailshakersort(copy, 15);
}

You might also like