1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
egoroff_w [7]
3 years ago
15

Design a data structure to support the following two operations for a set S of inte- gers, which allows duplicate values: • INSE

RT(S, x) inserts x into S. • DELETE-LARGER-HALF(S) deletes the largest d|S|/2e elements from S. Explain how to implement this data structure so that any sequence of m INSERT and DELETE-LARGER-HALF operations runs in amortized O(1) time per op- eration. Your implementation should also include a way to output the elements of S in O(|S|) time. Prove the running time of your implementation.
Computers and Technology
1 answer:
umka2103 [35]3 years ago
7 0

Answer and Explanation:

Note that we are free to use any data structure that allows for arbitrary insertion and deletion of data

As an underlying data structure, we’ll use an (unsorted) array. INSERT(S, x) will  simply append x to the array, increasing its length. This has a constant runtime,  so we’ll say its cost is 1.

DELETE-LARGER-HALF(S) will work as follows: first, use SELECT to find the  median. Next, use PARTITION around the median to make sure that the upper half is stored within the last [|S|/2] elements. Finally, we delete these elements,  reducing the size of the array.This has a linear running time, so we’ll say its cost is n.

To show that any m operations can run in O(m) time, we define a potential  function \phi(S) = 2|S|. The amortized cost of INSERT is thus 1 + \delta \phi = 1 + 1 = 1 ;  the amortized cost of DELETE-LARGER-HALF is n +\delta\phi\leq n-2(n/2) = 0. So the  amortized cost of any m operations is O(m).

This answer essentially captures the idea behind the problem. However, there  are some technical points to clear up. (Calling the real-time costs 1 and n are not among them; this underestimates the running time by at most a constant. Ignoring constants like that is necessary to make concise arguments about amortized costs.)

First, an array does not support arbitrary insertions. Possible remedies include:

(1) using a dynamic array along the lines of §17.4, or (2) using a different structure  like a linked list, and having DELETE-LARGER-HALF convert it to an array and  back in linear time so that the SELECT and PARTITION algorithms may be used.

Second, it’s important to know which median to partition around and how to  delete the upper half of the elements: a mistake could lead to incorrect behavior when the array has an odd size or repeated elements. We should select the lower median,[|S|/2], since that’s the number of elements we want in the lower set: as  written, the CLRS Partition function will put elements less than or equal to the

pivot in the left set, and strictly larger elements in the right set. (If the partition function is defined differently, the answer should be different as well. You generally  should give a brief description of how your partition function works.) After a call to Partition, it is safe simply to keep the first [|S|/2] elements and drop the rest. On the other hand, it is not safe to go around deleting every element with

a sufficiently large value—take an array of zeros as a drastic example. If you wish  to take that approach, you’ll have to count the number of elements equal to the  median and delete the correct number of them.

Finally, the argument only shows that the <em>amortized</em> cost with respect to \phi is  O(m). The conclusion we’re asked for requires a technical condition: the potential  \phi never drops below its initial value. This is true for the usual reason: initially,  \phi = 0 because S is empty; during execution, \phi \geq 0  by definition.

You might be interested in
Consider the following statement, which is intended to create an ArrayList named years that can be used to store elements both o
zvonat [6]

Answer:

a. ArrayList years

Explanation:

Required

Complete the code segment

The programming question is about Java programming language.

In java, the syntax to declare an ArrayList of no specific datatype is:

ArrayList [List-name] = new ArrayList();

From the question, the name of the ArrayList is years.

So, the syntax when implemented is:

<em>ArrayList years = new ArrayList();</em>

<em>So, the comment /*missing code*/ should be replaced with: ArrayList years</em>

7 0
3 years ago
The Internet may best be compared to a/an
Usimov [2.4K]
A volcano because its always erupting
3 0
3 years ago
To understand, read, and give meaning to data in
san4es73 [151]

Answer:

teh answer is b

Explanation:

which of the

following?

A interpretation

B analysis

C assessment

D data collection

6 0
3 years ago
Read 2 more answers
5.03 Describe a simple program that you’d like to create (or would ask a coding-proficient friend to create for you). Based on t
IRISSAK [1]

The simple program which I would create would be how to write "Welcome to Java" in Java Programming Language.

The best type of language to use for this simple program would be:

  • A procedural language which is Java. This is easy to understand and shows the user steps on how to make a simple sentence in Java.

<h3>The simple Java code</h3>

class Simple{

/** First, we have to create a class */

public static void main(String args[]){

/** Next thing for us to do is to create a special method and make it public so that the method can be called from outside the class */

System. out. println("Welcome to Java");

/** Here, we would input the word which we want to be printed */

}

}

Read more about procedural language here:

brainly.com/question/22654163

3 0
3 years ago
Basics of visual basic
Mazyrski [523]
Visual Basic (VB) is an object-oriented programming language, and it supports the concepts of encapsulation, abstraction, polymorphism, etc. In VB, all the variables, methods, and application entry points are encapsulated within the class definition
3 0
2 years ago
Read 2 more answers
Other questions:
  • What is looping? the rerecording of sound first recorded on set the recording of sound on set the process of combining different
    11·1 answer
  • What can be written to perform a certain number of iterations or to iterate until a specific result is achieved?
    14·1 answer
  • 11. What is the hexadecimal representation of each of the following binary numbers? a. 1100 1111 0101 0111
    6·1 answer
  • In chapter 3, we discussed syntax and semantics, in general there are two types of grammars for programming languages, regular a
    14·1 answer
  • A discription of how child abuse displays itself on social media​
    6·1 answer
  • Aubrey uses the following formula to calculate a required value. Which elements of the formula use mixed cell referencing?
    13·2 answers
  • Who here would like to play among us with me? <br> Time: Friday, November 13
    14·1 answer
  • g An OpenCl Compute Unit is composed of: Group of answer choices Processing Elements Devices Streaming Multiprocessors Platforms
    6·1 answer
  • Which option correctly describes a DDMS application?
    6·1 answer
  • What dictionary operation can you use to remove all the keys within the dictionary-named contacts?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!