Book Cover
Chapter 2 array
Problem1. Insert a data item into an unordered array:
DA. the time consumed is proportional to the array size. B. Multiple comparisons are required. C. You need to move other data items to provide space. D. No matter how many data items exist, it takes the same time. Solution: unordered array insertion is the only algorithm we have seen so far (within the scope of this chapter) that has nothing to do with the number of data items in the array. New data items are always placed in the next free place, regardless of the number of data items N in the array, a single insert always uses the same time. 2. Questions: When deleting data items from an unordered array, you need to move other data items to fill in holes in most cases. True solution: the deletion operation in the ordered array and unordered array is slow because the data item must be moved forward to fill the hole in the deleted data item. 3. In an unordered array, repeat is allowed, resulting in: B A. All operation times will increase. B. In some cases, the search time increases. C. The insert time is always increased. D. Sometimes the insertion time is reduced. Solution: Repeat may complicate the search algorithm: even if the last data item is matched, it must continue searching for possible matches until the last data item. 4. Random: When you look for a data item not in the array from an unordered array, it is usually faster than finding a data item. False solution: searches for the array items in an unordered array until now (within the scope of this chapter). During the search process, it compares one by one with the data items in the array (linear search ), if the last data item is still not matched, the value is not in the array. 5. To create an array in Java, you need to use the Keyword: New. Solution: Java treats the array as an object. Therefore, you must use the new operator when creating an array. 6. If Class A uses Class B, the methods in Class A should be well understood. B. It is more desirable for Class B to interact with program users. C. complicated operations are put in Class. D. The more operations class B can perform, the better. Solution: If a class is used by many programmers, it must be designed to be easy to use. The class user connects to the class through the class interface. Because class fields are often private, when we discuss interfaces, they often refer to class methods, what they are used for and what their parameters are. By calling these methods, class users interact with class objects. One of the most important advantages of object-oriented programming is that class interfaces can be designed as convenient and efficient as possible. 7. when Class A uses Class B, the methods and fields in Class B that can be accessed by Class A are called Class B: interface 8. compared with the unordered array, the ordered array is d. faster deletion. B. Insert faster. C. Faster creation. D. Faster search. Solution: What are the benefits of using ordered arrays? The main advantage is that the search speed is much faster than that of the unordered array. The disadvantage is that in the insert operation, because all the data on the back must be moved to open up space, the speed is slow. The deletion operation in the ordered array and unordered array is slow because the data item must be moved forward to fill the holes in the deleted data item. 9. logarithm is an exponential inverse function. Solution: the inverse function of an exponential function is called a logarithm. 10. the base-10 logarithm of 1000 is: log10 (1000) = 3 11. the maximum number of data items to be checked for Binary Search in an array containing 200 data items is B a.200. B .8. C.1. D.13. Solution:
Steps |
Possible value range |
0 |
1-200 |
1 |
1-100 |
2 |
1-50 |
3 |
1-25 |
4 |
1-12 |
5 |
1-6 |
6 |
1-3 |
7 |
1-1 |
8 |
1 |
12. The base-64 logarithm of 2 is: log2 (64) = 6 13. Question: The base-2 logarithm of 100 is 2. False 14. The large O notation indicates: A. How is the speed of the Algorithm related to the number of data items. B. The running time of an algorithm that contains a given data structure. C. The running time of the algorithm containing the specified number of data items. D. How is the size of the data structure related to the number of data items. Solution: the essence of the big O notation is not to give the actual value for the running time, but to express how the running time is affected by the number of data items. 15. O (1) indicates the time when a constant is executed for an operation. 16. Simple type variables and object variables can be stored in arrays.
Programming job
Programming assignments help to consolidate understanding of the content in this chapter and demonstrate how to apply the concepts in this chapter. 2.1 Add a method named getmax () to the higharray class of the higharray. Java program (listing 2.3). It returns the value of the maximum keyword in the array and-1 if the array is empty. Add some code to main () to use this method. It can be assumed that all keywords are positive numbers. 2.2 modify the method in programming job 2.1 so that not only the maximum keyword is returned, but also the keyword is deleted from the array. Name this method removemax (). The removemax () method in 2.3 programming job 2.2 provides a way to sort arrays by keyword values. To implement a sorting scheme, you only need to modify the code in main () without modifying the higharray class. This method requires the second array. The group data items are sorted in reverse order at the end of sorting. (This method is used to select a variant of sorting in "simple sorting" in Chapter 3rd .) 2.4 modify the orderedarray. Java program (list 2.4) so that the insert (), delete () and find () methods use binary lookup, as is recommended in the book. 2.5 Add a merge () method to the ordarray class of the orderedarray. Java program (listing 2.4) to combine two ordered source numbers into an ordered destination array. Add code in main (), insert a random number to the two source arrays, call the Merge () method, and display the result array. The number of data items in the two source arrays may be different. In the algorithm, you need to first compare the keywords in the source array, and select the smallest data item from it to copy it to the destination array. At the same time, we also need to consider how to solve the problem that when the data items in the source array have been obtained and the other has some data items left. 2.6 Add a nodup () method to the higharray class of the higharray. Java program (listing 2.3) so that all repeated data items in the array can be deleted. That is, if the keyword of three data items in the array is 17, the nodup () method deletes two of them. You do not have to consider maintaining the order of data items. One way is to first compare each data item with other data items, and overwrite duplicate data items with null (or a special value that is not used in a real keyword. Delete all the null values, and reduce the size of the array.