Chapter 8 OpenDSA
Which is the divide-by-twos increment series for an array of 32 elements?
16, 8, 4, 2, 1
The total number of pairs of records among nn records is:
n(n−1)/2
Selection Sort (as the code is written in this module) is a stable sorting algorithm
False
The order of the input records has what impact on the number of comparisons required by Selection Sort
None
Which of the following sorting methods will be best if the number of swaps done is the only measure of efficiency?
Selection Sort
In which cases are the time complexities the same for Mergesort?
Worst, Average and Best
You must merge 2 sorted lists of size m and n, respectively. The number of comparisons needed in the worst case by the merge algorithm will be:
m+n−1
In the worst case, the total number of comparisons for Mergesort is closest to:
nlogn
The {best|average|worst} case time complexity of Binsort is:
Θ(n+MaxKeyValue)
The array-based implementation for Mergesort uses how many arrays?
2
If it takes a given computer one second on average to run Quicksort on an array of 1000 records, how long (to the nearest thousand seconds) will it take to run Quicksort on 1,000,000 records?
2000
If we Shellsort an array of length 9, and we start with an increment size of 5, which element (if any) will be skipped on the first pass due to being in a sublist of only one element?
4
A decision tree is:
A model for the behavior of an algorithm based on the decisions that it makes
Mergesort works by splitting a list of n numbers in half, then sorting each half recursively, and finally merging the two halves. Which of the following list implementations would allow Mergesort to work in Θ(nlogn) time?
A singly linked list A doubly linked list An array
In which cases are the time complexities the same for Quicksort?
Best and Average only
Quicksort's worst-case case cost is O(n^2) and its average-case cost is O(nlogn). This means that the fraction of input cases with cost O(n^2) must
Drop as n grows
Binsort's time complexity depends on the initial ordering of the keys in the input array.
F
When is Quicksort a good choice for sorting an array?
In most standard situations where you want to sort many records
The worst-case cost for Radix Sort when sorting n keys with keys in the range 0 to r^3-1 is
O(n)
What is the running time of Insertion Sort when the input is an array that has already been sorted?
O(n)
(For the version of the algorithm as presented in this module:) What is the running time of Quicksort when the input is an array where all record values are equal?
O(n^2)
The average number of inversions in an array of nn records is n(n-1)/4. This is
O(n^2)
What is the running time of Selection Sort when the input is an array that is reverse sorted?
O(n^2)
What is the worst-case time for Selection Sort to sort an array of n records?
O(n^2)
When implementing Insertion Sort, a binary search could be used to locate the position within the first i−1 records of the array into which record i should be inserted. the worst case time will be:
O(n^2)
What is the average-case cost for Quicksort to sort an array of n elements?
O(nlogn)
After Quicksort completes the partition function, where is the pivot?
The last position in the partition
In the worst case, the total number of swaps done by Selection Sort is closest to
n
The order of the input records has what impact on the number of comparisons required by Heapsort (as presented in this module)?
There is a constant factor difference
What is the best-case time for Heapsort to sort an array of n records that each have unique key values?
Θ(nlogn)
Sometimes, the constant factors in an algorithm's runtime equation are more important that its growth rate. When the problem is sorting, this can happen in which situation?
When we are sorting lots of small groups of records.
The order of the input records has what impact on the number of comparisons required by Mergesort (as presented in this module)?
None
When implementing Insertion Sort, a binary search could be used to locate the position within the first i-1i−1 records of the array into which record ii should be inserted. Using binary search will:
Not speed up the asymptotic running time because shifting the records to make room for the insert will require Θ(i) time
We know that the worst case for Insertion Sort is about n^2/2, while the average case is about n^2/4. This means that:
The growth rates are the same and The runtime in the average case is about half that of the worst case
Which of the following is NOT relevant to the sorting problem lower bounds proof?
The worst-case cost for Bubble Sort is Θ(n^2)
The order of the input records has what impact on the number of comparisons required by Insertion Sort (as presented in this module)?
There is a big difference, the asymptotic running time can change
The order of the input records has what impact on the number of comparisons required by Quicksort (as presented in this module)?
There is a big difference, the asymptotic running time can change
How are Selection Sort and Quicksort similar?
They can both swap non-adjacent records
Shellsort
static void shellsort(int[] A) { for (int i=A.length/2; i>2; i/=2) // For each increment for (int j=0; j<i; j++) // Sort each sublist inssort2(A, j, i); inssort2(A, 0, 1); // Could call regular inssort here } /** Modified Insertion Sort for varying increments */ static void inssort2(int[] A, int start, int incr) { for (int i=start+incr; i<A.length; i+=incr) for (int j=i; (j>=incr) && (A[j] < A[j-incr]); j-=incr) Swap.swap(A, j, j-incr); }
What is the running time of Heapsort when the input is an array where all key values are equal?
Θ(n)
What is the worst-case cost for Quicksort's partition step?
Θ(n)
I is the number of inversions in an input array of n records, then Insertion Sort will run in what time?
Θ(n+I)
Assuming that the number of digits used is not excessive, the worst-case cost for Radix Sort when sorting nn keys with distinct key values is:
Θ(nlogn)
What is the best-case cost of Shellsort for an array whose size n is a power of 2 when using divide-by-twos increments?
Θ(nlogn)
What is the running time of Mergesort when the input is an array where all record values are equal?
Θ(nlogn)
What is the worst-case time for Heapsort to sort an array of n records that each have unique key values?
Θ(nlogn)
What is the average case cost of Shellsort for a good increment series?
Θ(n√n)
logn! is:
Ω(nlogn)
Consider an array AA of nn records each with a unique key value, and A_RAR the same array in reverse order. Any given pair of records must be an inversion in exactly one of AA or A_RAR.
T
Mergesort is easier to implement when operating on a linked list than on an array.
T
When is Insertion Sort a good choice for sorting an array?
The array has only a few records out of sorted order, or the array contains a few records
The lower bound for a problem is defined to be:
The best possible cost for any algorithm that solves the problem
The upper bound for a problem is defined to be:
The cost of the best algorithm that we know for the problem
What is the running time for Insertion Sort when the input array has values that are in reverse sort order?
O(N^2)
What is the running time of Insertion Sort when the input is an array where all record values are equal?
O(n)
What is the worst-case cost for Quicksort to sort an array of n elements?
O(n^2)
Radix sort processes one digit at a time, and it uses a Binsort to sort the nn records on that digit. However, any stable sort could have been used in place of the Binsort.
T
Selection Sort is generally faster than the Bubble Sort on the same input
T
If the upper and lower bounds for a problem meet then:
We can say that we understand the runtime cost of the problem
How many ways can n distinct values be arranged?
n!
How many times does Selection Sort call the swap function on an array of n records?
n-1
In the worst case, the total number of comparisons for Selection Sort is closest to
n^2/2
the worst case, the total number of comparisons for Insertion Sort is
n^2/2
How much auxilliary space or overhead (beyond the array holding the records) is needed by Heapsort?
Θ(1)
The order of the input records has what impact on the number of comparisons required by Radix Sort (as presented in this module)?
None
Mergesort (as the code is written in this module) is a stable sorting algorithm. Recall that a stable sorting algorithm maintains the relative order of records with equal keys.
T
On average, how many comparisons does Quicksort require to sort 1000 records
10k
What is the best case input for Shellsort?
A sorted array because each sublist is sorted in linear time
Which of the following sorting algorithms has a worst case complexity of Θ(nlogn)
Heap Sort, Mergesort
A disadvantage of Heapsort is:
It is not stable (i.e., records with equal keys might not remain in the same order after sorting)
Which sorting algorithm makes use of Insertion Sort's best case behavior?
Shellsort
What do we call the property of a sorting algorithm that guarantees that records with the same key value occur in the same order in the sorted list as in the original, unsorted list?
Stable
Heapsort (as the code is written in this module) is a stable sorting algorithm. Recall that a stable sorting algorithm maintains the relative order of records with equal keys.
F
Quicksort (as the code is written in this module) is a stable sorting algorithm. Recall that a stable sorting algorithm maintains the relative order of records with equal keys.
F
Radix Sort as implemented in this module would be a good solution for alphabetizing words.
F
The proof that the lower bound for the sorting problem is Ω(nlogn) technically only applies to comparison-based sorting. This means that we can find other approaches (such as radix sort) to solve the problem faster.
F
In shellsort it is illegal to have an increment of 8 when the array size is 14.
F; There is no requirement that the number of records be divisible by the increment size.
The order of the input records has what impact on the number of comparisons required by Bin Sort (as presented in this module)?
None
An exchange sort is
Any sort where only adjacent records are swapped
An exchange sort is:
Any sort where only adjacent records are swapped
Consider an array AA of n records each with a unique key value, and A_R the same array in reverse order. There are a certain number of pairs, where an arbitrary position i and position j is a pair. Between these two arrays, exactly half of these pairs must be inverted.
False
If I is the number of inversions in an input array of n records, then {Insertion|Bubble} Sort will require how many swaps?
I
A person sorting a hand of cards might reasonably use which sorting algorithm?
Insertion Sort
When selecting a pivot value, a simple thing to do is to always pick from the same place in the partition. If we use this approach, does it matter whether we always pick from the first position in the partition, the last position in the partition, or the middle position in the partition?
It is much better to pick the middle value
An important disadvantage of the first Binsort algorithm shown in this module is:
It works only for a permutation of the numbers from 0 to n−1
A disadvantage of Quicksort is:
Its worst-case running time is Θ(n^2)
What is the most complicated part of the Mergesort algorithm?
Merging the sorted halves back together
Insertion Sort (as the code is written in this module) is a stable sorting algorithm. Recall that a stable sorting algorithm maintains the relative order of records with equal keys.
True
In Shellsort, how are the sublists sorted?
Using Insertion Sort because Shellsort generally makes each subarray reasonably close to sorted
When is Mergesort a good choice for sorting an array?
We need a reasonably fast algorithm with a good worst case cost
When is Selection Sort a good choice to use for sorting an array?
When the cost of a swap is large, such as when the records are long strings
In which cases are the time complexities the same for Heapsort?
Worst and Average