Question

In: Computer Science

TO BE DONE IN C LANGUAGE BEFORE READING THE QUESTION PLEASE NOTE THAT YOUR FUNCTION SHOULD...

TO BE DONE IN C LANGUAGE

BEFORE READING THE QUESTION PLEASE NOTE THAT YOUR FUNCTION SHOULD EXACTLY PRODUCE THE GIVEN OUTPUT AND THE TIME COMPLEXITY SHOULD BE O(N^2)

YOU NEED TO KNOW ABOUT COCKTAIL SHAKER SORT ALGORITHM:

Cocktail-shaker sort is a modification of Bubble sort. Cocktail-shaker sort traverses the data structure in one direction, pushing the largest element ahead towards one end of the data structure, and then in the opposite direction, pushing the smallest element towards the other end, until all elements are sorted.

Implement cocktailshaker_sort that sorts the array data with length data_len. It also prints out data right at the beginning, i.e., before any sorting has occurred, and every time one element has reached its correct location SEE SAMPLE OUTPUT! (this means likely after moving one element all the way up or down respectively).

DO NOT CHANGE THE PARAMETER NAMES

TIME COMPLEXITY: O(N^2) , SEE THE GIVEN OUTPUT!

void cocktailshaker_sort(int *data, const int data_len) {
// your implementation goes here
}

INPUT:

1 10 8 20 5 4

OUTPUT:

[1, 10, 8, 20, 5, 4]
[1, 8, 10, 5, 4, 20]
[1, 4, 8, 10, 5, 20]
[1, 4, 8, 5, 10, 20]
[1, 4, 5, 8, 10, 20]

PLEASE MAKE SURE TO IMPLEMENT IN A WAY SO THAT THE DESIRED OUTPUT IS PRINTED

Solutions

Expert Solution

Answer:

C program to Implement cocktailshaker_sort that sorts the array data to get output value as per your requirment:

#include <stdio.h>

void Swap(int* a, int* b)
{
   *a ^= *b;
   *b ^= *a;
   *a ^= *b;
}

void CocktailSort(int* data, const int data_len)
{
   while (1)
   {
       char flag;
       int start[2] = { 1, data_len - 1 };
       int end[2] = { data_len, 0 };
       int inc[2] = { 1, -1 };

       for (int it = 0; it < 2; ++it)
       {
           flag = 1;

           for (int i = start[it]; i != end[it]; i += inc[it])
           {
               if (data[i - 1] > data[i])
               {
                   Swap(data + i - 1, data + i);
                   flag = 0;
               }
           }
          
           for (int i = 0; i < data_len; ++i)
           printf("%d%s", data[i], i == data_len - 1 ? "\n" : " "); // code to get output value as per your requirment
           if (flag)
           return;
       }
   }
}

int main(void) {
   int b[] = { 1, 10, 8, 20, 5, 4 };
   int i;
   size_t len = sizeof(b)/sizeof(b[0]);
printf("\nINPUT:\n");
for (i = 0; i < len; i++)
printf("%d%s", b[i], i == len - 1 ? "\n" : " ");
printf("\nOUTPUT:\n");
for ( i = 0; i < len; ++i)
printf("%d%s", b[i], i == len - 1 ? "\n" : " ");
CocktailSort(b, len);
   return 0;
}

Snapshot of the program execution:


Related Solutions

Language C++ *note* Your function count should match those given in the description; you have no...
Language C++ *note* Your function count should match those given in the description; you have no need for extra functions beyond that, and you should not have less. -Write a function, doTheRegression(), that takes as input a variable of type integer and returns a value of type double. It computes and returns 80% of the value given as the amount after reduction. -In your main function, write code that asks the user what the original amount is, and if they...
Please Use the STL library for this question and Should be done in C++ and Provide...
Please Use the STL library for this question and Should be done in C++ and Provide screenshots of the OUTPUT Topic: Stack Infix to postfix conversion         Take Input mathematical expression in infix notation, and convert it into a postfix notation. The infix notation may or may not have round parenthesis. Postfix expression evaluation         Take Input mathematical expression in postfix form, evaluate it, and display the result The mathematical expression can contain Numbers as operands (numbers can be of...
What factors should be examined before opening business? Note: The answer should be in simple language.
What factors should be examined before opening business? Note: The answer should be in simple language.
please do this in C++! I want to understand it, it must be done before the...
please do this in C++! I want to understand it, it must be done before the evening or nightime. Follow instructions exactly as it says. Please send a screenshot also with your code so I can see how it is supposed to be formatted. Since typing it a chegg answer, makes it look a bit messy. Your program will read in a file of commands. There are three types of commands: Warrior creates a new warrior with the specified name...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an integer n and returns 1 if n is a perfect number, otherwise it will return 0. If the sum of a number’s proper divisors are equal to the number, than the number is called a perfect number. For example, 6 is a perfect number: 6=1+2+3.
on C++ language please and if you can also put note on it to better undertand...
on C++ language please and if you can also put note on it to better undertand it. Write a program that reads data from a data file, the value of which is provided at the end of the problem. Your program is to incorporate the following requirements: Data to the program is input from a file of an unspecified length; that is, the program does not know in advance how many numbers are in the file. Save the output of...
Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global...
Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global variables should be used. PART A: (Minusculo Inn) Minusculo Inn has only eight guest rooms: Room Number Amenities 101 1 king size bed 102, 103,104 2 double beds 201, 202 1 queen size bed 203, 204 1 double bed & sofa bed Write a program that asks for the room number and displays the amenities. If the user enters a room number that does...
PLEASE NOTE: THIS QUESTION HAS BEEN POSTED BEFORE BUT A WRONG ANSWER WAS PROVIDED. PLEASE DO...
PLEASE NOTE: THIS QUESTION HAS BEEN POSTED BEFORE BUT A WRONG ANSWER WAS PROVIDED. PLEASE DO NOT COPY THAT ANSWER AND PASTE IT HERE AGAIN. I WOULD APPRECIATE A FRESH ATTEMPT. HERE IS THE QUESTION: 'The conclusion is that the more convex bond outperforms the less convex bond'. Explain this statement with the help of a diagram. Your answer should include a brief definition of ‘convex’ in this context.
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
in c++, please provide only a function. the function should be named "tally." it takes no...
in c++, please provide only a function. the function should be named "tally." it takes no parameters but returns an int. the first int should be 0, next being 1, then 2, etc.. will rate if correct. again should only be 1 function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT