Question

4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, ab) [5 points] Write an algorithm for the function MakeRoom, using worst-case analysis of your algorithm, counting each time a

4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort of C++ like pseudocode used in the text. Do a worst- case analysis of your algorithm, counting comparisons of num with array elements.
b) [5 points] Write an algorithm for the function MakeRoom, using worst-case analysis of your algorithm, counting each time an array element is moved. If you use any sort of swap routine, this would count as 2 moves of array elements. c) [5 points] The algorithm Insert which inserts num into A consists of calling Search and then MakeRoom. Do a worst-case analysis of Insert, counting both comparisons of num with elements in A, and counting whenever an element of A is moved
0 0
Add a comment Improve this question Transcribed image text
Answer #1


4 int Seasch Cint fa 3 p. fot j; Pas;Search Carr, η , num): intExamples index 4tn worst Cose, Search oi the Dumber ib Larycst compurnisions In best Coase g all- eei moes Case moke com T)e

Add a comment
Know the answer?
Add Answer to:
4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up t...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • 2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A (7,3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array. 3. What is the worst case for quick sort? What is...

    2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A (7,3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array. 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 4. Gi pseudocode for an algorithm that will solve the following...

  • C++: Find Time Analysis Worst case O() of each member function with explanation: 1) /** insert function:    parameters:...

    C++: Find Time Analysis Worst case O() of each member function with explanation: 1) /** insert function:    parameters: obj    Description: inserts new elements into the correct position in the    sorted array, sliding elements over the position to right, as needed **/ template <typename Object> SortedArray::void insert(const Object &obj) {    if (theSize >= theCapacity)    {        cout << "Error: there is no enough memory space. " << endl;        return;    }    else    {        for (int i = ((2 * theSize) + 1); (i...

  • Suppose you have an array S indexed from 1 to n which contains n numbers, not...

    Suppose you have an array S indexed from 1 to n which contains n numbers, not in any particular order, and you wish to count how many times a given number x occurs in S. Consider the recursive algorithm below for this which finds the number of occurrences of x in the index range i...j in S. Of course, solving the problem would involve an initial call to the algorithm for the range 1.n: int CountOccur (int i,j) { int...

  • (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is...

    (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an earray A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A i=j-1 while i > 0 and A[i] > key do Ali + 1] = Ai i=i-1 7: A[i+1] = key (a) Follow this algorithm for A[1..4) =< 7,9,6,8 >. Specifically, please indicate the contents of the array after each iteration of the outer...

  • 3 Quicksort 10 points (5 points each) 1. Suppose that you are given an array A[1..n]...

    3 Quicksort 10 points (5 points each) 1. Suppose that you are given an array A[1..n] and that you want to sort it using quicksort. Further suppose that your algorithm could consult an oracle to predict what element to use as the pivot. Which element would it pick so that your algorithm would run as fast as possible? What is the running time given your pivot? 2. Run the partition algorithm to partition the array A (6,7,2,4, 10,8, 1,9)

  • please check my answers if it wrong answer me a) (25 points) Suppose that you are...

    please check my answers if it wrong answer me a) (25 points) Suppose that you are asked to analyze the performance. Algorithms operate on 1D array of size nor 2D a of the algorithms below, write down the Big O or order of grow terms of n. If the algorithm cannot be applied to the array, write 0(1), O(log n), O(n), O(n logn), 90), O(n"), O(n!). The will only be given for reasonable Big O answers. & algorithms for their...

  • Searching/sorting tasks and efficiency analysis - Big-oh For each problem given below, do the following: 1....

    Searching/sorting tasks and efficiency analysis - Big-oh For each problem given below, do the following: 1. Create an algorithm in pseudocode to solve the problem. 2. Identify the factors that would influence the running time of your algorithm. For example, if your algorithm is to search an array the factor that influences the running time is the array size. Assign names (such as n) to each factor. 3. Count the operations performed by the algorithm. Express the count as a...

  • (5 +2 points) You are given an array A[1..n] of positive numbers where Ai] is the stock price on ...

    (5 +2 points) You are given an array A[1..n] of positive numbers where Ai] is the stock price on day i. You are allowed to buy the stock once and sell it at some point later. For each day you own the stock you pay S1 fee. Design a divide-and-conquer algorithm that will return a pair (i,j) such that buying the stock on day i and selling it on day j will maximize your gain The complexity of the algorithm...

  • can anyone provide answers with explaination ? thanks a lot I. In the example of recycling...

    can anyone provide answers with explaination ? thanks a lot I. In the example of recycling the elements of a list in O1) time, which situation holds? A. Both lists are circular B. Both ists are not circular C. The list to be recycled is circular, the garbage list is not D. The garbage list is circular, the list to be recycled is not 2. What is the worst-case time to perform MINIMUML) for a sorted, doubly-linked list with nodes?...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT