What is a sorting algorithm?
An algorithm that puts elements of a list into an order, such as numerical or lexicographical order.
What are the two formal conditions the output of any sorting algorithm must satisfy?
The output must be in monotonic order (each element is no smaller/larger than the previous) and must be a permutation (reordering) of the input.
What is the typical best-case time complexity for comparison-based sorting algorithms?
O(n log n) for good serial sorting algorithms.
What is the typical worst-case time complexity for simple sorts like bubble and insertion sort?
O(n2).
Define a stable sorting algorithm.
A stable sorting algorithm maintains the relative order of records with equal keys (values).
Why is stability important in sorting?
It preserves the order of equal elements over multiple sorts, so sorting by one key doesn't disrupt the order of another key.
What is an in-place sorting algorithm?
An algorithm that requires only O(1) additional memory beyond the items being sorted (sometimes O(log n) is considered in-place).
What is a comparison sort?
A sort that examines data only by comparing two elements with a comparison operator.
Describe the basic idea of bubble sort.
Repeatedly step through the list, compare adjacent elements, and swap them if they are in the wrong order. The largest elements 'bubble' to the end.
In bubble sort, after the first full pass, where is the largest element guaranteed to be?
At the last position of the list.
How many passes does bubble sort need in the worst case for a list of n elements?
n-1 passes.
What is the main advantage of bubble sort?
It is simple to understand and implement, and it is stable.
Describe the basic idea of insertion sort.
Build the sorted list one element at a time by taking each new element and inserting it into its correct position among the already sorted elements.
In insertion sort, how does the algorithm find the correct position for an element?
It compares the element with those before it, shifting larger elements to the right until the correct spot is found.
What is the best-case time complexity of insertion sort and when does it occur?
O(n), when the list is already sorted.
Is insertion sort stable?
Yes, it is stable because equal elements retain their relative order.
Describe the basic idea of merge sort.
It is a divide-and-conquer algorithm that recursively splits the list into halves, sorts each half, and then merges the sorted halves back together.
What is the time complexity of merge sort in all cases (best, average, worst)?
O(n log n).
What is the main disadvantage of merge sort?
It requires additional memory for the temporary arrays used during merging, so it is not in-place.
Is merge sort stable?
Yes, merge sort is stable if implemented correctly.
Compare the number of comparisons in bubble sort vs. insertion sort in the worst case.
Both have O(n2) comparisons in the worst case, but insertion sort typically makes fewer comparisons in practice.
Which of the three sorts (bubble, insertion, merge) is generally the fastest for large lists?
Merge sort, because it has O(n log n) time complexity, while bubble and insertion are O(n2).
Which of the three sorts is an example of a divide-and-conquer algorithm?
Merge sort.