Chapter 16 Sorting Algorithms
12. True or False: If data is sorted in descending order, it means it is ordered from lowest value to highest value.
false
14. True or False: The maximum number of comparisons performed by the sequential search on an array of n elements is n/2.
false
10. This sorting algorithm begins by sorting the initial portion of the array consisting of two elements. Then, the third element is moved to its correct position, relative to the first two elements. At that point, the first three elements are in sorted order. This process continues with the fourth and subsequent elements until the entire array is sorted.
insertion sort
Typically, for an array of n items, the sequential search will locate an item in _____ attempts.
n/2
compareTo method works: If the calling object is less than the object passed as an argument, the method returns a ________number.
negative
compareTo method works: If the calling object is greater than the object passed as an argument, the method returns a ______number.
positive
The _____ sort works like this: The smallest value in the array is located and moved to position 0. Then, the next smallest value is located and moved to position 1. This process continues until all of the elements have been placed in their proper order.
selection
9. In this sorting algorithm, the smallest value in the array is located and moved to position 0. Then the next smallest value is located and moved to position 1. This process continues until all of the elements have been placed in their proper order.
selection sort
1. This search algorithm steps sequentially through an array, comparing each item with the search value.
sequential search
3. This search algorithm will search half the array on average.
sequential search
The ____ _____algorithm uses a loop to step sequentially through an array, starting with the first element. It compares each element with the value being searched for and stops when the value is found or the end of the array is encountered
sequential search
The binary search algorithm requires that its data already be _______.
sorted
A ___ _____ is a technique for scanning through an array and rearranging its contents in some specific order.
sorting algorithm
11. True or False: If data is sorted in ascending order, it means it is ordered from lowest value to highest value.
true
13. True or False: The average number of comparisons performed by the sequential search on an array of n elements is n/2.
true
compareTo method works: If the calling object is equal to the object passed as an argument, the method returns ______.
0
16.6 On average, with an array of 20,000 elements, how many comparisons will the sequential search perform? (Assume the items being searched have equal probability of being found at any of the positions in the array.)
10,000
16.7 With an array of 20,000 elements, what is the maximum number of comparisons the binary search will perform?
15
16.9 What is a basic operation?
A basic operation is one that requires constant time, regardless of the size of the problem that is being solved.
16.18 Assuming g(n)≥1 for all n≥1 show that every function in O(g(n) + 100) is also in O(g(n)).
Assuming that g(n)≥1 for all n≥1, we have 100≤100g(n) for all n≥1. This implies that g(n)+100≤g(n)+100g(n)=101g(n) for all n≥1. Now if f(n) is in O(g(n)+100), there exists a positive K such that f(n)≤K(g(n)+100)≤101Kg(n) for all n≥1. Taking K1=101K, we see that f(n)≤K1g(n) for all n≥1.
16.11 One algorithm needs 100n basic operations to process an input of size n, and another algorithm needs 25n basic operations to process the same input. Which of the two algorithms is more efficient?
Because 100n and 25n differ by a constant factor and constant factors are not significant, the two algorithms are equivalent in efficiency.
16.1 Which of the sorting algorithms that we discussed makes several passes through an array and causes the larger values to move gradually toward the end of the array with each pass?
Bubble sort
If we want to write a bubbleSort method that can accept an array of objects, a good solution is to require that the objects in the array implement the ________interface.
Comparable
The ________interface, which is in the java.lang package, specifies a method named compareTo, which is used to compare the calling object with another object that is passed as an argument.
Comparable
16.2 One of the sorting algorithms that we discussed works like this: It begins by putting the initial portion of the array consisting of the first two elements in sorted order. Then the third element is moved to its correct position, relative to the first two elements. At that point the first three elements are in sorted order. This process continues with the fourth and subsequent elements until the entire array is sorted. Which algorithm is this?
Insertion sort
16.17 Show that every function in O(20) also is in O(1).
Let f(n) be in O(20). Then there is a positive K such that f(n)≤20K for all n≥1. Let K1=20K. Then f(n)≤1K1 for all n≥1, so f(n) is in O(1).
16.8 If a sequential search is performed on an array, and it is known that some items are searched for more frequently than others, how can the contents of the array be reordered to improve the average performance of the search?
The items frequently searched for can be stored near the beginning of the array.
16.15 Let a[ ] and b[ ] be two integer arrays of size n. Following the examples of this section, give a formal description of the problem of determining if every element of a[ ] is also an element of b[ ]. The output of the algorithm should be one of the words "true" or "false."
The problem definition and algorithm are as follows: MAIN ALGORITHM INPUT: two integer arrays a[ ] and b[ ] of size n SIZE OF INPUT: The number n of array entries OUTPUT: true if each element of a[ ] is also an element of b[ ], false otherwise boolean contained = true int i = 0 While contained && i < n do contained = LINSEARCH(a[i], b) i++; End While print contained SEARCH ALGORITHM INPUT: an integer X and an array b[ ] of size n SIZE OF INPUT: The number n of array entries RETURN: true if X is contained in b[ ], false otherwise boolean LINSEARCH(int X, int [ ] b) Begin int k = 0; boolean found = false; While !found && k < n do If (X == b[k]) found = true; else k++ END If End While return found End LINSEARCH
16.5 Describe the difference between the sequential search and the binary search.
The sequential search algorithm simply uses a loop to step through each element of an array, comparing each element's value with the value being searched for. The binary search algorithm, which requires the values in the array to be sorted in order, starts searching at the element in the middle of the array. If the middle element's value is greater than the value being searched for, the algorithm next tests the element in the middle of the first half of the array. If the middle element's value is less than the value being searched for, the algorithm next tests the element in the middle of the last half of the array. Each time the array tests an array element and does not find the value being searched for, it eliminates half of the remaining portion of the array. This method continues until the value is found, or there are no more elements to test. The binary search is more efficient than the sequential search.
16.10 What is the worst case complexity function of an algorithm?
The worst case complexity function f (n) of an algorithm is a measure of the time required by the algorithm to solve a problem instance of size n that requires the most time.
16.13 What does it mean to say that f(n) is in O(g(n) )??
To say that f(n) is in O(g(n)) means that there is a positive constant K such that f(n)≤Kg(n) for all n≥1. This means that for large problem sizes, an algorithm with complexity function f(n) is no worse than one with complexity function g(n).
16.12 What does it mean to say that f(n) is not in O(g(n))??
To say that f(n) is not in O(g(n)) means that f(n) cannot be bounded by any constant multiple of g(n).
16.14 Show that 100n3+50n2 +75 is in O( 20 n 3 ) by finding a positive K that satisfies
To show that 100n3+50n2+75 is in O(20n3), observe that 100 n 3 + 50 n 2 + 75 20 n 3 = 5 + 5 2 n + 75 20 n 3 ≤ 5 + 5 + 75 ≤ 85 for all n≥1. Take K=85 in Equation 17.1.
7. This sorting algorithm makes several passes through an array and causes the larger values to gradually move toward the end of the array with each pass.
bubble sort
The ___ _____starts by comparing the first two elements in the array. If element 0 is greater than element 1, they are exchanged.
bubble sort
This particular algorithm is called the _____ ______ because it makes several passes through the elements of the array and the larger values "bubble" toward the end of the array with each pass.
bubble sort
Simple algorithms like the ______ sort and the ____ search are inefficient because they access array elements so many times.
bubble, sequential
The results obtained from timing an application depend on many variables, including the programming language used and on the quality of the compiler used to generate machine code. A better approach is to _______ the number of basic steps an algorithm requires to process an input of a given size.
count
6. If an array is sorted in this order, the values are stored from highest to lowest.
descending
If the values are sorted in ______ _______, they are stored from highest to lowest.
descending order
16.4 One of the algorithms we discussed divides an array into two sublists, with a pivot value between them. It arranges the sublists so that all the values less than the pivot are stored in the left sublist and all the values greater than the pivot are stored in the right sublist. The algorithm recursively sorts the sublists in this same way. When all of the recursive calls have completed, the array is completely sorted. Which algorithm is this?
Quicksort
8. This sorting algorithm recursively divides an array into sublists.
Quicksort
More efficient algorithms like _____ and the ____search are preferable, especially on large arrays
Quicksort, binary
16.16 Give an algorithm for solving the problem you defined in Checkpoint 16.15, and determine its worst case complexity.
See solution of 16.15 for the algorithm. In the worst case, the main algorithm makes n calls to the LINSEARCH procedure. Each call to LINSEARCH makes at most n2 comparisons. Therefore, the algorithm makes a total of comparisons.
16.3 One of the sorting algorithms that we discussed works like this: The smallest value in the array is located and moved to element 0. Then the next smallest value is located and moved to element 1. This process continues until all of the elements have been placed in their proper order. Which algorithm is this?
Selection sort
_____ ______are used to arrange data into some order.
Sorting algorithms
5. If an array is sorted in this order, the values are stored from lowest to highest.
ascending
If an array is sorted in _______ _________, it means the values in the array are stored from lowest to highest.
ascending order
2. This search algorithm repeatedly divides the portion of an array being searched in half.
binary search
4. This search algorithm requires that the array's contents be sorted.
binary search
Powers of 2 are used to calculate the maximum number of comparisons the ___ ____ will make on an array of any size. Simply find the smallest power of 2 that is greater than or equal to the number of elements in the array.
binary search
The ____ _____ is a clever algorithm that is much more efficient than the sequential search. Its only requirement is that the values in the array must be sorted in some order.
binary search