Question

In: Computer Science

Design an algorithm that minimises Ccmp, the number of comparisons of two books’ labels in the...

Design an algorithm that minimises Ccmp, the number of comparisons of two books’ labels in the alphabetical order, and Cmov, the number of book movements (one movement is counted whenever a book is moved from one location to another) in the worst case. Suppose there arenbooks on the main shelf, which haveal ready been sorted in an ascending alphabetical order. The m newly arrived books are carried into the library on a portable shelf.

a) Scenario 1: The newly arrived books have also been sorted in an ascending alphabetical order and you are allowed to use a temporary shelf of an infinite capacity. Design an algorithm (description + pseudo code) for the robot to arrange the new books onto themain shelf using Ccmp=Θ(n+m) label comparisons and Cmov=Θ(n+m) book movements in the worst case. Explain why your algorithm has the stated worst case complexity (ignoring the constant and choosing the dominant term in your analysis).

Algorithm ArrangingBooksScenario1(A,B,C,n,m)

//A,B, and C represent the arrays of book labels for the main, the portable, and the temporary shelves

//COMPLETE WITH YOUR PSEUDO CODE HERE

b) Scenario 2: There is no temporary shelf to use in the library and the number of newly arrived books,m, is a small constant compared ton, for instance,m=10. Design an algorithm (description + pseudo code) for the robot to arrange the new books onto the main shelf using Ccmp=Θ(log(n)) label comparisonsin the worst case. What is the number of book movements Cmov incurred in the worst-case of your algorithm and why?

Algorithm ArrangingBooksScenario2(A,B,n,m)

// A and B represent the arrays of book labels for the main and the portable shelves

//COMPLETE WITH YOUR PSEUDO CODE HERE

P/s: I have already posted this question, but haven't received a useful idea for it. So, please help me with a useful idea and more detailed pseudocode.

Solutions

Expert Solution

So in above question

ccmp = minimum number of comparision of two books

cmov = number of book movement (one movement is counted whenever a book is moved from one location to another)

A = main(n book initially in  array) , B = portable(m book initially in array ) ,c temporary

1.) so in the first part of question given that array A and B both are sorted , to obtain final sorted result we will use concept of merging two sorted array ----

i) we take i=0 and j=0 , k=0 pointing to stating index of array A ,B and C respectively we will use a wihle loop until any of one A or B is empty.

if(A[i]<=B[i]) then in this case we will insert A[i] in temporary and i++(move one step in A array because A[i] is inserted in C array )

if(A[i]>B[i]) then in this case we will insert B[i] in temporary and j++(move one step in B array because B[j] is inserted in C array )

----after book in any of array is finished we will simplly append books of other array to C array.

void ArrangeBookScenario(A,B,C,n,m) 
{ 
    int i = 0, j = 0, k = 0; 
  
    // Traverse both array 
    while (i<n && j <m) 
    { 
 
         //inserting the book first which is lexographical smaller in order 
        if (A[i] <= B[j]) 
            C[k++] = A[i++]; 
        else
            C[k++] = B[j++]; 
    } 
  //simply appending remaining array element in case one of the array is empty
    while (i < n) 
        C[k++] = A[i++]; 
  
   
    while (j <m) 
        C[k++] = B[j++]; 

 A = C //putting books of temporary cell in main shelf
} 

2.) In This siuation first array is sorted but second is not so we will use binary search concept . we will find upper_bound(first element in array greater than given element ) for each element in array B and insert that element at index of upper_bound. after shifting array to right.

complexity o(m*logn)

//as we know in worst case binary search takes logn comparision so total comparison will be m*logn(as we are applying binary search for each elemenrt of B). ccmp = logn//because m <<< n given in question

example of worst case

//lets take a scenario that element of B are also sorted and less than every element of A so upper_bound will be first element itself so we have to shift whole array A to right .

Algo for above problem

Algorithm ArrangingBooksScenario2(A,B,n,m)
{
for(int i=0;i<B;i++)
{
int index = upper_bound(A,B[i]); //binary search based upper_bound function that will return first position where A > b[i]
 
  shiftarray(index);//shift content of array by 1 position to right from index till end of array
  A[index] = B[i]; 
}
}

Related Solutions

Design an algorithm that minimises Ccmp, the number of comparisons of two books’ labels in the...
Design an algorithm that minimises Ccmp, the number of comparisons of two books’ labels in the alphabetical order, and Cmov, the number of book movements (one movement is counted whenever a book is moved from one location to another) in the worst case. Suppose there arenbooks on the main shelf, which haveal ready been sorted in an ascending alphabetical order. The m newly arrived books are carried into the library on a portable shelf. a) Scenario 1: The newly arrived...
What is the number of comparisons in the bubble sort algorithm, if it is used to...
What is the number of comparisons in the bubble sort algorithm, if it is used to sort a list of n-entries? Justify your formula.
Design an algorithm to prompt user to enter a number. Determine the number is odd or...
Design an algorithm to prompt user to enter a number. Determine the number is odd or even with Case Logic.
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find...
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code:...
write a method called Comparisons that will return the number of comparisons to find an element...
write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code: int totalComparisons=0;...
Design an efficient algorithm to compute the number of binary strings with length n that satisfy...
Design an efficient algorithm to compute the number of binary strings with length n that satisfy 1 the regular expression ((0 + 11 + 101)∗ (1101))∗ .
(a) Design an algorithm that reveals some secret integer number from the set {1, 2, ......
(a) Design an algorithm that reveals some secret integer number from the set {1, 2, ... , n} by guessing random numbers within the given range until the secret number has been guessed. Numbers should be replaced after being guessed such that ​it is possible to guess 2 and then 2 again​, assuming 2 is in the given range. The algorithm should return both the secret number as well as the number of guesses taken. (b) If possible, calculate the...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design in place sorting algorithm? How this in place term is related to the time complexity and space complexity of the program?
How many character comparisons will the BMH algorithm perform to solve the pattern search problem shown...
How many character comparisons will the BMH algorithm perform to solve the pattern search problem shown below? text: my next door neighbor is a witch pattern: is explain c language
Please PROVE the following statement: The time complexity of any sorting algorithm that uses only comparisons...
Please PROVE the following statement: The time complexity of any sorting algorithm that uses only comparisons between elements is the following: Ωn log n in the worst case. you need to prove this , if you can please type answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT