Exam Flashcards
Analysis of Selection Sort
Best case:
Array in sorted order
Comparisons: O(n^2)
Time: O(n^2)
Average case:
Comparisons: O(n^2)
Time: O(n^2)
Worst case:
Array in reverse order
Comparisons: O(n^2)
Time: O(n^2)
Analysis of Merge Sort
Best case:
Comparisons: O(n log n)
Time: O(n log n)
Average case:
Comparisons: O(n log n)
Time: O(n log n)
Worst case:
Comparisons: O(n log n)
Time: O(n log n)
Analysis of Insertion Sort
Best case:
Array in sorted order
Comparisons: O(n)
Time: O(n)
Average case:
Comparisons: O(n^2)
Time: O(n^2)
Worst case:
Array in reverse order
Comparisons: O(n^2)
Time: O(n^2)
Analysis of Quick Sort
Best case:
Comparisons: O(n log n)
Time: O(n log n)
Average case:
Comparisons: O(n log n)
Time: O(n log n)
Worst case:
When the list is sorted and left most element is chosen as the pivot
Comparisons: O(n^2)
Time: O(n^2)
Analysis of Shell Sort
Best case:
Comparisons: O(n log n)
Time: O(n log n)
Average case:
Comparisons: O(n^4/3)
Time: O(n(log(n))^2)
Worst case:
Comparisons: O(n^3/2)
Time: O(n(log(n))^2)
Given a list of size n, for which gap value is shell sort the same as insertion sort?
1
Analysis of Sequential Search
Best case:
O(1)
Average case:
O(n)
Worst case:
O(n)
Comparisons:
n
n+1/ 2
Analysis of Binary Search
Best case:
O(log n)
Average case:
O(log n)
Worst case:
O(log n)
Comparisons:
log n
2 log n (average)
n (best)
Pros and Cons of Selection Sort
Pros:
Simple implementation
Cons:
Inefficient on large lists
Pros and Cons of Merge Sort
Pros:
Quicker for larger lists
Unlike insertion sort, it doesn’t go through the list multiple times
Cons:
Comparatively slower than other algorithms for smaller data sets
Pros and Cons of Insertion Sort
Pros:
Simple implementation
Extremely efficient on small lists
More efficient than bubble and selection sort
Cons:
Much less efficient on large lists
Pros and Cons of Quick Sort
Pros:
An in-place sorting algorithm does not use additional memory space while sorting.
Cons:
Not very stable
Binary Search Tree Analysis
Average:
O(log n)
Worst:
O(n)
Hash Table Analysis
Average:
O(1)
Worst:
O(n)
Calculate the number of comparisons insertion sort
Best Case:
n - 1
Worst Case:
n(n-1)/2