CSC 331 Quiz 2
What algorithm should I use to quickly sort 2 million 10-digit numbers?
Radix sort
For finding the median of an array of numbers, what is the name of the algorithm with the best expected runtime?
Selection Best case: find the right pivot first time - O(n) Worst case: take lowest or highest element as pivot every time - O(n^2) Average case: reduce by half each time - O(n)
If you were sorting 100,000 license plates (each with 6 characters that can be either letters or numbers) what would you choose among quicksort, counting sort, and radix sort?
radix sort
What is an algorithm and not a problem?
selection
When does O(log n) occur for time complexity in an algorithm?
when a set of data is repeatedly divided into half and the middle element is processed(binary search)
Rank Big O complexity in order of fastest to slowest
0(log n ), 0(1), 0(n), 0(n log n), 0(n^2), 0(2^n), 0(n!)
what is the time complexity for T(n) = T(n/2) + C
0(log n)
The fastest worst-case time complexity for a comparison-based sorting algorithm is ?
0(n log n)
Fastest algorithm so far?
0(n log n) merge sort is the faster algorithm
The average time complexity to find the median of a list of numbers using the selection algorithm is
0(n)
Using the selection method, what is the average time complexity for finding the median in a list of numbers?
0(n)
For T(n) = T(n-1) + C what is the base case for the recurrence tree?
1 node
T or F: if the function grows fast algorithm is slow
True
What does it mean for a sort to be stable
Two equal items maintain their relative positions
Quicksort depends on the partition algorithm. After running partition one time on an array, what kind of structure does the array have?
All the elements to the left of the pivot are smaller, and to the right, bigger. The partition element is located at its sorted location in the array.
What is the time complexity of Quick Sort?
Best Theta(n log n) - Worst O(n^2) - Average(O(n log n)
Is every algorithm theta(1)?
Yes
What is faster Brute or Divide and Conquer?
either Brute or Divide and Conquer could be faster depending on the time and algorithm
When do we care about how fast an algorithm is?
for any large input size
For sorting multi digit numbers with radix sort, the digits should be sorted
from right to left
I need to sort very large files by name. I have exactly 10,000 of them. They are almost sorted already.
Insertion Sort. O(n2) worst case but O(n) for almost-ordered.
Which sorting algorithm tends to be the fastest for small numbers of items?
Insertion sort
What part of quick sort would most benefit from being random?
selecting the pivot element
What causes quick sort to have the worst run time?
the array is already sorted
The problem with sorted data for quick sort is ?
there are more recursive calls
The average time for searching a sorted array is?
O (log n)
For T(N) = 2T(n/2)+c what is the time complexity?
O( n log n)
When does O( n log n) occur for time complexity in an algorithm?
when a set of data is repeatedly divided into half and each half is processed again independently
What is the worst case time complexity for quicksort?
n^2
I need to sort a lot of numbers as quickly as possible
Quicksort O(n log n
How do you use selection problem using divide and conquer?
1. find the kth smallest element in a array (function selection(A, left, right, k) 2.Divide( partition the array into smaller,larger and equal categories) 3.Conquer( if the pivot index is k, return the element. Recursively find the kth element in the smaller or larger sub array) Base case: if there is only one element in the array return it.
Selection algorithm
1. find the smallest kth element in a array of size n 2. median is just k=n/2 Average time of 0(n)
Which of the following algorithms has the slowest best case runtime
Merge sort
Can there be a faster comparison based sorting algorithm than 0(n log n)
no, its impossible
What is the time complexity of Selection Sort?
Best Theta 0(n) - Worst O(n^2) - Average(O(n))
What is the time complexity of Counting Sort?
Best Theta(n + k) - Worst O(n + k) - Average(O(n + k)
What is the time complexity of Merge Sort?
Best Theta(n log n ) - Worst O(n log n) - Average(O(n log n)
What is the time complexity of Bubble Sort(slower then insertion sort even when already sorted)?
Best Theta(n) - Worst O(n^2) - Average(O(n^2)
What is the time complexity of Insertion Sort?
Best Theta(n) - Worst O(n^2) - Average(O(n^2)
What is the time complexity of Radix Sort?
Best Theta(nk) - Worst O(nk) - Average(O(nk)
What are 3 ways for to solve the selection problem for the smallest term?
Brute force: use linear search to find the smallest element k times Better: sort the array and find the kth element in the array Best: use partitioning like quick sort
I need to sort a large set of numbers that are between 0 and 100
Counting Sort. O(n)
For what kind of data would counting sort be a bad choice?
a big range of possible values