CSCI 251 - Ch. 3

¡Supera tus tareas y exámenes ahora con Quizwiz!

When sorting an array with n elements, the maximum number of elements that RadixSort may put in a bucket is n. True False

true

The list is sorted into ascending order:(chopsticks, forks, knives, spork) True False

true *the list of strings are in alphabetical order

What will RadixGetMaxLength return when the array is (17, 4, 101)?

3

Quicksort The worst case runtime is ________. If unequal partitioning happens at every level, there will be ______ levels. This rarely happens!

O(N^2), N- 1

Partition Algorithm

Partition(numbers, lowIndex, highIndex) { // Pick middle element as pivot midpoint = lowIndex + (highIndex - lowIndex) / 2 pivot = numbers[midpoint] done = false while (!done) { // Increment lowIndex while numbers[lowIndex] < pivot while (numbers[lowIndex] < pivot) { lowIndex += 1 } // Decrement highIndex while pivot < numbers[highIndex] while (pivot < numbers[highIndex]) { highIndex -= 1 } // If zero or one elements remain, then all numbers are // partitioned. Return highIndex. if (lowIndex >= highIndex) { done = true } else { // Swap numbers[lowIndex] and numbers[highIndex] temp = numbers[lowIndex] numbers[lowIndex] = numbers[highIndex] numbers[highIndex] = temp // Update lowIndex and highIndex lowIndex += 1 highIndex -= 1 } } return highIndex }

Which fast sorting algorithm's worst case runtime complexity is worse than O(NlogN)? Quicksort Heap sort Radix sort

Quicksort Quicksort's worst case runtime complexity is O(N^2), which is worse than O(NlogN)

Recursive Quicksort Algorithm

Quicksort(numbers, lowIndex, highIndex) { // Base case: If the partition size is 1 or zero // elements, then the partition is already sorted if (lowIndex >= highIndex) { return } // Partition the data within the array. Value lowEndIndex // returned from partitioning is the index of the low // partition's last element. lowEndIndex = Partition(numbers, lowIndex, highIndex) // Recursively sort low partition (lowIndex to lowEndIndex) // and high partition (lowEndIndex + 1 to highIndex) Quicksort(numbers, lowIndex, lowEndIndex) Quicksort(numbers, lowEndIndex + 1, highIndex) }

What will RadixGetLength(17) evaluate to?

2 After 2 divisions, digits is set to 2 and the loop completes. Then 2 is returned.

Merge Sort numbers = (1, 2, 3, 4, 5), i = 0, k = 4 j = _____

2 j = (i + k) / 2 = (0 + 4) / 2 = 2 j is the midpoint in the list that divides the list into two halves.

ShellSort will properly sort an array using any collection of gap values, provided the collection contains 1. True False

true

The fastest average runtime complexity of a comparison sorting algorithm is O(NlogN)

true

The list is sorted into ascending order:(great, greater, greatest) True False

true

The list is sorted into descending order:(20, 15, 10, 5, 0) True False

true

The list is sorted into descending order:(99.87, 99.02, 67.93, 44.10) True False

true

The list is sorted into descending order:(F, D, C, B, A) True False

true

Assume radix sort has sorted integers by absolute value to produce the array (-12, 23, -42, 73, -78), and is about to build the negative and non-negative buckets to complete the sort. What integers will be placed into the negative bucket? Type answer as: 15, 42, 98

-12, -42, -78

Assume radix sort has sorted integers by absolute value to produce the array (-12, 23, -42, 73, -78), and is about to build the negative and non-negative buckets to complete the sort. After reversal, what integers are in the negative bucket? Type answer as: 15, 42, 98

-78, -42, -12

Assume radix sort has sorted integers by absolute value to produce the array (-12, 23, -42, 73, -78), and is about to build the negative and non-negative buckets to complete the sort. What is the final array after RadixSort concatenates the two buckets? Type answer as: 15, 42, 98

-78, -42, -12, 23, 73

RadixGetLength

// Returns the length, in number of digits, of value RadixGetLength(value) { if (value == 0) return 1 digits = 0 while (value != 0) { digits = digits + 1 value = value / 10 } return digits }

RadixGetMaxLength

// Returns the maximum length, in number of digits, out of all elements in the array RadixGetMaxLength(array, arraySize) { maxDigits = 0 for (i = 0; i < arraySize; i++) { digitCount = RadixGetLength(array[i]) if (digitCount > maxDigits) maxDigits = digitCount } return maxDigits }

Radix sort 51, 47, 96, 52, 27 When placing integers using the 1's digit, how many integers will be in bucket 5? 0 1 2

0

Given the call InsertionSortInterleaved(values, 10, 0, 5), what are the indices of the first two elements compared? 1 and 5 1 and 6 0 and 4 0 and 5

0 and 5 *The code initializes one index to the starting index of 0 and the other index to the starting index of 0 plus the gap value of 5. 0 + 5 = 5.

Quicksort numbers = (200, 11, 38, 9), lowIndex = 0, highIndex = 3 midpoint = ______

1

Radix sort 51, 47, 96, 52, 27 When placing integers using the 10's digit, how many will be in bucket 9? 0 1 2

1

When sorting the array (57, 5, 501) with RadixSort, what is the largest number of integers that will be in bucket 5 at any given moment?

1

Given list (9, 8, 7, 6, 5), how many swaps will occur during the first pass of the outer loop (i = 0)?

1 *One swap occurs during each pass of the outer loop. The swap occurs at the end of the outer loop, once the search for the smallest element (inner loop) has completed.

Assume insertion sort's goal is to sort in ascending order. Given list (10, 11, 12, 13, 14, 15), how many comparisons will be made during the third outer loop execution (i = 3)?

1 *Only a single comparison is needed for elements already in sorted order.

Assume radix sort has sorted integers by absolute value to produce the array (-12, 23, -42, 73, -78), and is about to build the negative and non-negative buckets to complete the sort. What integers will be placed into the non-negative bucket? Type answer as: 15, 42, 98

23, 73

The merge sort algorithm uses _____ index variables to keep track of the elements to sort for each recursive function call.

three

Merge Sort numbers = (34, 78, 14, 23, 8, 35), i = 3, k = 5 Left partition = ( ________ )

23,8 Elements at indices 3 to 4 are in the left partition. This partition will again be divided into two halves. Each of those halves will have one element.

Insertion Sort Given list (22, 24, 26, 31, 33, 39, 29, 32, 36), when i is 6, how many swaps will be performed in the inner loop?

3

Merge Sort How many recursive partitioning levels are required for a list of 8 elements?

3

Quicksort numbers = (1, 2, 3, 4, 5), lowIndex = 0, highIndex = 4 pivot = ______

3

Merge Sort numbers = (1, 2, 3, 4, 5), i = 0, k = 4 Left partition = ( ________ )

1, 2, 3

Selection Sort (3, 8, 4, 1, 9) What is the list after the first outer loop iteration?

1, 8, 4, 3, 9

Quick sort How many partitioning levels are required for a list of 1024 elements?

10 log_2 1024 = 10. Assuming equal-size partitions, for N elements, the number of levels is log_2 N.

Insertion Sort Given list (10, 20, 12, 11, 19, 44, 84, 35), what is the list after three passes of the outer loop?

10, 11, 12, 20, 19, 44, 84, 35

How many times longer will sorting a list of 500 elements take compared to a list of 50 elements?

100 *500^2 / 50^2 = 250000 / 2500 = 100. Selection sort's runtime grows quadratically with the input size. If the input increases in size by X times, the runtime increases X^2 times.

Quicksort How many total comparisons are required to sort a list of 1024 elements?

10240 log_2 1024 * 1024 = 10 * 1024 = 10240 comparisons. There will be log_2 N partitioning levels, and each level requires N comparisons.

Merge Sort How many recursive partitioning levels are required for a list of 2048 elements?

11

Quicksort numbers = (200, 11, 38, 9), lowIndex = 0, highIndex = 3 pivot = _______

11

How many times is InsertionSortInterleaved called if ShellSort is called with gap array (10, 2, 1)? 3 12 13 20

13 *10 calls for the gap value of 10, 2 calls for the gap value of 2, and 1 call for the gap value of 1. 10 + 2 + 1 = 13

Insertion sort Given list (20, 14, 85, 3, 9), what value will be in the 0th element after the first pass over the outer loop (i = 1)?

14 *Each outer loop pass inserts one element into the sorted part starting with the element at index 1. In this case, 14 is inserted at index 0, and 20 is moved to index 1. The sorted part is (14, 20) and the unsorted part is (85, 3, 9).

When using selection sort to sort a list with 18 elements, what is the minimum number of assignments to indexSmallest?

17 *A list of 18 elements causes 17 outer loop iterations because the outer loop iterates from 0 to 18−1. Therefore, indexSmallest is assigned a minimum of 17 times.

Quicksort numbers = (1, 2, 3, 4, 5), lowIndex = 0, highIndex = 4 midpoint = _______

2

Radix sort 51, 47, 96, 52, 27 When placing integers using the 1's digit, how many integers will be in bucket 7? 0 1 2

2

Shell Sort Assume a list with 6 elements. If a gap value of 2 is chosen, how many interleaved lists will be sorted? 1 2 3 6

2

Shell Sort Assume a list with 6 elements. With a gap value of 3, how many items will be in each interleaved list? 1 2 3 6

2 *Making 3 lists from a list with 6 items means that each interleaved list will have 2 items.

Insertion Sort Given list (1, 9, 17, 18, 2), how many swaps will occur during the outer loop execution (i = 4)?

3 *2 will be swapped with 18, 17, and 9, after which the element is in the correct position and all elements are sorted.

Shell Sort Assume a list with 6 elements. With a gap value of 3, how many interleaved lists will be sorted? 1 2 3 6

3 *The gap value specifies the number of interleaved lists.

Quick sort How many partitioning levels are required for a list of 8 elements?

3 1st: (a, b, c, d) (e, f, g, h) 2nd: (a, b) (c, d) (e, f) (g, h) 3rd: (a) (b) (c) (d) (e) (f) (g) (h)

Quicksort numbers = (55, 7, 81, 26, 0, 34, 68, 125), lowIndex = 3, highIndex = 7 pivot = ______

34

Merge Sort numbers = (34, 78, 14, 23, 8, 35), i = 3, k = 5 Right partition = ( ________ )

35

Insertion Sort Using the Big O runtime complexity, how many times longer will sorting a list of 20 elements take compared to sorting a list of 10 elements?

4

Merge Sort numbers = (34, 78, 14, 23, 8, 35), i = 3, k = 5 j = ______

4

Selection Sort How many times longer will sorting a list of 20 elements take compared to sorting a list of 10 elements?

4 *A list with twice as many elements requires 4 times as many comparisons.

Shell Sort Assume a list with 6 elements. If a gap value of 4 is chosen, how many interleaved lists will be sorted? A gap value of 4 cannot be used on an array with 6 elements. 2 3 4

4 *The gap value equals the number of interleaved lists. In this case, 2 of the 4 interleaved lists will have 2 elements each, and the other 2 interleaved lists will have 1 element each.

Merge Sort numbers = (1, 2, 3, 4, 5), i = 0, k = 4 Right partition = ( ________ )

4, 5

In the worst case, assuming each comparison takes 1 µs, how long will insertion sort algorithm take to sort a list of 10 elements? (in microseconds)

45 *(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) * 1 µs = 45 µs

When using selection sort, how many times longer will sorting a list of 63 elements take compared to sorting a list of 9 elements?

49

Selection Sort When sorting a list with 50 elements, indexSmallest will be assigned to a minimum of _____ times.

49 *The outer loop always assigns to indexSmallest at the beginning of each iteration. The inner loop never assigns to indexSmallest if the list is already sorted. A list of 50 elements causes 49 outer loop iterations, and thus a minimum of 49 assignments to indexSmallest.

Insertion sort Given list (24, 27, 30, 33, 34, 31), what is the value of i when the first swap executes?

5

Quicksort numbers = (55, 7, 81, 26, 0, 34, 68, 125), lowIndex = 3, highIndex = 7 midpoint = ________

5

Assume insertion sort's goal is to sort in ascending order. Given list (10, 11, 12, 13, 14, 7), how many comparisons will be made during the final outer loop execution (i = 5)?

5 *7 is the smallest element, and will be compared to and swapped with all other elements before reaching the correct position at index 0.

Assume selection sort's goal is to sort in ascending order. Given list (9, 8, 7, 6, 5), what value will be in the 0th element after the first pass over the outer loop (i = 0)?

5 *Each outer loop iteration moves the smallest element in the unsorted part, in this case 5, to the i-th position. In other words, the correct element for the i-th position is "selected", hence the algorithm's name.

Given the call InsertionSortInterleaved(values, 4, 1, 4), what is the initial value of the loop variable i? 0 1 4 5

5 *i is initialized to startIndex + gap, which in this case is 1 + 4 = 5.

Given list (5, 9, 8, 7, 6) and i = 1, what will be the list after completing the second outer loop iteration? Type answer as: 1, 2, 3

5, 6, 8, 7, 9

Merge Sort How many elements will the temporary merge list have for merging two partitions with 250 elements each?

500 The temporary list must be large enough to merge all elements from the two partitions.

Insertion Sort Given list (10, 20, 6, 14, 7), what will be the list after completing the second outer loop iteration (i = 2)? Type answer as: 1, 2, 3

6, 10, 20, 14, 7

Assume insertion sort's goal is to sort in ascending order. Given list (18, 23, 34, 75, 3), how many total comparisons will insertion sort require?

7 *Iterations 1, 2, and 3 requires 1 comparison each, and iteration 4 requires 4 comparisons.

Merge sort is a fast sorting algorithm. True False

true

Insertion Sort Algorithm

InsertionSort(numbers, numbersSize) { i = 0 j = 0 temp = 0 // Temporary variable for swap for (i = 1; i < numbersSize; ++i) { j = i // Insert numbers[i] into sorted part // stopping once numbers[i] in correct position while (j > 0 && numbers[j] < numbers[j - 1]) { // Swap numbers[j] and numbers[j - 1] temp = numbers[j] numbers[j] = numbers[j - 1] numbers[j - 1] = temp --j } } }

Radix sort 51, 47, 96, 52, 27 All integers would be in bucket 0 if using the 100's digit. True False

true

Merge Algorithm

Merge(numbers, i, j, k) { // Size of merged partition mergedSize = k - i + 1 // Position to insert merged number mergePos = 0 // Position of elements in left partition leftPos = 0 // Position of elements in right partition rightPos = 0 // Dynamically allocates temporary array // for merged numbers mergedNumbers = new int[mergedSize] // Initialize left partition position leftPos = i // Initialize right partition position rightPos = j + 1 // Add smallest element from left or right partition to merged numbers while (leftPos <= j && rightPos <= k) { if (numbers[leftPos] <= numbers[rightPos]) { mergedNumbers[mergePos] = numbers[leftPos] ++leftPos } else { mergedNumbers[mergePos] = numbers[rightPos] ++rightPos } ++mergePos } // If left partition is not empty, add remaining elements to merged numbers while (leftPos <= j) { mergedNumbers[mergePos] = numbers[leftPos] ++leftPos ++mergePos } // If right partition is not empty, add remaining elements to merged numbers while (rightPos <= k) { mergedNumbers[mergePos] = numbers[rightPos] ++rightPos ++mergePos } }

Merge Sort Algorithm

MergeSort(numbers, i, k) { j = 0 if (i < k) { // Find the midpoint in the partition j = (i + k) / 2 // Recursively sort left and right partitions MergeSort(numbers, i, j) MergeSort(numbers, j + 1, k) // Merge left and right partition in sorted order Merge(numbers, i, j, k) } }

Quicksort has several partitioning levels, the first level dividing the input into 2 parts, the second into 4 parts, the third into 8 parts, etc. At each level, the algorithm does at most ______ comparisons moving the lowIndex and highIndex indices.

N

Insertion Sort If a list has N elements, the outer loop executes ______ times.

N - 1

Selection Sort If a list has N elements, the outer loop executes ________ times.

N - 1

If the pivot yields two equal-sized parts, then there will be log N levels, requiring the ________ comparisons.

N x log N

Merge sort At each level, the algorithm does about N comparisons selecting and copying elements from the left and right partitions, yielding ______ comparisons

N x log N

Insertion Sort The inner loop executes on average _____ times.

N/2

Selection Sort If a list has N elements, the inner loop executes an average of ______ times

N/2

The merge sort algorithm's runtime is ________

O( N log N)

The quicksort's runtime is typically ________

O(N log N)

For sorted or nearly sorted inputs, insertion sort's runtime is _________

O(N)

Merge sort requires ________ additional memory elements for the temporary array of merged elements.

O(N)

Insertion Sort The total number of comparisons is proportional to ______

O(N^2)

Insertion sort's typical runtime is ______

O(N^2)

Selection Sort The total number of comparisons is proportional to ________

O(N^2)

The selection sort algorithm runtime is _________

O(N^2)

Radix sort is a fast sorting algorithm. True False

true

Shell Sort Algorithm

ShellSort(numbers, numbersSize, gapValues) { for each (gapValue in gapValues) { for (i = 0; i < gapValue; i++) { InsertionSortInterleaved(numbers, numbersSize, i, gapValue) } } }

a collection of integer values that all share a particular digit value. Ex: Values 57, 97, 77, and 17 all have a 7 as the 1's digit

bucket

a sorting algorithm that operates on an array of elements that can be compared to each other

element comparison sorting algorithm

Calling ShellSort with gap array (7, 3, 1) vs. (3, 7, 1) produces the same result with no difference in efficiency. True False

false

Insertion sort is a fast sorting algorithm. True False

false

The RadixSort() function shown above also works on an array of floating-point values. True False

false

The list is sorted into ascending order:(3, 9, 44, 18, 76) True False

false

InsertionSortInterleaved will result in an out of bounds array access if called on an array of size 4, a starting index of 1, and a gap value of 4. True False

false *Although i is initialized to 5, which is outside of the array bounds, the for loop's condition of i < arrSize means the loop body is never entered and the array is not accessed at index 5.

If a gap value of 2 is chosen, then the following 2 function calls will fully sort a list: InsertionSortInterleaved(list, 9, 0, 2) InsertionSortInterleaved(list, 9, 1, 2) True False

false *The two InsertionSortInterleaved calls sort two interleaved lists, but do not guarantee the entire list is sorted.

Radix sort Integers will be placed into buckets based on the 1's digit. More buckets are needed for an array with one thousand integers than for an array with one hundred integers. True False

false Base-10 integers have 10 possible values (0-9) for a digit, meaning 10 buckets are necessary regardless of array size.

A fast sorting algorithm's worst case runtime complexity must be O(NlogN) or better. True False

false Fast sorting algorithms are classified based on average case, not worst case, runtime complexity.

RadixSort has a space complexity of O(1). True False

false Radix sort always uses 10 buckets, but the number of integers in the buckets increases as the array gets larger. Because buckets collectively must store the entire array's contents of n elements, the space complexity of RadixSort is O(n).

Radix sort Consider integers X and Y, such that X < Y. X will always be in a lower bucket than Y. True False

false The buckets depend on what digit is being used to place the integers into buckets. Ex: X=19 and Y=22. X is < Y, but when using the 1's digit, X will be in bucket 9 and Y in bucket 2. On the other hand, if using the 10's digit, then X would be in a lower bucket.

a sorting algorithm that has an average runtime complexity of O(NlogN) or better

fast sorting algorithm

a positive integer representing the distance between elements in an interleaved list.

gap value

Selection sort algorithm

i = 0 j = 0 indexSmallest = 0 temp = 0 for (i = 0; i < numbersSize - 1; ++i) { // Find index of smallest remaining element indexSmallest = i for (j = i + 1; j < numbersSize; ++j) { if (numbers[j] < numbers[indexSmallest]) { indexSmallest = j } } // Swap numbers[i] and numbers[indexSmallest] temp = numbers[i] numbers[i] = numbers[indexSmallest] numbers[indexSmallest] = temp }

Shell sort begins by choosing a gap value K and sorting K interleaved lists in place. Shell sort finishes by performing a standard _______ sort on the entire array.

insertion

a sorting algorithm that treats the input as two parts, a sorted part and an unsorted part, and repeatedly inserts the next value from the unsorted part into the correct location in the sorted part.

insertion sort

Merge sort The index variable i is the index of first element in the list, and the index variable k is the index of the last element. The index variable j is used to divide the list into two halves. Elements from i to j are in the left half, and elements from ______ to k are in the right half.

j + 1

Merge sort divides the input in half until a list of 1 element is reached, which requires _______ partitioning levels.

log N

a sorting algorithm that divides a list into two halves, recursively sorts each half, and then merges the sorted halves to produce a sorted list. The recursive partitioning continues until a list of 1 element is reached, as a list of 1 element is already sorted.

merge sort

a list that only contains a few elements not in sorted order. Ex: (4, 5, 17, 25, 89, 14)

nearly sorted list

To partition the input, quicksort chooses a _______ to divide the data into low and high parts

pivot

a sorting algorithm that repeatedly partitions the input into low and high parts (each part unsorted), and then recursively sorts each of those parts.

quicksort

What are the fastest sorting algorithms?

quicksort, merge sort, heap sort, and radix sort

Which sorting algorithm is not a comparison sorting algorithm?

radix sort

a sorting algorithm designed specifically for integers. The algorithm makes use of a concept called buckets and is a type of bucket sort.

radix sort

a sorting algorithm that treats the input as two parts, a sorted part and an unsorted part, and repeatedly selects the proper next value to move from the unsorted part to the end of the sorted part.

selection sort

what are the slowest sorting algorithms?

selection sort, insertion sort, and shell sort

a sorting algorithm that treats the input as a collection of interleaved lists, and sorts each list individually with a variant of the insertion sort algorithm. This algorithm uses gap values to determine the number of interleaved lists.

shell sort

the process of converting a list of elements into ascending (or descending) order

sorting

Sorting signed integers in radix sort

the algorithms allocates two buckets, one for negative integers and the other for non-negative integers. The algorithm iterates through the array in order, placing negative integers in the negative bucket and non-negative integers in the non-negative bucket. The algorithm then reverses the order of the negative bucket and concatenates the buckets to yield a sorted array.

Radix sort All integers from an array could be placed into the same bucket, even if the array has no duplicates. True False

true Ex: When using the 1's digit for the array (10, 20, 30, 40), each integer will be in bucket 0, even though each integer is distinct.

Selection sort can be used to sort an array of strings. True False

true Selection sort is a comparison sorting algorithm, and a string can be compared against another string, so selection sort can sort an array of strings.

When sorting an array of n 3-digit integers, RadixSort's worst-case time complexity is O(n). True False

true With all integers being 3 digits, maxDigits is the constant 3 and RadixSort has a constant number of iterations in the outer loop. Each inner loop is O(n), making the algorithm O(n).


Conjuntos de estudio relacionados

Bill of Rights (First 10 Amendments to the Constitution

View Set

Research Methods Chapter 10: Experimentation and Validity

View Set

Chapter 17: Maximizing Comfort for the Laboring Woman

View Set

EQ3: What spheres of influence are contested by superpowers and what are the implications of this?

View Set

Individual Life Insurance Contract - Provisions and Options

View Set

Anatomy Ch.15- The Special Senses

View Set

Presidential communication (Unit 2)

View Set

Investigating God's World Chapter 8

View Set

Mortgage Loan Origination - ProSchools - Loan Fraud

View Set