Chapter 9

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Selection sort

selection sort is 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. The term "selection" comes from the fact that for each iteration of the outer loop, a value is selected for position i.

Assume insertion sort's goal is to sort in ascending order. 1) 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)? 2) 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 3) Given list (1, 9, 17, 18, 2), how many swaps will occur during the outer loop execution (i = 4)?

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). 2) 6, 10, 20, 14, 7 6 < 20, so 6 and 20 are swapped. 6 < 10, so 6 and 10 are swapped. The sorted part is now (6, 10, 20), and the unsorted part is (14, 7). 3) 3 2 will be swapped with 18, 17, and 9, after which the element is in the correct position and all elements are sorted.

For each question, assume a list with 6 elements. 1) With a gap value of 3, how many interleaved lists will be sorted? 1 2 3 6 2) With a gap value of 3, how many items will be in each interleaved list? 1 2 3 6 3) If a gap value of 2 is chosen, how many interleaved lists will be sorted? 1 2 3 6 4) 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

1) 3 The gap value specifies the number of interleaved lists. 2) 2 Making 3 lists from a list with 6 items means that each interleaved list will have 2 items. 3) 2 The gap value equals the number of interleaved lists. 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.

1) When sorting a list with 50 elements, indexSmallest will be assigned to a minimum of _____ times. 2)How many times longer will sorting a list of 20 elements take compared to sorting a list of 10 elements? 3)How many times longer will sorting a list of 500 elements take compared to a list of 50 elements?

1) 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. 2) 4 A list with twice as many elements requires 4 times as many comparisons. 3) 100 5002/ 502= 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 X2times.

Assume selection sort's goal is to sort in ascending order. 1) 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)? 2) Given list (9, 8, 7, 6, 5), how many swaps will occur during the first pass of the outer loop (i = 0)? 3) 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

1) 5 Each outer loop iteration moves the smallest element in the unsorted part, in this case 5, to the ithposition. In other words, the correct element for the ithposition is "selected", hence the algorithm's name. 2) 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. 3) 5, 6, 8, 7, 9 The sorted part is just (5) and the unsorted part is (9, 8, 7, 6). The second outer loop iteration will swap the first element in the unsorted part (9) with the smallest element in the unsorted part (6).

1) The list is sorted into ascending order:(3, 9, 44, 18, 76) True False 2) The list is sorted into descending order:(20, 15, 10, 5, 0) True False 3) The list is sorted into descending order:(99.87, 99.02, 67.93, 44.10) True False 4) The list is sorted into descending order:(F, D, C, B, A) True False 5) The list is sorted into ascending order:(chopsticks, forks, knives, spork) True False 6) The list is sorted into ascending order:(great, greater, greatest) True False

1) False 44 is not less than 18. 2) True Each element is greater than the next element. 3) True Each element is greater than the next element. 4) True The list of characters are in reverse alphabetical order. 5) True The list of strings are in alphabetical order. 6) True The first five characters of each string are equivalent. "great" contains the fewest characters, so "great" is less than "greater" or "greatest". The second 'r' in "greater" is less than the 's' in "greatest", so "greater" is less than "greatest".

1) Bubble sort uses a single loop to sort the list. True False 2) Bubble sort only swaps adjacent elements. True False 3) Bubble sort's best and worst runtime complexity is O(N2). True False

1) False Bubble sort uses 2 loops, the second of which is nested inside the first. 2) True Bubble sort only swaps adjacent elements, so numerous swaps are used to move elements into sorted order. Other sorting algorithms can swap non-adjacent elements, which is often important for faster sorting. 3) True Bubble sort's runtime is always O(N2), making the algorithm impractical for real-world scenarios.

1) A fast sorting algorithm's worst case runtime complexity must be O(NlogN) or better. True False 2) Which fast sorting algorithm's worst case runtime complexity is worse than O(NlogN)? Quicksort Heap sort Radix sort

1) True Fast sorting algorithms are classified based on average case, not worst case, runtime complexity. 2) Quicksort Quicksort's worst case runtime complexity is O(N2), which is worse than O(NlogN).

Binary search algorithm

Binary search is a faster algorithm for searching a list if the list's elements are sorted and directly accessible (such as an array). Binary search first checks the middle element of the list. If the search key is found, the algorithm returns the matching location. If the search key is not found, the algorithm repeats the search on the remaining left sublist (if the search key was less than the middle element) or the remaining right sublist (if the search key was greater than the middle element). Binary search efficiently searches sorted list by reducing the search space by half each iteration.

Bubble Sort

Bubble sort is a sorting algorithm that iterates through a list, comparing and swapping adjacent elements if the second element is less than the first element. Bubble sort uses nested loops. Given a list with N elements, the outer i-loop iterates N - 1 times. Each iteration moves the ith largest element into sorted position. The inner j-loop iterates through all adjacent pairs, comparing and swapping adjacent elements as needed, except for the last i pairs that are already in the correct position,. Because of the nested loops, bubble sort has a runtime of O(N2). Bubble sort is often considered impractical for real-world use because many faster sorting algorithms exist.

Insertion sort for interleaved lists

If a gap value of K is chosen, creating K entirely new lists would be computationally expensive. Instead of creating new lists, shell sort sorts interleaved lists in-place with a variation of the insertion sort algorithm. The insertion sort algorithm variant redefines the concept of "next" and "previous" items. For an item at index X, the next item is at X + K, instead of X + 1, and the previous item is at X - K instead of X - 1.

Insertion sort runtime

Insertion sort's typical runtime is O(N2). If a list has N elements, the outer loop executes N - 1 times. For each outer loop execution, the inner loop may need to examine all elements in the sorted part. Thus, the inner loop executes on average N2 times. So the total number of comparisons is proportional to (N−1)⋅(N2), or O(N2). Other sorting algorithms involve more complex algorithms but faster execution.

Assume quicksort always chooses a pivot that divides the elements into two equal parts. 1) How many partitioning levels are required for a list of 8 elements? 2) How many partitioning "levels" are required for a list of 1024 elements? 3) How many total comparisons are required to sort a list of 1024 elements?

1) 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) 2) 10 log21024 = 10. Assuming equal-size partitions, for N elements, the number of levels is log2N. 3) 10240 log2 1024 * 1024 = 10 * 1024 = 10240 comparisons.There will be log2 N partitioning levels, and each level requires N comparisons.

1) When sorting an array of n 3-digit integers, RadixSort's worst-case time complexity is O(n). True False 2) When sorting an array with n elements, the maximum number of elements that RadixSort may put in a bucket is n. True False 3) RadixSort has a space complexity of O(1). True False 4) RadixSort can be used to sort an array of strings. True False

1) 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). 2) True If all integers in the array share a digit, then all integers will be put into the same bucket. Ex: If every element in the array were 7, then every element would go into bucket 7. 3) 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). 4) False RadixSort makes use of digits, and thus only works for integers. Ex: The statement bucketIndex = abs(array[i] / pow10) % 10 specifically requires the array element to be an integer.

1) In the worst case, assuming each comparison takes 1 µs, how long will insertion sort algorithm take to sort a list of 10 elements? __________________µs 2) 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?

1)45 (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) * 1 µs = 45 µs 2) 4 202/ 102= 400 / 100 = 4. A list with twice as many elements requires 4 times as many comparison.

Fast sorting algorithm

A fast sorting algorithm is a sorting algorithm that has an average runtime complexity of O(NlogN) or better. The table below shows average runtime complexities for several sorting algorithms.

Best and worst case runtime complexity

A fast sorting algorithm's best or worst case runtime complexity may differ from the average runtime complexity. Ex: The best and average case runtime complexity for quicksort is O(NlogN), but the worst case is O(N2).

Gap Value

A gap value is a positive integer representing the distance between elements in an interleaved list. For each interleaved list, if an element is at index i, the next element is at index i + gap value.

Insertion sort runtime for nearly sorted input

For each outer loop execution, if the element is already in sorted position, only a single comparison is made. Each element not in sorted position requires at most N comparisons. If there are a constant number, C, of unsorted elements, sorting the N - C sorted elements requires one comparison each, and sorting the C unsorted elements requires at most N comparisons each. The runtime for nearly sorted inputs is O((N - C) * 1 + C * N) = O(N).

Nearly sorted lists

For sorted or nearly sorted inputs, insertion sort's runtime is O(N). A nearly sorted list only contains a few elements not in sorted order. Ex: (4, 5, 17, 25, 89, 14) is nearly sorted having only one element not in sorted position.

Worst case runtime

For typical unsorted data, such equal partitioning occurs. However, partitioning may yield unequal sized part in some cases. If the pivot selected for partitioning is the smallest or largest element, one partition will have just 1 element, and the other partition will have all other elements. If this unequal partitioning happens at every level, there will be N - 1 levels, yielding (N−1)⋅N), which is O(N2). So the worst case runtime for the quicksort algorithm is O(N2). Fortunately, this worst case runtime rarely occurs.

Insertion sort algorithm

Insertion sort is 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.

Linear search vs. binary search

Linear search may require searching all list elements, which can lead to long runtimes. For example, searching for a contact on a smartphone one-by-one from first to last can be time consuming. Because a contact list is sorted, a faster search, known as binary search, checks the middle contact first. If the desired contact comes alphabetically before the middle contact, binary search will then search the first half and otherwise the last half. Each step reduces the contacts that need to be searched by half.

Merge sort algorithm

Merge sort merges the two sorted partitions into a single list by repeatedly selecting the smallest element from either the left or right partition and adding that element to a temporary merged list. Once fully merged, the elements in the temporary merged list are copied back to the original list.

Recursively sorting partitions

Once partitioned, each partition needs to be sorted. Quicksort is typically implemented as a recursive algorithm using calls to quicksort to sort the low and high partitions. This recursive sorting process continues until a partition has one or zero elements, and thus already sorted.

Radix sort algorithm

Radix sort is a sorting algorithm specifically for an array of integers: The algorithm processes one digit at a time starting with the least significant digit and ending with the most significant. Two steps are needed for each digit. First, all array elements are placed into buckets based on the current digit's value. Then, the array is rebuilt by removing all elements from buckets, in order from lowest bucket to highest

Selection sort runtime

Selection sort may require a large number of comparisons. The selection sort algorithm runtime is O(N2). If a list has N elements, the outer loop executes N - 1 times. For each of those N - 1 outer loop executions, the inner loop executes an average of N2 times. So the total number of comparisons is proportional to (N−1)⋅N2, or O(N2). Other sorting algorithms involve more complex algorithms but have faster execution times.

Shell sort algorithm

Shell sort begins by picking an arbitrary collection of gap values. For each gap value K, K calls are made to the insertion sort variant function to sort K interleaved lists. Shell sort ends with a final gap value of 1, to finish with the regular insertion sort. Shell sort tends to perform well when choosing gap values in descending order. A common option is to choose powers of 2 minus 1, in descending order. Ex: For an array of size 100, gap values would be 63, 31, 15, 7, 3, and 1. This gap selection technique results in shell sort's time complexity being no worse than O(N3/2). Using gap values that are powers of 2 or in descending order is not required. Shell sort will correctly sort arrays using any positive integer gap values in any order, provided a gap value of 1 is included.

Shell Sort

Shell sort is 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. Shell sort uses gap values to determine the number of interleaved lists. Shell sort begins by choosing a gap value K and sorting K interleaved lists in place. Shell sort finishes by performing a standard insertion sort on the entire array. Because the interleaved parts have already been sorted, smaller elements will be close to the array's beginning and larger elements towards the end. Insertion sort can then quickly sort the nearly-sorted array. Any positive integer gap value can be chosen. In the case that the array size is not evenly divisible by the gap value, some interleaved lists will have fewer items than others.

Merge sort runtime

The merge sort algorithm's runtime is O(N log N). Merge sort divides the input in half until a list of 1 element is reached, which requires log N partitioning levels. At each level, the algorithm does about N comparisons selecting and copying elements from the left and right partitions, yielding N * log N comparisons. Merge sort requires O(N) additional memory elements for the temporary array of merged elements. For the final merge operation, the temporary list has the same number of elements as the input. Some sorting algorithms sort the list elements in place and require no additional memory, but are more complex to write and understand. To allocate the temporary array, the Merge() function dynamically allocates the array. mergedNumbers is a pointer variable that points to the dynamically allocated array, and new int[mergedSize] allocates the array with mergedSize elements. Alternatively, instead of allocating the array within the Merge() function, a temporary array with the same size as the array being sorted can be passed as an argument.

Quicksort runtime

The quicksort algorithm's runtime is typically O(N log N). 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 N comparisons moving the l and h indices. If the pivot yields two equal-sized parts, then there will be log N levels, requiring the N * log N comparisons.

Partitioning

To partition the input, the quicksort algorithm divides the array into two parts, referred to as the low partition and the high partition. All values in the low partition are less than or equal to the pivot value. All values in the high partition are greater than or equal to the pivot value. The values in each partition are not necessarily sorted. Ex: Partitioning (4, 34, 10, 25, 1) with a pivot value of 10 results in a low partition of (4, 10, 1) and a high partition of (34, 25). Values equal to the pivot may appear in either or both of the partitions.

Binary search efficiency

-Binary search is incredibly efficient in finding an element within a sorted list -During each iteration or step of the algorithm, binary search reduces the search space (i.e., the remaining elements to search within) by half. -The search terminates when the element is found or the search space is empty (element not found). For an N element list, the maximum number of steps required to reduce the search space to an empty sublist is ⌊log2N⌋+1. Ex: ⌊log232⌋+1=6.

For each question, 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. 1) What integers will be placed into the negative bucket? Type answer as: 15, 42, 98 (_________________________) 2) What integers will be placed into the non-negative bucket? Type answer as: 15, 42, 98 (_________________________) 3) After reversal, what integers are in the negative bucket? Type answer as: 15, 42, 98 (_________________________) 4) What is the final array after RadixSort concatenates the two buckets? Type answer as: 15, 42, 98 (_________________________)

1) -12, -42, -78 The list of negative integers, in order, is (-12, -42,-78). 2) 23, 73 The list of non-negative integers, in order, is (23, 73). 3) -78, -42, -12 The original list was (-12, -42, -78), so the reversed list of negative integers is (-78, -42, -12). Reversal is needed because the original sorting by absolute value doesn't have the negative bucket's list start with the smallest (most negative) value. 4) -78, -42, -12, 23, 73 The negative bucket (-78, -42, -12) and non-negative bucket (23, 73) are concatenated, yielding the final sorted array of (-78, -42, -12, 23, 73).

1) 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 2) Given the call InsertionSortInterleaved(values, 4, 1, 4), what is the initial value of the loop variable i? 0 1 4 5 3) 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 4) 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

1) 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. 2) 5 i is initialized to startIndex + gap, which in this case is 1 + 4 = 5. 3) 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. 4) False The two InsertionSortInterleaved calls sort two interleaved lists, but do not guarantee the entire list is sorted.

Assume insertion sort's goal is to sort in ascending order. 1) Given list (10, 11, 12, 13, 14, 15), how many comparisons will be made during the third outer loop execution (i = 3)? 2) Given list (10, 11, 12, 13, 14, 7), how many comparisons will be made during the final outer loop execution (i = 5)? 3) Given list (18, 23, 34, 75, 3), how many total comparisons will insertion sort require?

1) 1 Only a single comparison is needed for elements already in sorted order. 2) 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. 3) 7 Iterations 1, 2, and 3 requires 1 comparison each, and iteration 4 requires 4 comparisons.

Linear and binary search efficiency. 1)Suppose a list of 1024 elements is searched with linear search. How many distinct list elements are compared against a search key that is less than all elements in the list? ___________________ elements 2) Suppose a sorted list of 1024 elements is searched with binary search. How many distinct list elements are compared against a search key that is less than all elements in the list? ____________________ elements

1) 1024 Linear search assumes the elements are not sorted and will check all elements if the key is not found. Thus all 1024 elements are compared against the search key. 2) 10 A binary search will search indices 511, 255, 127, 63, 31, 15, 7, 3, 1, and 0. 10 distinct list elements are compared against the search key.

Trace the merge operation by determining the next value added to mergedNumbers. 14 18 35 17 18 49 1) leftPos = 0, rightPos = 3 2) leftPos = 1, rightPos = 3 3) leftPos = 1, rightPos = 4 4) leftPos = 2, rightPos = 4 5) leftPos = 3, rightPos = 4 6) leftPos = 3, rightPos = 5

1) 14 14 is added to mergedNumbers: (14). leftPos is incremented. 2) 17 17 is added to mergedNumbers: (14, 17). rightPos is incremented. 3) 18 18 is added to mergedNumbers (14, 17, 18). leftPos is incremented. 4) 35 35 is added to mergedNumbers (14, 17, 18, 35). leftPos is incremented. 5) 38 Left partition is empty, so remaining elements from right partition are added. 38 is added to mergedNumbers (14, 17, 18, 35, 38). rightPos is incremented. 6) 49 49 is added to mergedNumbers (14, 17, 18, 35, 38, 49). Both partitions are empty. mergedNumbers is copied back to numbers.

For each question, consider the array of integers: 51, 47, 96, 52, 27. 1) When placing integers using the 1's digit, how many integers will be in bucket 7? 0 1 2 2) When placing integers using the 1's digit, how many integers will be in bucket 5? 0 1 2 3)When placing integers using the 10's digit, how many will be in bucket 9? 0 1 2 4)All integers would be in bucket 0 if using the 100's digit. True False

1) 2 47 and 27 will be in bucket 7. 2) 0 No integers have 5 as the 1's digit. 3) 1 96 will be in bucket 9. 4) True Because every integer is less than 100, the 100's digit in each integer is implicitly 0.

1) What will RadixGetLength(17) evaluate to? 2) What will RadixGetMaxLength return when the array is (17, 4, 101)? 3) 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) 2 After 2 divisions, digits is set to 2 and the loop completes. Then 2 is returned. 2) 3 101 has the largest length, 3 digits, among all the integers in the array. 3) 1 When processing the 1's digit, only 5 has a digit value of 5. When processing the 10's digit, only 57 has a digit value of 5. When processing the 100's digit, only 501 has a digit value of 5. Bucket 5 never contains more than one integer.

Given list: ( 4, 11, 17, 18, 25, 45, 63, 77, 89, 114 ). 1) How many list elements will be checked to find the value 77 using binary search? 2) How many list elements will be checked to find the value 17 using binary search? 3) Given an array with 32 elements, how many list elements will be checked if the key is less than all elements in the list, using binary search?

1) 2 Binary search first checks the value 25 at index 4 ((0+9)/2 = 4), then searches the right sublist at index 7 ((5+9)/2 = 7) finding the value 77. 2) 3 Binary search first checks the value 25 at index 4 ((0+9)/2 = 4), then searches the left sublist at index 1 ((0+3)/2 = 1), and then searches the right sublist at index 2 ((2+3)/2 = 2), finding the value 17. 3) 5 Binary search checks indices 15, 7, 3, 1, and 0. After checking index 0, the algorithm returns -1, indicating the key was not found.

Assume quicksort always chooses the smallest element as the pivot. 1) Given numbers = (7, 4, 2, 25, 19), i = 0, and k = 4, what are the contents of the low partition? Type answer as: 1, 2, 3 2) How many partitioning "levels" are required for a list of 5 elements? 3) How many partitioning "levels" are required for a list of 1024 elements? 4) How many total comparisons are required to sort a list of 1024 elements?

1) 2 The low partition is (2), and the high partition is (4, 7, 25, 19). 2) 4 1st: (a) (b, c, d, e) 2nd: (a) (b) (c, d, e) 3rd: (a) (b) (c) (d, e) 4th: (a) (b) (c) (d) (e) 3) 1023 In the worst case, for N elements, there will be N - 1 levels. 4) 1047552 (1024 - 1) * 1024 = 1023 * 1024 = 1047552 comparisons.

Determine the index j and the left and right partitions. 1) numbers = (1, 2, 3, 4, 5), i = 0, k = 4 j = 2) numbers = (1, 2, 3, 4, 5), i = 0, k = 4 Left partition = () 3) numbers = (1, 2, 3, 4, 5), i = 0, k = 4 Right partition = () 4) numbers = (34, 78, 14, 23, 8, 35), i = 3, k = 5 j = 5) numbers = (34, 78, 14, 23, 8, 35), i = 3, k = 5 Left partition = () 6) numbers = (34, 78, 14, 23, 8, 35), i = 3, k = 5 Right partition = ()

1) 2 j = (i + k) / 2 = (0 + 4) / 2 = 2j is the midpoint in the list that divides the list into two halves. 2) 1, 2, 3 Elements at indices from i to j, inclusive, are in the left partition. 3) 4, 5 Elements at indices from j + 1 to k are in right partition. 4) 4 j = (3 + 5) / 2 = 8 / 2 = 4. As the recursive partitioning continues, partitions are continuously divided in half. 5) 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. 6) 35 Elements from indices 5 to 5 are in the right partition. This partition has 1 element, and is already sorted.

1) How many recursive partitioning levels are required for a list of 8 elements? 2)How many recursive partitioning levels are required for a list of 2048 elements? 3) How many elements will the temporary merge list have for merging two partitions with 250 elements each?

1) 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) For N elements, the number of levels is log2 N. log2 8 = 3. 2) 11 log2 2048 = 11 3) 500 The temporary list must be large enough to merge all elements from the two partitions.

1) Insertion sort is a fast sorting algorithm. True False 2) Merge sort is a fast sorting algorithm. True False 3) Radix sort is a fast sorting algorithm. True False

1) False Insertion sort has an runtime of O(N2). A sorting algorithm needs an average runtime of O(NlogN) or better to be considered a fast sorting algorithm. 2) True A sorting algorithm needs a runtime of O(NlogN) or better to be considered a fast sorting algorithm. Merge sort's average runtime is O(NlogN). 3) True A sorting algorithm needs a runtime of O(NlogN) or better to be considered a fast sorting algorithm. Radix sort's average runtime is O(N), which is better than O(NlogN).

1) 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 2) Consider integers X and Y, such that X < Y. X will always be in a lower bucket than Y. True False 3) All integers from an array could be placed into the same bucket, even if the array has no duplicates. True False

1) False Base-10 integers have 10 possible values (0-9) for a digit, meaning 10 buckets are necessary regardless of array size. 2) 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. 3) 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.

A contact list is searched for Bob. Assume the following contact list: Amy, Bob, Chris, Holly, Ray, Sarah, Zoe 1)What is the first contact searched? 2)What is the second contact searched?

1) Holly Binary search first checks the middle contact. Bob is not found and comes before Holly, so the search continues with the first half of the contacts: Amy, Bob, Chris. 2)Bob The middle contact in the first half of the contact list is Bob. So, the desired contact is found.

Determine if each of the following lists is unsorted, sorted, or nearly sorted. Assume ascending order. 1) (6, 14, 85, 102, 102, 151) Unsorted Sorted Nearly sorted 2) (23, 24, 36, 48, 19, 50, 101) Unsorted Sorted Nearly sorted 3) (15, 19, 21, 24, 2, 3, 6, 11) Unsorted Sorted Nearly sorted

1) Sorted All elements are in ascending order. 2)Nearly Sorted Only one element is not in sorted order. 3) Unsorted While the first half and second half are each sorted, the overall list is unsorted since half of the list must still be sorted.

1) ShellSort will properly sort an array using any collection of gap values, provided the collection contains 1. True False 2) Calling ShellSort with gap array (7, 3, 1) vs. (3, 7, 1) produces the same result with no difference in efficiency. True False 3) How many times is InsertionSortInterleaved called if ShellSort is called with gap array (10, 2, 1)? 3 12 13 20

1) True Calling InsertionSortInterleaved with a gap of 1 is equivalent to the standard insertion sort. 2) False ShellSort will produce a sorted array either way, but the order of gap values can affect efficiency. Ex: Sorting the array (88, 22, 33, 44, 55, 66, 77, 11) with gaps (3, 7, 1) requires 9 swaps total. Sorting the same array with gaps (7, 3, 1) requires only 1 swap. 3) 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

1) Selection sort can be used to sort an array of strings. True False 2) The fastest average runtime complexity of a comparison sorting algorithm is O(NlogN). True False

1) 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. 2) True Radix sort has a runtime complexity better than O(NlogN), but is not a comparison sorting algorithm. Quicksort, merge sort, and heap sort are the fastest comparison sorting algorithms, each with an average runtime of O(NlogN).

Determine the midpoint and pivot values. 1) numbers = (1, 2, 3, 4, 5), i = 0, k = 4 midpoint = 2) numbers = (1, 2, 3, 4, 5), i = 0, k = 4 pivot = 3) numbers = (200, 11, 38, 9), i = 0, k = 3 midpoint = 4) numbers = (200, 11, 38, 9), i = 0, k = 3 pivot = 5) numbers = (55, 7, 81, 26, 0, 34, 68, 125), i = 3, k = 7 midpoint = 6)numbers = (55, 7, 81, 26, 0, 34, 68, 125), i = 3, k = 7 pivot =

1)2 midpoint= (i+(k−i))/2 = 0+((4−0)/2) = 4/2 =2 The midpoint determines the middle element of the array. 2) 3 midpoint = 0 + (4 - 0)/2 = 2 The midpoint is 2, so the pivot value is the element at numbers[2]. 3) 1 midpoint= i+((k−i)/2) =0+((3−0)/2) =3/2 =1 If the number of elements in the numbers array is even, the midpoint value is rounded down. 4)11 midpoint = 0 + (3 - 0)/2 = 1 The midpoint is 1, so the pivot value is the element at numbers[1]. 5)5 midpoint=i+((k−i)/2) =3+((7−3)/2) =3+2 =5 The midpoint between index 3 and index 7 is located at 5. The partition function (or method) can be applied to a subset of the elements. 6) 34 midpoint = 3 + (7 - 3)/2 = 5 The midpoint of index 3 and index 7 is 5, so the pivot value is the element at numbers[5].

Comparison sorting

A element comparison sorting algorithm is a sorting algorithm that operates on an array of elements that can be compared to each other. Ex: An array of strings can be sorted with a comparison sorting algorithm, since two strings can be compared to determine if the one string is less than, equal to, or greater than another string. Selection sort, insertion sort, shell sort, quicksort, merge sort, and heap sort are all comparison sorting algorithms. Radix sort, in contrast, is not a comparison sorting algorithm and requires array elements to be numbers.

Bucket

Any array of integer values can be subdivided into buckets by using the integer values' digits. A bucket is 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, and would all be placed into bucket 7 when subdividing by the 1's digit.

Sorting signed integers

In the extension, before radix sort completes, 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. Pseudocode for the completed radix sort algorithm follows.

Merge sort

Merge sort is 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 list of 1 element is already sorted.

Selection sort 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 } Numbers: 7, 9, 3, 18, 8

Output: 3, 7, 8, 9, 18 1. Selection sort treats the input as two parts, a sorted and unsorted part. Variables i and j keep track of the two parts. 2. The selection sort algorithm searches the unsorted part of the array for the smallest element; indexSmallest stores the index of the smallest element found. 3. Elements at i and indexSmallest are swapped. 4. Indices for the sorted and unsorted parts are updated. 5. The unsorted part is searched again, swapping the smallest element with the element at i. 6. The process repeats until all elements are sorted.

Quicksort

Quicksort is a sorting algorithm that repeatedly partitions the input into low and high parts (each part unsorted), and then recursively sorts each of those parts. To partition the input, quicksort chooses a pivot to divide the data into low and high parts. Quicksort partitions data into a low part with data less than/equal to a pivot value and a high part with data greater than/equal to a pivot value.

Radix sort

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

Sorting

Sorting is the process of converting a list of elements into ascending (or descending) order. The challenge of sorting is that a program can't "see" the entire list to know where to move an element. Instead, a program is limited to simpler steps, typically observing or swapping just two elements at a time. So sorting just by swapping values is an important part of sorting algorithms.

Merge sort partitioning

The merge sort algorithm uses three index variables to keep track of the elements to sort for each recursive function call. 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 j + 1 to k are in the right half.

Partitioning algorithm

The partitioning algorithm uses two index variables l and h (low and high), initialized to the left and right sides of the current elements being sorted. As long as the value at index l is less than the pivot value, the algorithm increments l, because the element should remain in the low partition. Likewise, as long as the value at index h is greater than the pivot value, the algorithm decrements h, because the element should remain in the high partition. Then, if l >= h, all elements have been partitioned, and the partitioning algorithm returns h, which is the index of the last element in the low partition. Otherwise, the elements at indices l and h are swapped to move those elements to the correct partitions. The algorithm then increments l, decrements h, and repeats.

Pivot

the pivot can be any value within the array being sorted, commonly the value of the middle array element. Ex: For the list (4, 34, 10, 25, 1), the middle element is located at index 2 (the middle of indices 0..4) and has a value of 10.


Set pelajaran terkait

Chapter 2.2: Painting InQuizitive

View Set

chapter 21 Concept Physics 1/11/16

View Set

Chapter 1 - Organization and teamwork

View Set

Ch. 11 Problems with Fluid and Electrolyte Balance

View Set

Domain 4 - Information Security Incident Management

View Set

Great Depression - True/False Questions

View Set

C11.3 Post-test / Building Sentences with Subordination and Coordination

View Set