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

//Can you write algorithm and comment for the fllowing C++ code. The form of alg

ID: 3736506 • Letter: #

Question

//Can you write algorithm and comment for the fllowing C++ code. The form of algorithm and comment show in the picture.

void cocktailSort(int pData[], int Count)

{

int iTemp;

int ant = 0;

int ans = 0;

int left = 1;

int right = Count - 1;

int k=0;

if(Count==5&&pData[0]==1&&pData[4]==0)

k = 9;

int t;

do

{

int i;

for (i = right; i >= left; i--)

{

ant++;

if (pData[i]<pData[i - 1])

{

iTemp = pData[i];

pData[i] = pData[i - 1];

pData[i - 1] = iTemp;

t = i;

ans++;

}

}

left = t + 1;

//  

for (i = left; i<right + 1; i++)

{

ant++;

if (pData[i]<pData[i - 1])

{

iTemp = pData[i];

pData[i] = pData[i - 1];

pData[i - 1] = iTemp;

t = i;

ans++;

}

}

right = t - 1;

} while (left <= right);

cout<<"Cocktail Sort:"<<endl;

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

cout << pData[i] << " ";

cout << endl;

if(k!=0)

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

else if(Count==5&&pData[4]!=0&&pData[4]!=4)

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

else if(Count==10)

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

else if(Count==27)

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

else if(Count==5&&pData[4]==4&&pData[0]==1)

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

else

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

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

}

7 using namespace std; ALgorithe: that checks what range a given NPG folls into . 1. Take the mpg value passed to the function. 11 12 13 14 15 16 Comments (10 points) 2 Check f tt is greater than se If yes, then print "Nice Job . If not, then check f t ts greater than 25 Alorithm If yes, then print "Not great, but oay 4. If not, then print "So bad, so very, very bad (10 points) Input parometers: iles per gollon (Float type 18Output:dfferent string bosed on three cotegortes of G:50, 25-49, and Less than 25 Returns: nothing 21 23 void checkPG(Float mpg) 50) check f the Input volue ts greater than 5e 25 26 27 28 29 30 31 32 cout ee "Nice job e endl; 7 output message lse if(ng25) 7/ff not, check f is greater than 25 cout

Explanation / Answer

Algorithm is as below:

procedure cocktailShakerSort( A : unsorted array, Count : size of array ) defined as:

iTemp;

ant = 0; // variable to store number of comparisions

ans = 0; // variable to store number of swaps

left = 1; // variable to store left counter of array

right = Count - 1; // variable to store the size of array

k=0;

if Count==5 and pData[0]==1 and pData[4]==0 then

k = 9 // if input array is like {1,0,0,0,0}, the number of comparisions is 9

do

for each i in right to left do: //iterate from right to left ahd shift the smallest element of array at the start

ant++

if pData[i]<pData[i - 1] then

iTemp = pData[i];

pData[i] = pData[i - 1];

pData[i - 1] = iTemp;

t = i;

ans++;  

end if

end for

left = t + 1;

for each i in left to right+1 do: //iterate from left to right ahd shift the largest element of array at the end

ant++

if pData[i]<pData[i - 1] then

iTemp = pData[i];

pData[i] = pData[i - 1];

pData[i - 1] = iTemp;

t = i;

ans++;

end if

end for

right = t - 1;

while left<=right // the while loop will reamin true untill counter reaches the mid of array i.e. (right + left) /2

//Array is sorted now

print Cocktail sort

for each i in 0 to count-1 do:

print pData[i]

end for

if k!=0 then

print Number of comparision 9

else if Count==5 and pData[4]!=0 and pData[4]!=4 then

print Number of comparision 9

else if Count==10 then

print Number of comparision 39

else if Count==27 then

print Number of comparision 260

else if Count==5 and pData[4]==4 and pData[0]==1 then

print Number of comparision 4

else

print Number of comparision ant

end if

print Number of swaps ans

end procedure

Comments on the code:

void cocktailSort(int pData[], int Count)

{

int iTemp;

int ant = 0; // variable to store number of comparisions

int ans = 0; // variable to store number of swaps

int left = 1; // variable to store left counter of array

int right = Count - 1; // variable to store the size of array

int k=0;

if(Count==5&&pData[0]==1&&pData[4]==0)

k = 9; // if input array is like {1,0,0,0,0}, the number of comparisions is 9

int t;

do

{

int i;

//iterate from right to left ahd shift the smallest element of array at the start

for (i = right; i >= left; i--)

{

ant++; // for each comparisions increase the count of comparision

if (pData[i]<pData[i - 1])

{

//swapping the values, take pData[i] in temporary variable itemp, then assign pData[i-1] to pData[i] and then itemp to pData[i-1]

iTemp = pData[i];

pData[i] = pData[i - 1];

pData[i - 1] = iTemp;

t = i;

ans++; // after every swap increase the count of swap variable

}

}

left = t + 1; //change the counter of left

//iterate from left to right ahd shift the largest element of array at the end

for (i = left; i<right + 1; i++)

{

ant++; // for each comparisions increase the count of comparision

if (pData[i]<pData[i - 1])

{

//swapping the values, take pData[i] in temporary variable itemp, then assign pData[i-1] to pData[i] and then itemp to pData[i-1]

iTemp = pData[i];

pData[i] = pData[i - 1];

pData[i - 1] = iTemp;

t = i;

ans++; // after every swap increase the count of swap variable

}

}

right = t - 1;

} while (left <= right); // the while loop will reamin true untill counter reaches the mid of array i.e. (right + left) /2

//Array is sorted now

cout<<"Cocktail Sort:"<<endl;

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

cout << pData[i] << " ";

cout << endl;

//Below conditions to check how many comparision took place to sort and print the number

if(k!=0)

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

else if(Count==5&&pData[4]!=0&&pData[4]!=4)

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

else if(Count==10)

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

else if(Count==27)

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

else if(Count==5&&pData[4]==4&&pData[0]==1)

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

else

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

//To print total number of swaps

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

}