Chapter 8 Sorting

Ace your homework & exams now with Quizwiz!

A decision tree is:

A model for the behavior of an algorithm based on the decision that it makes

Merge sort 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 Merge sort to work in Θ(nlogn) time? (1) A singly linked list (2) A doubly linked list (3) An array

(1),(2), and (3)

Radix Sort as implemented in the example given in the book would be a good solution for alphabetizing words. (True or False) Hints: 1. Words have widely varying lengths 2. Unless one implements a different version of Radix Sort to account for different lengths, the longest length will dictate the amount of work to be done. 3. That wastes a lot of time on short words.

False

Selection Sort is a stable sorting algorithm. Recall that a stable sorting algorithm maintains the relative order of records with equal keys. (True or False)

False

What is the worst-case time for Heapsort to sort an array of n records that each have unique key values?

Θ(nlogn)

log n! is:

Ω(nlogn)

Which is the divide-by-twos increment series for an array of 22 elements? Hints: 1. The first increment must be a power of 2 that generates sub lists with two elements. 2. Each succeeding increment must be half the size of the previous one.

16,8,4,2,1

Suppose that Selection Sort is given an input of 100 records, and it has completed 45 iterations of the main loop. How many records are now guaranteed to be in their final position? Hints: 1. On each pass Selection Sort puts a record into its final position. 2. So, if Selection Sort has done 45 passes, then at least 45 records are in their final positions.

45

What is the best case input for Shellsort?

A sorted array because each sub list is sorted in linear time

The proof that the lower bound for sorting problem is Ω(nlogn) technically only applies to comparison-based sorting. This means that we can find other approaches to solve the problem faster. (True or False)

False

The order of the input records has what impact on the number of comparisons required by Quicksort?

There is a big difference, the asymptotic running time can change

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? Hints: 1.Sorted input is the best case. 2. How many increments are there for the divide-by-two increment series? 3. With divide-by-two increments, there are log n increments.

Θ(nlogn)

Which of these is the best definition for a stable sorting algorithm? Hints: 1. "Stable" has nothing to do with how fast it is. 2. It refers to maintaining the relative order of records with equal key values. 3. In some applications, we require that records with equal key values preserve the relative order of those records.

An algorithm that does not change the relative ordering of records with identical keys.

An exchange sort is: Hints: 1. Most of the sorts that we study swap records. 2. Insertion Sort is not the only exchange sort. 3. An "exchange" means a swap of adjacent records.

Any sort where only adjacent records.

In shellsort it is illegal to have an increment of 8 when the array size is 14. (True or False) Hints: 1. There is no requirement that the number of records be divisible by the increment size. 2. Even an Increment size greater than half the number of records is legal ,it just won't cause any work to be done.

False

Quicksort is a stable sorting algorithm. Recall that a stable sorting algorithm maintains the relative order of records with equal keys. (True or False) Hints: 1. Think of behavior for partition if there are two equal key values in the array.

False

Which of the following sorting algorithms has a worst case complexity of Θ(n log n)? Hints: 1. Bubble Sort, Insertion Sort, and Selection Sort are referred to as "quadratic sorts" because of their worst-case time cost.

Heap Sort

If I is the number of inversions in an input array of n records, then {Insertion|Bubble} Sort will require how many swaps? Hints: 1. An inversion requires a swap to undo it. 2. The number of comparisons done by an algorithm is generally different from the number of swaps.

I

If I is the number of inversions in an input array of n records, then {insertion | Bubble} Sort will require how many swaps? Hints: 1. An inversion requires a swap to undo it 2. The number of comparisons done by an algorithm is generally different from the number of swaps.

I

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 using binary search will: Hints: 1. The position at which to insert could be found in Θ(log(i)) steps, but shifting the records to make room for the this record will require Θ(i) time.

Not speed up the asymptotic running time because shifting the records to make room for the insert will require Θ(i) time.

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? Hints: 1. If you pick the first or last one, then sorted input will give the worst case performance. 2. If you pick the middle value, then it is still possible to get worst-case performance. 3. But to do so requires a very specific and unusual permutation that will normally occur very rarely. 4. If all permutations were equally likely, then it wouldn't matter. But in practice, the sorted input is much more likely to occur.

It is much better to pick the middle value

Is the following a legal series of increments when running Shellsort on an array of 16 values? 1,8 Hints: 1. The values on the series must be decreasing. 2. A legal series always ends in 1.

No

Is the following a legal series of increments when running Shellsort on an array of 16 values? 6,8,4,3,1 Hints: 1. The values on the series must be decreasing. 2. A legal series always ends in 1.

No

Is the following a legal series of increments when running Shellsort on an array of 16 values? 9,3,1,1 Hints: 1. The values on the series must be decreasing. 2. A legal series always ends in 1.

No

The order of the input records has what impact on the number of comparisons required by Bubble Sort? Hints: 1. Does Bubble Sort change when it make a comparison according to the order of the array input values? 2. No, it does not matter what order the input values have

None

The order of the input records has what impact on the number of comparisons required by Merge sort? Hints: 1. Does Merge sort change when it make a comparison according to the order of the array input values? 2. No, it does not matter what order the input values have.

None

The upper bound for a problem is defined to be: Hints: 1. For a given problem, it is possible to write an algorithm that is as bad as we want. So defining a bound in terms of bad algorithms is not useful.

The cost of the best algorithm that we know for the problem

After Quicksort completes the partition function, where is the pivot? Hints: 1. When partition is called, the pivot is at the end of the partition 2. The partition operation itself does not move the pivot. That is don't afterwards by the Quicksort function itself.

The last position in the partition.

Which of these is a traditional measure for the cost of a sorting algorithms? Hints: 1. Ask what is a MEASURE of cost not what affects. 2. The memory size affects the cost, but does not measure it 3. Records being out of order can increase the cost, but not measure it

The number of swaps

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 ? Hints: 1. Does Insertion Sort change when it make a comparison according to the order of the array input values? 2. Yes, Insertion Sort might stop early or might look at many records.

There is a big difference, the asymptotic running time can change.

In Shell sort, how are the sub lists sorted?

Using Insertion Sort because Shell sort generally makes each subarray reasonably close to sorted.

When is Merge sort a good choice for sorting an array? Hints: 1. Does merge sort's number of comparisons depend on the particular order of the input array? 2. No, it does not 3. This makes it reliable in the worst case.

We need a reasonably fast algorithm with a good worst case cost.

In which case might the number of comparisons NOT be a good representation of the cost for a sorting algorithm? Hints: 1. CPU speed would affect all comparisons in the same way 2. Number of records or amount of space won't affect the value of counting comparisons 3. The longer the string the longer it takes to compare

When we are comparing strings of widely varying length

Sometimes, the constant factors in an algorithm's runtime equation are important that its growth rate. When the problem is sorting this can happen in which situation? Hints: 1. CPU speed would affect all comparisons in the same way 2. Records being sorted or reverse sorted affects the growth rate of different algorithms differently. But not relevant to this question 3. When we sort only a few records, constants matter a lot.

When we are sorting lots of small groups of records.

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: Hints: 1. Each comparison puts one record in the final sorted array. 2. You don't compare when there is only one record.

m + n - 1

In the average case, the total number of swaps done by Selection Sort is closest to: Hints: 1. Selection sort is good cause it make swaps way less

n

The total number of pairs of records among n records is: Hints: 1. There are n ways to pick the first records in the pair 2. This leaves n-1 ways to pick the second record 3. We consider pair (A,B) to be the same as pair (B,A)

n(n-1)/2

In which cases are the time complexities the same for Quicksort? Hints: 1. There are a few really bad inputs. 2. While there are a few bad inputs, they are so few as to not affect the average or best cases.

Best and Average only

Binsort's time complexity depends on the initial ordering of the keys in the input array. (True or False)

False

The order of the input records has what impact on the number of comparisons required by Radix Sort? Hints: 1. Radix Sort works differently from most sorts, it does not directly compare records 2. No, it does not matter what order the input values have.

None

Which of these will NOT affect the RELATIVE running times for two sorting algorithms? Hints: 1. If we speed up the CPU by a factor of two, both sorts will go twice as fast.

The CPU speed

When is insertion sort a good choice for sorting an array? Hints: 1. Insertion Sort if fairly simple 2. Because Insertion Sort is simple, it tends to cost only a little per comparison when compared to more complicated sorting algorithms.

The array contains only a few records

The lower bound for a problem is defined to be:

The best possible cost for any algorithm that solves the problem

If the upper and lower bounds for a problem meet then:

We can say that we understand the runtime cost of the problem.

An inversion is: Hints: 1. inversion is not a sort 2. Inversion related to swaps, but not a swap itself 3. Inversion refers to an instance of a record being out of order

When a record with key value greater than the current record's key appears before it in the array.

When is Selection Sort a good choice to use for sorting an array?

When the cost of swap is large, such as when the records are long strings.

In which cases are the time complexities the same for Heapsort? Hints: 1. Does Heapsort's cost vary according to the order of the array input values? 2. No, it does not matter what order the input values have 3. However, the best case occurs when all the values are the same.

Worst and Average

In which cases are the time complexities the same for Bubble Sort?

Worst, Average , and Best.

In which cases are the time complexities the same for Merge sort? Hints: 1. Does Merge sort's cost vary according to the order of the array input values? 2. No, it does not matter what order the input values have.

Worst, Average and Best

Is the following a legal series of Increments when running Shellsort on an array of 16 values? 8,1 Hints: 1. The values on the series must be decreasing. 2. A legal series always ends in 1.

Yes

How many ways can n distinct values be arranged? Hints: 1. There are n ways to the first record 2. Leaving n-1 for the second record 3. This means that there are n*(n-1) ways to pick the first two records 4. so on..

n!

In the worst case, the total number of comparison for Insertion Sort is closest to: Hints: 1. Insertion Sort's implementation is made up of two nested for loops 2. The outer for loop is executed n-1 times 3. The inner for loop is executed i times 4. The total cost is the sum of i's for i goes from 1 to n.

n^2/2

In the worst case, the total number of comparisons for Bubble Sort is closet to: Hints: 1. Bubble Sort's implementation is made up of two nested for loops. 2. The outer for loop is executed n-1 times. 3. The inner for loop is executed i times 4. The total cost is the sum of i's for i goes from 1 to n.

n^2/2

Assuming that the number of digits used is not excessive, the worst-case for Radix Sort when sorting n keys with distinct key values is: Hints: 1. It takes at least log n digits to represent n distinct values 2. The cost is number of digits times number of records 3. In a reasonable implementation, the number of digits won't be much worse than log n.

Θ (n log n)

How much auxiliary space or overhead is needed by Heapsort? Hints: 1. Heapsort does not require any auxiliary arrays

Θ(1)

The {best | average | worst} case time complexity of Binsort is: Hints: 1. We need a separate bin for every key value. 2. And we need to look at every such bin 3. So the cost has to be related to both the number of records and the number of bins.

Θ(n + MaxKeyValue)

The worst-case cost for Radix Sort when sorting n keys with keys in the range 0 to r^3 -1 is: Hints: Each record is processed three times

Θ(n)

What is the running time of Heapsort when the input is an array where all key values are equal?

Θ(n)

What is the running time of Insertion Sort when the Input is an array that has already been sorted? Hints: Each test in the inner loop will fail because the value at position i is never less than the value at position i-1.

Θ(n)

If I is the number of inversions in an input array of n records, then Insertion Sort will run in what time? Hints: 1. Insertion sort has to do n passes where it compares at least once 2. If the record for this pass has no remaining inversions, then it requires no work. 3. But if it does have inversions, it will need a swap for each such inversion.

Θ(n+I)

What is the best-case time for Bubble Sort to sort an array of n records? Hints: 1. Does Bubble Sort's number of comparisons depend on the particular order of the input array? 2. No, it does not

Θ(n^​2​​)

What is the running time for Bubble Sort when the input array has values that are in reverse sort order? Hints: 1. On each iteration, the ith record will have to move to the start of the array 2. This is the worst case

Θ(n^​2​​)

Average-case time for Merge sort to sort an array of n records? Hints: 1. Does Merge sort's number of comparisons depend on the particular order of the input array? 2. No, it does not

Θ(nlogn)

What is the best-case cost for Quicksort to sort an array of n elements? Hints: 1. While there are a few bad inputs, they are so few as to not affect the average or best cases. 2. The best thing that can happen is that every pivot split its partition in half. Somwthing close to this happens in the average case.

Θ(nlogn)

What is the running time for Insertion Sort when the input array has values that are in reverse sort order?

Θ(n​^2​​)

What is the worst case time for Insertion Sort to sort an array of n records?

Θ(n​^2​​)

What is the average case cost of Shellsort for a good increment series?

Θ(n√​n​​​)

Which of these inputs will cost the most for Shellsort when using divide-by-twos increments on an array with a size where n is a power 2? Hints: 1.A sorted array will result in every sub list also being sorted, which is fast. 2. An array with random input is unlikely to give the worst case. 3. A reverse-sorted array turns out not to cause any particular problem. 4. When the array size is a power of 2, the divide-by-twos increment series will keep the even-position items separate from the odd-position items until the final pass, resulting an a really expensive final pass.

An array where even positions store values 1 to n/2 and odd positions store values n/2 + 1 to n

When is Quicksort a good choice for sorting an array? Hints: 1. Quicksort doesn't change its performance based on record size

In most standard situations where you want to sort many records.

A disadvantage of Heapsort is:

It is not stable

A disadvantage of Radix Sort is:

It needs auxiliary storage beyond the input array

An important disadvantage of the first Binsort algorithm shown in this book is: Hints: 1. behavior is the same for all input 2. Each key value has to have a bin, and no key value can be repeated.

It works only for a permutation of number from 0 to n - 1

A disadvantage of Quicksort is: Hints: How does Quicksort do in the worst case?

Its worst-case running time is Θ(n​^2​​)

Which statement best characterizes Selection Sort? Recall that a stable sorting algorithm maintains the relative order of records with equal keys. 1. Think of behavior of every pass through the Inner for loop of Selection Sort if two records are equal, with the greatest value in the array. 2. Which record will be selected? 3. The first such records 4. It will be moved to the last position in the array, putting it out of order with equal-valued records 5. But we could easily change the maximum-finding part of the loop to take the last of these equal-valued records.

Selection Sort is not stable, but with minor modifications it could be made so

The order of the input records has what impact on the number of comparisons required by Heapsort? Hints: 1. Can Heapsort's behavior change depending on the outcome of a comparison? 2. Yes, it has changed things a little bit in that it might move things up and down the heap more or less. 3. But this does not matter, because removing a value from the heap normally costs log n

There is a constant factor difference

When is Bubble Sort a good choice for sorting an array?

There is no situation where Bubble Sort is the best choice over all of the others in the chapter.

How are Selection Sort and Quicksort similar? Hints: 1. Selection Sort does not divide- and- conquer?

They can both swap non-adjacent records

Binsort can be viewed as a special case of Radix Sort where the base is so big that all keys are one digit long. (True or False) Hints: 1. Binsort uses a single array 2. Each key value gets its own array position

True

Consider an array A of n records each with a unique key value, and A​(sub R) the same array in reverse order. Any given pair of records must be an inversion in exactly one of A or A(sub R) (True or False) Hints: 1. Any given pair of records with different values is either in order, or not in order. 2. Being out of order is called an inversion. 3. If your pair is in order in some array, then it must be out of order in the reverse.

True

Merge sort is easier to implement when operating on a linked list than on an array. (True or False) Hints: 1. Look at the length of the code given in the modules. 2. There are a lot of details to deal with when implementing Merge sort on an array.

True

Radix sort processes one digit at a time, and it uses a Binsort to sort the n records on that digit. However any stable sort could have been used in place of the Binsort. (True or False) Hints: 1. Using a Binsort on the records based on the current digit is a seasonable thing to do within the context of the Radix Sort 2. But, using other sorts on those digits might give the same effect 3. The key is that the previously-sorted digits do not get pushed out of order, which can be avoided by using a stable sort.

True

Selection sort is simple, but less efficient than the best sorting algorithms. (True or False) Hints: 1. What is Selection Sort's average case complexity? 2. It is Θ(n^​2​​) 3. Are there any better sorting algorithms?

True


Related study sets

Which Word to Use? - Ware, wear, where, were, we're

View Set

CS 110 - Midterm Study Notes - Part 1

View Set

Identify the survivorship curve described in each of the following

View Set

Introduction To Activated Sludge Study Guide

View Set

AWS Overview: Intro to Cloud Computing & 6 Advantages of Cloud Computing

View Set

Ch 6-(Test 3)-Fair Credit Reporting Act (FCRA)

View Set