Sorting Algorithms

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

The given array is arr = {1,2,4,3}. Bubble sort is used to sort the array elements. How many iterations will be done to sort the array with improvised version?

2 (only 2 elements in the given array are not sorted, hence only 2 iterations are required to sort them).

What is the best case efficiency of bubble sort in the improvised version?

O(n) (since some iterations can be skipped if the list is sorted)

What is the average case complexity of bubble sort?

O(n^2)

What is the worst case complexity of bubble sort?

O(n^2)

What is the average case complexity of selection sort?

O(n^2) (in the average case, even if the input is partially sorted, selection sort behaves as if the entire array is not sorted).

What is the base case complexity of selection sort?

O(n^2) (same as the worse and average)

What is the worst case complexity of selection sort?

O(n^2) (selection sort creates a sub-list. LHS of the 'min' element is already sorted and RHS is yet to be sorted. Starting with the first element, the 'min' element moves towards the final element.)

What is the worse case complexity of QuickSort?

O(n^2) (when the input array is already sorted)

What is the best case complexity of Quicksort?

O(nlogn) (the array is partitioned into equal halves, using the Divide and Conquer master theorem)

What is the average case complexity of QuickSort?

O(nlogn) (the position of partition (split) is unknown, hence all(n) possibilities are considered, the average is found by adding all and dividing by n)

The given array is arr = {2,3,4,1,6}. What are the pivots that are returned as a result of subsequent partitioning? a) 1 and 3 b) 3 and 1 c) 2 and 6 d) 6 and 2

a (1 and 3)

What is an in-place sorting algorithm? a) it needs O(1) or O(logn) memory to create auxiliary locations b) the input is already sorted and in-place c) it requires additional storage d) none of the above

a (auxiliary memory is required for storing the data temporarily)

The given array is arr {1,2,4,3}. Bubble sort is used to sort the array elements. How many iterations will be done to sort the array?

a (even though the first two elements are sorted, bubble sort must go through all the elements in an array)

What is an external sorting algorithm? a) algorithm that uses tape or disk during the sort b) algorithm that uses main memory during the sort c) algorithm that involved swapping d) algorithm that are considered 'in place'

a (external sorting algorithm uses external memory like tape or disk)

What is the advantage of selection sort over other sorting techniques? a) it requires n additional storage space b) it is scalable c) it works best for inputs which are already sorted d) it is faster than any other sorting technique

a (since selection sort is an in-place sorting algorithm, it does not require additional storage)

The given array is arr = {3,4,5,2,1}. The number of iterations in bubble sort and selection sort respectively are, a) 5 and 4 b) 4 and 5 c) 2 and 4 d) 2 and 5

a (since the input array is not sorted, bubble sort takes 5 iterations and selection sort takes 4 (n-1) iterations.

What is an internal sorting algorithm? a) algorithm that uses tape or disk during the sort b) algorithm that uses main memory during the sort c) algorithm that involved swapping d) algorithm that are considered 'in place'

b ( internal sorting algorithm uses internal main memory)

What is the disadvantage of selection sort? a) it requires auxiliary memory b) it is not scalable c) it can be used for small keys d) none of the above

b (as the input size increases, the performance of selection sort decreases)

QuickSort can be categorized into which of the following? a. Brute Force technique b. Divide and conquer c. Greedy algorithm d. Dynamic programming

b (divide and conquer)

Which of the following is not true about quicksort? a) in-place algorithm b) pivot position can be changed c) adaptive sorting algorithm d) can be implemented as a stable sort

b (pivot position can be changed) (once a pivot is chosen, its position is finalized in the sorted array, it cannot be modified).

The given array is arr = {1,2,3,4,5}. (Bubble sort is implemented with a flag variables - the improved version). The number of iterations in selection sort and bubble sort respectively are, a) 5 and 4 b) 1 and 4 c) 0 and 4 d) 4 and 1

b (selection sort is insensitive to input, hence 4 (n-1) iterations. Whereas bubble sort iterates only once to set the flag to 0 as the input is already sorted.

How can you improve the best case efficiency in bubble sort?

boolean swapped = true; for(int j = arr.length-1; j >= 0 && swapped; j--){ swapped = false; for(int k = 0; k < j; k++){ if(arr[k] > arr[k+1]){ int temp = arr[k]; arr[k] = arr[k+1]; arr[k+1] = temp; swapped = true; }}}

What is a randomized QuickSort? a) the leftmost element is chosen as the pivot. b) the rightmost element is chosen as the pivot. c) any element in the array is chosen as the pivot. d) a random number is generated which is used as the pivot.

c (any element in the array is chosen as the pivot) (quicksort is randomized by placing the input data in the randomized fashion in the array or by choosing a random element in the array as a pivot)

What is the advantage of bubble sort over other sorting techniques? a) it is faster b) consumes less memory c) detects whether the input is already sorted d) all of the above

c (detects whether the input is already sorted)

In the following scenarios, when will you use selection sort? a) the input is already sorted b) a large file has to be sorted c) large values need to be sorted with small keys d) small values need to be sorted with large keys

c (selection is based on keys, hence a file with large values and small keys can be efficiently sorted with selection sort)

The given array is arr = {2,6,1}. What are the pivots that are returned as a result of subsequent partitioning? a) 1 and 6 b) 6 and 1 c) 2 and 6 d) none of the above

d (there is only one pivot with which the array will be sorted, the pivot is 1).

What is the code that performs bubble sort?

for(int j = arr.length-1; j>=0; j--){ for(int k = 0; k < j; k++){ if(arr[k] > arr[k+1]{ int temp = arr[k]; arr[k] = arr[k+1]; arr[k+1] = temp; }}}

What is the code that performs selection sort?

int min; for(int j = 0; j < arr.length-1; j++){ min = j; for(int k = j+1; k <=arr.length-1; k++){ if(arr[k] < arr[min]) min=k; } int temp = arr[min]; arr[min] = arr[j]; arr[j] = temp; }

What is the partition operation in Quicksort? (code)

private static int partition(int[] arr, int low, int high){ int left, right, pivot_item = arr[low]; left = low; right = high; while(left <= right){ while(arr[left] <= pivot_item) left++; while(arr[right] > pivot_item) right--; if(left < right) swap(arr, left, right) } arr[low] = arr[right] arr[right] = pivot_item; return right;

What is the appropriate recursive call for QuickSort (arr is the array, low is the starting index and high is the ending index of the array, partition returns the pivot element)

public static void quickSort(int[] arr, int low, int high){ int pivot; if(high>low){ pivot = partition(arr, low, high); quickSort(arr, low, pivot-1); quickSort(arr, pivot+1, high);


Conjuntos de estudio relacionados

MD 1001-1200 ចម្លើយពេញ

View Set

Med-Surg: Healthcare of the older adult

View Set

TOPIC 3: FORCASTING AND PLANNING ( End of Topic Review)

View Set

(Copied) True/False Questions Chapter 5 Biology

View Set

Supply Chain Management -- Exam 2

View Set

econ 345 - ch. 6 (the risk and term structure of interest rates) m/c's

View Set