Ch. 12 Bubble Sort
Given an unsorted array {28, 32, 4, 15, 12, 45, 25, 33, 22, 56, 47, 11, 6}, what is the correct subarray after 3 recursive calls to merge sort? {28, 32, 4, 15} {28, 32} {12, 45, 25} {4, 12, 15}
Explanation After 3 recursive calls to merge sort, the subarray obtained will be {28, 32}.
For a given array of [32, 44, 12, 15], the number of iterations performed using selection sort algorithm are: 4 3 5 2
Explanation The number of iterations performed in case of selection sort is (n - 1) where n is the length of the array. Since this is 4 element array, the number of iterations will be 3.
How many times will the outer for loop be executed? int mySimpleArray[[] ={1,4,7,5,19,3,11,4,22,8,2,21}; int temp = 0; int n = mySimpleArray.length; for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (mySimpleArray[j] > mySimpleArray[j+1]) { int temp = mySimpleArray[j]; mySimpleArray[j] = mySimpleArray[j+1]; mySimpleArray[j+1] = temp; } } } 10 2 11 1
11
What is the position where key 75 would be inserted into the table below using the quadratic probing method?
4 We compute the base address as: H(75) = 75%10 = 5 Since position 5 is occupied, we use the quadratic function H(k) = (probe number 2+base address)%10 We would use probe number as 1 in this equation since this will be the first computation for inserting 75. H(75) = (1 2+ 5)%10 = (1 + 5)%10 = 6%10 = 6 Since position 6 is already occupied, we recompute using probe number as 2: H(75) = (2 2+ 5)%10 = (4 + 5)%10 = 9%10 = 9 Since position 9 is occupied, we recompute using the next probe number as 3: H(75) = (3 2+ 5)%10 = (9 + 5)%10 = 14%10 = 4 We would store 75 and position 4.
0 1 2 3 4 5 6 7 8 9 - - - - - 5 25 - - 35 What is the position where key 75 would be inserted into the table below using the quadratic probing method?
4 Explanation We compute the base address as: H(75) = 75%10 = 5 Since position 5 is occupied, we use the quadratic function H(k) = (probe number 2+base address)%10 We would use probe number as 1 in this equation since this will be the first computation for inserting 75. H(75) = (1 2+ 5)%10 = (1 + 5)%10 = 6%10 = 6 Since position 6 is already occupied, we recompute using probe number as 2: H(75) = (2 2+ 5)%10 = (4 + 5)%10 = 9%10 = 9 Since position 9 is occupied, we recompute using the next probe number as 3: H(75) = (3 2+ 5)%10 = (9 + 5)%10 = 14%10 = 4 We would store 75 and position 4.
Given a second hash function of I(k) = (1 + k%7) , in which position in the table below would the key 53 be stored using the double hashing technique?
8 We calculate the base address H(53) = 53%10 = 3 Since position 3 is occupied, we need to resolve the collision by computing the alternative location using double hashing with second hash equation as: I(k) = (1 + k%7) The full equation to use when a collision occurs the first time: (1*I(k) + base address)%(table length) Position for 53 = (1*I(53) + 3)%10 = ((1+53%7) + 3)%10 = ((1+4) +3)%10 = 8%10 = 8
____ _____ uses a second hash function based on the key, to determine the increment of the current position. All of these methods have advantages in resolving collisions, but each also has disadvantages.
Double Hashing
Which probing formula is this? Alternate location = (probe number*increment based on key + base address) mod (array length) second hash function I(k) = (1+k mod 7)
Double Hashing With double hashing, the location is computed by using a second hash function based on the key to increment the current location rather than a constant as used in linear probing. This method reduces the number of times the same location is recomputed by using the following equation Position for 22 = (1*I(22) + 2)%10 = (1*(1+22%7) + 2)%10 = (1*(1 + 1) + 2)%10 = 4
0 1 2 3 4 5 6 7 8 9 - 11 31 - - - - - - - Using linear probing, which table correctly shows how the sequence of keys 51, 67, and 86 be stored in the table?
Explanation To insert 51, we first compute the base address as: H(51) = 51%10 = 1 Since position 1 is occupied, we need to compute the alternation location as (1+current position)%10 In this case we compute (1 + 1)%10 = 2%10 = 2. Position 2 is also occupied, so we compute the next position using (1+2)%10 = 3. Since position 3 is empty, we store 51 in that position. The keys 67 and 86 do not cause collisions when we compute the base addresses, so we can store the keys at the base address. To insert key 67 we compute the base address as 67%10 = 7 Similarly, the base address for 86 is computed as 86%10 = 6
The _____ probing method is the simplest method of finding another table position by the increment of the current position by 1, but this method inefficiently recomputes the same alternate position leading to a clustering problem
linear probing
The ____ ______ algorithm works on the divide and conquer paradigm where an unsorted array is recursively divided into subarrays. Each subarray is sorted and merged till we get a single array
merge sort
_____ probing uses the probing sequence to determine the next alternate position but is only capable of accessing half of empty positions available
Quadratic probing
{1,4,7,5,19,3,11,4,22,8,2,21}; What would the array look like after the first pass of the inner for loop of the Bubble Sort algorithm? [7,6,4,2,1] [1,2,6,7,4] [7,4,1,2,6] [4,6,1,2,7]
[4,6,1,2,7]
What is the requirement for an internal sort?
Internal sorting is where the memory provided is large enough to hold the entire data set.
Which probing formula method is this? alternative location = (1 + current location) mod (table length)
Linear Probing if the current location is taking we add one and repeat the method.
rom the performance point of view, merge sort is very predictable and reliable and has the complexity of _______
O(n log n) as per Big-O notation.
The Quick Sort is one of the most efficient sorting algorithms. It has a Big O rating of _____ for average performance and an ____ worst case performance
O(n log n), O(n^2)
Which probing formula is this? Alternate location = ((probe number)^2+ base address) mod (array length)
Quadratic Using this method we compute locations for the sequence 12, 22, and 52 in the table as follows: H(12) = 12%10 = 2 H(22) = (1^2+2)%10 = 3 H(52) = (2^2+2)%10 = 6 The problem with this method is there will be only half of the empty table locations that will be traversed in this way in some scenarios and cause infinite looping in search of an empty location.
Consider a situation where the minimum number of swap operation is required. Which sorting algorithm is best for this use-case? Merge Sort Selection Sort Heap Sort Insertion Sort
Selection Sort Explanation Selection sort requires fewer swap operations as compared to other sorting algorithms.
Which sort is this? Unsorted array: 95, 42, 13, 9, 23 95, 42, 13, 9, 23 9, 42, 13, 95, 23 9, 13, 42, 95, 23 9, 13, 23, 95, 42 9, 13, 23, 42, 95
Selection sort
Using the Big-O Notation, the best and worse case scenario for the Selection Sort algorithm is represented by _____ Best case:O(log n) Worst Case:O(1) Best case:O(n^2) Worst Case:O (n log n) Best case: O(n^2) Worst Case: O(n^2) Best case:O (n log n) Worst Case: O(n^2)
The Selection Sort best and worst case scenarios both follow the time complexity format: O(n^2) as the sorting operation involve two nested loops.
What is the advantage of selection sort? It is highly scalable. it requires no additional memory for operation. It is faster and requires fewer iterations. It required additional memory for operation.
The advantage of selection sort is that it requires no additional memory for operation.
In which of the following sort algorithms does the arrangement of elements not affect its performance? Manage Sort algorithm Selection Sort algorithm Insertion Sort algorithm Bubble Sort algorithm
The arrangement of elements does not affect its performance of the Selection Sort algorithm
What is the best case complexity of merge sort? θ(n log n) Ω(n log n) O(n) O(n2)
The best case complexity of merge sort is Ω(n log n).
n the nested loop for the insertion sort, why do we decrement? The sort orders from end to beginning To get to the last element in the array The sort needs to go past the last element This is an error; sorting should increment
The sort orders from end to beginning
What type of sort is Quick Sort?
There are several different types of sorting algorithms: insertion, selection, exchange, and merge. The Quick Sort is a type of exchange sort.
Using linear probing, which table correctly shows how the sequence of keys 51, 67, and 86 be stored in the table below?
To insert 51, we first compute the base address as: H(51) = 51%10 = 1 Since position 1 is occupied, we need to compute the alternation location as (1+current position)%10 In this case we compute (1 + 1)%10 = 2%10 = 2. Position 2 is also occupied, so we compute the next position using (1+2)%10 = 3. Since position 3 is empty, we store 51 in that position. The keys 67 and 86 do not cause collisions when we compute the base addresses, so we can store the keys at the base address. To insert key 67 we compute the base address as 67%10 = 7 Similarly, the base address for 86 is computed as 86%10 = 6
Which of the following location states will be treated the same when doing an INSERT operation into the hash table in open addressing? Empty and Deleted None of the states will be treated the same in open addressing Occupied and Deleted Occupied and Empty
When doing an INSERT operation, both Empty and Deleted spaces in the table will have values inserted into them. Deleted spaces will be treated differently from Empty spaces when doing data retrieval only.