• No se han encontrado resultados

SÍNTESIS DE LA SIMBOLOGÍA DEL MUEBLE DE ASIENTO

In document Trabajo Fin de Grado (página 32-38)

2. ESTUDIO ANALÍTICO

2.5. SÍNTESIS DE LA SIMBOLOGÍA DEL MUEBLE DE ASIENTO

Algorithms with exponential and factorial (even faster) growth rates have an effective practical upper limit on the size of problem they can be used for, even with faster and faster computers. For example, if we have an O(2n) algorithm that takes an hour for 100 inputs, adding the 101st input will take a second hour, adding 5 more inputs will take 32 hours (more than a day!), and adding 14 inputs will take 16,384 hours, which is almost 2 years! This relation is the basis for cryptographic algorithms—algorithms that encrypt text using a special key to make it unreadable by anyone who intercepts it and does not know the key. Encryption is used to provide security for sensitive data sent over the Internet. Some crypto- graphic algorithms can be broken in O(2n) time, where n is the number of bits in the key. A key length of 40 bits is considered breakable by a modern computer, but a key length of 100 (60 bits longer) is not because the key with a length of 100 bits will take approximately a billion–billion (1018) times as long as the 40-bit key to crack.

E X E R C I S E S F O R S E C T I O N 2 . 1

S E L F ‐ C H E C K

1. Determine how many times the output statement is executed in each of the following frag-

ments. Indicate whether the algorithm is O(n) or O(n2).

a. for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)

System.out.println(i + " " + j);

b. for (int i = 0; i < n; i++) for (int j = 0; j < 2; j++)

System.out.println(i + " " + j);

c. for (int i = 0; i < n; i++)

for (int j = n ‐ 1; j >= i; j‐‐) System.out.println(i + " " + j);

d. for (int i = 1; i < n; i++) for (int j = 0; j < i; j++) if (j % i == 0)

System.out.println(i + " " + j);

2. For the following T(n), find values of n0 and c such that cn3 is larger than T(n) for all n

larger than n0.

T( )n n3 5n2 20n 10

3. How does the performance grow as n goes from 2000 to 4000 for the following? Answer

the same question as n goes from 4000 to 8000. Provide tables similar to Table 2.3.

a. O(log n) b. O(n) c. O(n log n) d. O(n2)

e. O(n3)

4. According to the plots in Figure 2.3, what are the processing times at n = 20 and at n = 40 for each of the growth rates shown?

P R O G R A M M I N G

1. Write a program that compares the values of y1 and y2 in the following expressions for

values of n up to 100 in increments of 10. Does the result surprise you?

y1 = 100 * n + 10 y2 = 5 * n * n + 2

2.2 The List Interface and ArrayList Class 63

2.2 The List Interface and ArrayList Class

An array is an indexed data structure, which means you can select its elements in arbitrary order as determined by the subscript value. You can also access the elements in sequence using a loop that increments the subscript. However, you can’t do the following with an array object:

r Increase or decrease its length, which is fixed.

r Add an element at a specified position without shifting the other elements to make room. r Remove an element at a specified position without shifting the other elements to fill in the

resulting gap.

The classes that implement the Java List interface (part of Java API java.util) all provide methods to do these operations and more. Table 2.4 shows some of the methods in the Java List interface.

These methods perform the following operations:

r Return a reference to an element at a specified location (method get). r Find a specified target value (method get).

r Add an element at the end of the list (method add). r Insert an element anywhere in the list (method add). r Remove an element (method remove).

r Replace an element in the list with another (method set). r Return the size of the list (method size).

r Sequentially access all the list elements without having to manipulate a subscript. The symbol E in Table 2.4 is a type parameter. Type parameters are analogous to method parameters. In the declaration of an interface or class, the type parameter represents the data type of all objects stored in a collection.

Although all of the classes we study in this chapter support the operations in Table 2.4, they do not do them with the same degree of efficiency. The kinds of operations you intend to per- form in a particular application should influence your decision as to which List class to use. One feature that the array data structure provides that these classes don’t is the ability to store primitive‐type values. The List classes all store references to Objects, so all primitive‐ type values must be wrapped in objects. Autoboxing facilitates this.

Figure 2.4 shows an overview of the List interface and the four actual classes that implement it. We will study the ArrayList and LinkedList classes in this chapter; we will study the Stack

TA B L E 2 . 4

Methods of Interface java.util.List<E>

Method Behavior

public E get(int index) Returns the data in the element at position index

public E set(int index, E anEntry) Stores a reference to anEntry in the element at position index. Returns the data formerly at position index

public int size() Gets the current size of the List

public boolean add(E anEntry) Adds a reference to anEntry at the end of the List. Always returns true public void add(int index, E anEntry) Adds a reference to anEntry, inserting it before the item at position index int indexOf(E target) Searches for target and returns the position of the first occurrence, or –1

if it is not in the List

class Chapter 4. Class Vector has been deprecated, which means it is included for historical

reasons. Deprecated classes have been replaced by better classes and should not be used in new applications. We will briefly discuss the RandomAccess interface and the two abstract

classes AbstractList and AbstractSequentialList in Section 2.10.

The ArrayList Class

The simplest class that implements the List interface is the ArrayList class. An ArrayList

object is an improvement over an array object in that it supports all of the operations just listed. ArrayList objects are used most often when a programmer wants to be able to grow a

list by adding new elements to the end but still needs the capability to access the elements stored in the list in arbitrary order. These are the features we would need for an e‐mail address book application: New entries should be added at the end, and we would also need to find e‐mail addresses for entries already in the address book. The size of an ArrayList

automatically increases as new elements are added to it, and the size decreases as elements are removed. An ArrayList object has an instance method size that returns its current size.

Each ArrayList object has a capacity, which is the number of elements it can store. If you add

a new element to an ArrayList whose current size is equal to its capacity, the capacity is

automatically increased. ‹‹interface›› RandomAccess ‹‹interface›› List ArrayList Vector AbstractList AbstractSequentialList Stack LinkedList F I G U R E 2 . 4 The java.util.List

Interface and Its Implementers

In document Trabajo Fin de Grado (página 32-38)

Documento similar