Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem2 Write a function cocktailSort that takes an array of integers and the n

ID: 3736770 • Letter: P

Question

Problem2 Write a function cocktailSort that takes an array of integers and the number of elements in the array. Your function should sort the array using cocktail sort and print the number of comparisons and swaps (a"swap" is whenever two numbers change places). You should implement the cocktail sort algorithm with a flag. Note: You might also want to write and use a function that swaps two values. Your function should not return anything but should display the sorted array and the number of swaps executed. Your output should look like: Cocktail Sort: 1123333 445677 7 89 10 13 15 17 Number of comparisons: Number of swaps:

Explanation / Answer

#include<bits/stdc++.h>

using namespace std;

// sort the array

void Cocktail_sort(int arr[], int n)

{

    // set flag to true

    bool flag = true;

   

    // oint to the beginning of array

    int p = 0;

   

    // oint to the ending of array

    int r = n-1;

   

    // store the number of swaps

    int swaps = 0;

   

    // store the number of compaisons

    int comparison = 0;

    // loop untill flag becomes false

    while (flag)

    {

        // reset the flag to false

        flag = false;

      // loop through the array

        for (int i = p; i < r; ++i)

        {

            comparison++;

           

            // if current element is larger than the next element

            if (arr[i] > arr[i + 1])

            {

                swaps++;

               

                // swap the contents of the i th and (i+1) the position

                swap(arr[i], arr[i+1]);

               

                // set flag to true

                flag = true;

            }

        }

        // if the array is already sorted, then nothing was moved

        // so break out of loop as array is now sorted

        if (!flag)

            break;

        // reset flag for next iteration

        flag = false;

        // decrement the pointer to the right side of array

        // as the r th element is in its right position

        --r;

        // from right to left, doing the

        // same comparison as in the previous stage

        for (int i = r - 1; i >= p; --i)

        {

            comparison++;

           

            // if current element is larger than the next element

            if (arr[i] > arr[i + 1])

            {

                swaps++;

               

                // swap the contents of the i th and (i+1) the position

                swap(arr[i], arr[i+1]);

               

                // set flag to true

                flag = true;

            }

        }

        // increment the pointer to the left side of array

        ++p;

    }

   

    int i;

   

    cout<<"Sorted array : ";

   

    // print the contents of array

    for (i = 0 ; i < n ; i++)

        cout<<arr[i]<<" ";

       

    cout<<" Number of Comparisons : "<<comparison<<endl;

    cout<<"Number of swaps : "<<swaps;

}

int main()

{

    int arr[] = {7 , 6 , 5 , 4 , 1 , 2};

   

    Cocktail_sort(arr, 6 );

   

    return 0;

}

Output:-

Sorted array :

1 2 4 5 6 7

Number of Comparisons : 15

Number of swaps : 14

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote