Sorting Algorithms
- 22Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What is the worst-case time complexity for searching an element in an unsorted list using linear search?
The worst-case time complexity for linear search on an unsorted list is θ(n).
Or swipe the card
22 cards
What is the worst-case time complexity for searching an element in an unsorted list using linear search?
The worst-case time complexity for linear search on an unsorted list is θ(n).
What is the worst-case time complexity for searching an element in a sorted list using linear search?
The worst-case time complexity for linear search on a sorted list is θ(n).
What is the primary caveat for using the binary search algorithm?
The list must be sorted to use the binary search algorithm correctly.
What is the worst-case time complexity of binary search on a sorted list?
The worst-case time complexity of binary search on a sorted list is θ(logn).
Explain when it makes sense to sort a list to use binary search, rather than just using linear search multiple times.
It makes sense to sort a list once and then perform multiple binary searches (k searches) if the cost of one sort plus k binary searches is less than k linear searches. This typically occurs when k is large, allowing the one-time sorting cost to be amortized.
What is bogo sort (random sort/monkey sort)?
Bogo sort is a highly inefficient sorting algorithm that repeatedly shuffles a list randomly until it happens to be sorted by chance.
What is the best-case time complexity of bogo sort?
The best-case time complexity of bogo sort is θ(n).
What is the worst-case time complexity of bogo sort?
The worst-case time complexity of bogo sort is unbounded or θ(∞).
Describe the core idea behind the bubble sort algorithm.
Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process effectively 'bubbles up' the largest elements to their correct positions at the end of the list with each pass.
Explain how bubble sort 'bubbles up' elements during a pass.
During each pass of bubble sort, adjacent elements are compared and swapped if out of order. This causes larger elements to gradually move towards the end of the list, much like bubbles rising to the surface, until the largest element reaches its final position.
What is the worst-case time complexity of bubble sort?
The worst-case time complexity of bubble sort is θ(n2).
Describe the core idea behind the selection sort algorithm.
Selection sort works by repeatedly finding the minimum element from the unsorted part of the list and putting it at the beginning of the unsorted part. With each pass, it places one element into its correct sorted position.
How does selection sort determine which element belongs at index 0 in the first pass?
In the first pass of selection sort, the element at index 0 is compared with every other element in the list. If a smaller element is found, it is swapped with the element at index 0. This process continues until the smallest element in the entire list is placed at index 0.
What is the worst-case time complexity of selection sort?
The worst-case time complexity of selection sort is θ(n2).
How does a variation of selection sort improve efficiency by deferring swaps?
A variation of selection sort can improve efficiency by keeping track of the smallest element's index during the inner loop and only performing one swap at the end of each outer loop iteration, rather than swapping every time a smaller element is found.
What kind of algorithmic approach does merge sort use that is different from iterative sorts like bubble or selection sort?
Merge sort uses a divide and conquer (recursive) approach, inspired by binary search, rather than an iterative approach of comparing and swapping adjacent elements.
What are the two main steps of the merge sort algorithm?
The two main steps of the merge sort algorithm are dividing the list recursively into halves until single-element lists are formed, and then merging these sorted sublists back together.
Explain the 'smart merge' step in merge sort when combining two already sorted sublists.
To merge two sorted sublists, you only need to compare the first element of each list. The smaller element is taken and added to the result list, and its respective pointer advances. This process continues until one list is exhausted, after which all remaining elements from the other list are simply appended.
What is the time complexity of the merge step (combining two sorted lists) in merge sort?
The time complexity of the merge step is θ(n), where n is the total number of elements in the two lists being merged.
How many levels of division are there in merge sort for a list of n elements?
There are logn levels of division in merge sort for a list of n elements.
What is the overall worst-case time complexity of merge sort?
The overall worst-case time complexity of merge sort is θ(nlogn).
Why is θ(nlogn) considered the fastest possible complexity for a comparison-based sorting algorithm?
θ(nlogn) is the fastest possible complexity for comparison-based sorting because any such algorithm must perform at least Ω(nlogn) comparisons in the worst case to determine the correct order of elements.

Ask anything
We've read this whole study set, so questions about it get the best answers.