Java II Chapter 17
The Quicksort algorithm
is in O(n log n) in the average case
If lower is the first subscript in a contiguous portion of an array, and upper is the last subscript, then the array item in the middle of that array portion is at subscript
(lower + upper)/2
An array of 4 elements is being sorted in ascending order using the insertion sort algorithm. How many comparisons will insertion sort perform if the array was originally sorted in descending order?
6 comparisons
An array a[ ] of N elements is being sorted using the insertion sort algorithm. The algorithm is at the last stage, with the segment of the array in positions 0 through N-2 already sorted. How many array elements will a[N-1] have to be compared to, before it can be placed into its proper position?
Could be any number of elements between 1 and N-1 (inclusive)
Suppose that we are searching for an item X in an array sorted in descending order. If we find that X is smaller than the middle item of the array, then we should continue our search in the half of the array whose elements
have higher subscripts than the middle of the array
The method findMax shown below is supposed to return the position of the largest value in the portion of an array between 0 and last, inclusive: int findMax(int array[ ], int last) { int maxPos = 0; for (int k = 1; k <= last; k++) { // Code is Missing } return maxPos; } The missing code is
if (array[k] > array[maxPos]) maxPos = k;
One can sort an array a[ ] as follows. Start by observing that at stage 0, the array segment consisting of the single element a[0] is sorted. Now suppose that at the stage k, the segment a[0..k] is sorted. Take the element a[k+1], and call it X. By moving some of the elements in a[0..k] one place to the right, create a place to put X in so that now a[0..k+1] is sorted. The algorithm that uses this strategy is called
insertion sort
The method int getPosition(int array[], int X) is designed to return the position of X within the array. If X is not in the array, the method may return either -1 or array.length. Which of the following is a correct implementation for this method?
int k = 0; while (k < array.length && array[k] != X) { k++; } return k;
Assuming a method int findMax(int array[ ], int last) that returns the subscript of the largest value in the portion of an array whose elements are at 0 through last (inclusive), a recursive method for sorting in ascending order a portion of an array between 0 and last, inclusive, can be written as follows: void rSort(int array[ ], int last) { if (last >= 1) { // Missing code } } If a method void swap(int array[ ], int pos1, int pos2) can be used to swap the contents of two array entries, then the logic for the missing code is
int p = findMax(array, last); swap(array, p, last); rSort(array, last-1);
The addition of two integers
is a basic step if there is a fixed bound on the size of the integers
The complexity function 100^n^2
is in O(n^2 /2)
For a computational problem, the input size
is the amount of memory needed to store the input
The worst case complexity function f(n) of an algorithm
is the maximum number of basic steps performed in solving a problem instance with input size n
Consider the code static void doQuickSort(int array[ ], int start, int end) { int pivotPoint; if (start < end) { pivotPoint = partition(array, start, end); doQuickSort(array, start, pivot-1); doQuickSort(array, pivot+1, end); } } In this code, the value pivotPoint returned by partition
is the position of the array element that is greater than each element of the first sublist, and less or equal to each element of the second sublist
A contiguous segment of an array is specified using two subscripts, lower and upper. Which expression gives the position of the element in the middle of the array segment?
lower + (upper - lower) / 2
A contigous segment of an array is given by specifying two subscripts, lower and upper. Which of the following expressions gives the subscript of the array element that is three quarters of the way from lower to upper?
lower + (upper — lower) * 3 / 4
The Quicksort algorithm works by
partitioning the unsorted portion of the array into two sublists and a pivot and recursively sorting the two sublists
If an algorithm with an input size of n has a nested loop, and both loops make a complete pass over the input, then the performance of the algorithm will be
quadratic time
The compareTo method of the Comparable interface
repeatedly locating the smallest value in the unsorted portion of the array and moving it toward the lower end of the array
The insertion sort algorithm works by
repeatedly taking the first value in the unsorted portion of the array and placing it at its proper place in the part of the array that is already sorted
A search for an item X in an array starts at the lower end of the array, and then looks for X by comparing array items to X in order of increasing subscript. Such a method is called
sequential search
The best method for searching an array that is not sorted is
sequential search
The two criteria most often used to measure the efficiency of an algorithm are
space and time
Assuming a method int findMax(int array[ ], int last) that returns the subscript of the largest value in the portion of an array whose elements are at 0 through last (inclusive), a method for sorting an array in ascending order can be written as follows: void sort(int array[ ]) { for (int last = array.length-1; last >=1; last --) { int maxPos = findMax(array, last); // Code is missing } } If a method void swap(int array[ ], int pos1, int pos2) can be used to swap the contents of two array entries, then the logic for the missing code is
swap(array, maxPos, last);
Let F be an algorithm with complexity function f(n), and let G be an algorithm with complexity function g(n). If the ratio f(n)/g(n) converges to 0 as n increases to infinity, then
the algorithm F is asymptotically faster than G
Let F be an algorithm with complexity function f(n), and let G be an algorithm with complexity function g(n). If there exists a positive constant K such that the ratio f(n)/g(n) is less or equal to K for all n greater or equal to 1, then
the algorithm F is asymptotically no worse than G
Let F be an algorithm with complexity function f(n), and let G be an algorithm with complexity function g(n). If the ratio f(n)/g(n) converges to infinity as n increases to infinity, then
the algorithm G is asymptotically faster than F
The maximum number of comparisons that binary search will ever need to make on an array of N elements is
the smallest integer k such that 2^k is larger or equal to N
Let F be an algorithm with complexity function f(n), and let G be an algorithm with complexity function g(n). If the ratio f(n)/g(n) converges to 2 as n increases to infinity, then
the two algorithms are equivalent in efficiency and there is no clear winner
The best way to measure the goodness of an algorithm is
to look at its worst case and average case complexity functions
To compare String objects for the purpose of sorting, a programmer should
use the compareTo method of the Comparable interface
A computational problem is
a problem that can be solved using an algorithm
A basic step is
an operation that can be executed within a constant amount of time
A search for an item X in a portion of a sorted array works by repeatedly selecting the middle item and comparing it to X. If X is not found there, the search method selects either the portion of the array to the left of the middle item, or the portion of the array to the right of the middle item, and continues the search. This method is called
binary search
If an array is known to be sorted, it can be searched very quickly by using
binary search
The binary search algorithm
cannot be used to search an array that is not sorted
If a[ ] is an array of integers, the pseudo code int k = 0 int m = 1 While m < a.length If a[m] < a[k] Then k = m End If m = m+1 End While describes a strategy for
determining the location of the smallest value in the array
Binary Search is in the complexity class
O(log n)
The bubble sort algorithm works by
The insertion sort algorithm works by
The worst case complexity function is a good measure to use when
we want a guarantee on the performance of an algorithm
The following implementation of QuickSort static void doQuickSort(int array[ ], int start, int end) { int pivotPoint; pivotPoint = partition(array, start, end); doQuickSort(array, pivot+1, end); doQuickSort(array, start, pivot-1); }
will be terminated by the system for making too many recursive calls
When applied to an array a[ ] of integers, the pseudo code Boolean sort = true int k = 0 While sort == true and k < a.length-1 If a[k] > a[k+1] Then sort = false End If k = k +1 End While
will determine if the array is arranged in ascending order
If insertion sort is used to sort an array of N elements that is already sorted in the right order, the number of array comparisons performed by insertion sort will be
N-1
On the average, performing a sequential search on an array of N elements will require
N/2 comparisons
The role of the partition(array, start, end) method in Quicksort
None of these
If an algorithm makes two separate, unnested passes over an input of size n, the performance of the algorithm will be in the class
O(n)
Linear time is the class of all complexity functions that are in
O(n)
Sequential Search is in the complexity class
O(n)
Consider the following implementation of insertion sort: public static void insertionSort(int [ ] array) { int unsortedValue; // The first unsorted value int scan; // Used to scan the array // The outer loop steps the index variable through // each subscript in the array, starting at 1. This // is because element 0 is considered already sorted. for (int index = 1; index < array.length; index++) { // The first element outside the sorted segment is // array[index]. Store the value of this element // in unsortedValue unsortedValue = array[index]; // Start scan at the subscript of the first element // outside the sorted segment. scan = index; // Move the first element outside the sorted segment // into its proper position within the sorted segment. while (scan > 0 && array[scan-1] > unsortedValue) { array[scan] = array[scan - 1]; scan --; } // Insert the unsorted value in its proper position // within the sorted segment. array[scan] = unsortedValue; } } This method uses the < and > operators to compare array subscripts, as when index is compared against the length of the array, a.length. The method also uses these operators to compare array elements against each other, for example, in an expression such as a[scan-1]>unSortedValue. What would happen if we change every < operator to >, and change every > operator to <?
The method would return, leaving the array unmodified
