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

#include<bits/stdc++.h> using namespace std; void cocktailSort(int arr[], int n)

ID: 3736789 • Letter: #

Question

#include<bits/stdc++.h>

using namespace std;

void cocktailSort(int arr[], int n)

{

bool flag = true;


int p = 0;


int r = n-1;


int swaps = 0;


int comparison = 0;


while (flag)

{

flag = false;

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

{

comparison++;


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

{

swaps++;

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


flag = true;

}

}


if (!flag)

break;

flag = false;

--r;

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

{

comparison++;


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

{

swaps++;


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


flag = true;

}

}


++p;

}

  
int i;


cout<<"Cocktail Sort: ";

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

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

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

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

highlighted part is my error, how can i fix it?

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, number of comparisons and the number of swaps executed. Your output should look like Cocktail Sort: 1 1 2 3 333 445677789 10 13 15 17 Number of comparisons: Number of swaps:

Explanation / Answer

There was an extra new line before the line "Number of comparisons". So, I removed it. Now it prints as in the question.

Code

#include<bits/stdc++.h>

using namespace std;

void cocktailSort(int arr[], int n)

{

    bool flag = true;

    int p = 0;

    int r = n-1;

    int swaps = 0;

  

    int comparison = 0;

    while (flag)

    {

        flag = false;

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

        {

            comparison++;

          

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

            {

                swaps++;

             

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

              

                flag = true;

            }

        }

        if (!flag)

            break;

        flag = false;

        --r;

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

        {

            comparison++;

           

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

            {

                swaps++;

              

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

              

                flag = true;

            }

        }

        ++p;

    }

    int i;

    cout<<"Cocktail Sort: ";

  

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

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

      

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

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

}

int main()

{

    int arr[] = { 0, 0, 0, 0};

   

    cocktailSort(arr, 4);

   

    return 0;

}