Sorting Algorithms Flashcards
what are the different design approaches to sorting algorithms
brute force/exhaustive
decrease and conquer
divide and conquer
transform and conquer
dynamic programming
greedy
examples of brute force sorting algorithms
bubble sort
selection sort
when are the brute force sorting algorithms best
for small cases and simple situations
examples of the decrease and conquer algorithms
insertion sort
what does decrease and conquer involve
breaking down the problem into smaller problems and solving them
what are some examples of transform and conquer sorting algorithms
balanced search trees
what are some examples of divide and conquer algorithms
quick sort and merge sort
what does divide and conquer mean
split problem and solve separately usually recursively
what is dynamic programming
solves problems by combining the solution to subproblems
examples of dynamic programming
Warshall’s and Floyd’s shortest path algorithm
what are greedy algorithms
always make the choice that looks best in the moment
do not always yield the optimal solution but often does
examples of greedy programmin
Dijkstra shortest path
binary search is an example of what type of sorting algorihm
divide and conquer
what does it mean if an algorithm is stable
in the final sorted array, repeats of numbers stay in the same order that they were in the original array
which are examples of stable algorithms
insertion sort, bubble sort, merge sort
what does it mean if an algorithm is in place
no extra space is needed
when do we not care if the algorithm is stable
when equal elements are indistinguishable eg integers
if all the keys are different
when do we care about stability in sorting algorithms
when there are duplicate keys and we want to maintain original order
examples of the in place sorting algorithms
selection sort, insertion sort, shell sort, quick sort
how does insertion sort work
- start from first element of the array
- move it back until you find a smaller element
- if you dont find a smaller element, it gets shifted all the way back to the 0th index
- keep the LHS of the current index sorted and the RHS are the elements you are yet to sort
when is insertion sort useful
when arrays are small and almost sorted
best case input for insertion sort
almost sorted or already sorted
because it won’t require much swapping
what is the worst case input for insertion sort
a reverse order array
as every element will have to be moved the max number of times
what is the time complexity of insertion sort
O(n)
is insertion sort stable
yes
as it only moves one element at a time
is insertion sort in place
yes
it doesn’t use extra space
how does bubble sort work
compare elements adjacent to each other and swap if they are out of order
time complexity of bubble sort
O(n^2)
what is the best case input for bubble sort
array is sorted
what is the worst case input for bubblesort
array is sorted in reverse order
best case performance of bubblesort
n
is bubblesort in place
yes
is bubble sort stable
yes
how does selection sort work
in each iteration it finds the smallest remaining entry and swaps the current element with that one
LHS entries are sorted
RHS are unsorted