Sorting Algorithms Flashcards
What is insertion sort?
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. Array is imaginary divided into two parts - sorted one and unsorted one. At the beginning, sorted part contains first element of the array and unsorted one contains the rest. At every step, algorithm takes first element in the unsorted part and inserts it to the right place of the sorted one. When unsorted part becomes empty, algorithm stops.
Best case: O(n) (list is already sorted)
Worst case: O(n^2)
Pros of insertion sort?
- simple implementation
- efficient for small data sets
- more efficient than bubble sort or selection sort
- adaptive (i.e. effective for sets that are already substantially sorted.)
- stable (i.e. doesn’t change the relative order of elements with equal keys)
- does sort in place
- very low overhead
Cons of insertion sort?
- inefficient on large data set or if the data set is reverse order
What is selection sort?
Selection sort is a sorting algorithm, specifically an in-place comparison sort. The idea of selection sort is rather simple: repeatedly find the next largest (or smallest) element in the array and move it to its final position in the sorted array.
Assume that the array should be sorted in increasing order, i.e. the smallest element at the beginning of the array and the largest element at the end. Begin by selecting the largest element and moving it to the highest index position. Do this by swapping the element at the highest index and the largest element. Then reduce the effective size of the array by one element and repeat the process on the smaller (sub)array. The process stops when the effective size of the array becomes 1 (an array of 1 element is already sorted).
Best case: O(n^2)
Worst case: O(n^2)
Pros of selection sort?
- has the property of minimizing the number of swaps. In applications where the cost of swapping items is high, selection sort very well may be the algorithm of choice.
Cons of selection sort?
- inefficient on large data set
- not adaptive
- not stable
Code for selection sort
void selectionSort(int numbers[], int array_size) { int i, j; int min, temp;
for (i = 0; i
Code for insertion sort
void insertionSort(int numbers[], int array_size) { int i, j, index;
for (i = 1; i 0) && (numbers[j − 1] > index)) { numbers[j] = numbers[j − 1]; j = j − 1; } numbers[j] = index; } }
What is merge sort?
Merge sort (also commonly spelled merge sort) is an O(n log n) comparison-based sorting algorithm. Steps for the merge sort algorithm are:
- Divide Step: If a given array A has zero or one element, simply return; it is already sorted. Otherwise, split A[p .. r] into two subarrays A[p .. q] and A[q + 1 .. r], each containing about half of the elements of A[p .. r]. That is, q is the halfway point of A[p .. r].
- Conquer Step: Conquer by recursively sorting the two subarrays A[p .. q] and A[q + 1 .. r].
- Combine Step: Combine the elements back in A[p .. r] by merging the two sorted subarrays A[p .. q] and A[q + 1 .. r] into a sorted sequence. To accomplish this step, we will define a procedure MERGE (A, p, q, r).
Best case: O(n * log n)
Worst case: O(n*log n)
Pros of merge sort?
- very predictable
- stable
- it can be applied to files of any size.
- reading of the input during the run-creation step is sequential ==> Not much seeking.
- reading through each run during merging and writing the sorted record is also sequential. The only seeking necessary is as we switch from run to run.
- if heapsort is used for the in-memory part of the merge, its operation can be overlapped with I/O
- Since I/O is largely sequential, tapes can be used.
Cons of merge sort?
- not as fast as quick sort (on average)
- requires as much memory as the original array
Code for merge sort
void m_sort(int numbers[], int temp[], int left, int right) {
int mid;
if (right > left) {
mid = (right + left) / 2;
m_sort(numbers, temp, left, mid);
m_sort(numbers, temp, mid+1, right);
merge(numbers, temp, left, mid+1, right);
}
}
void merge(int numbers[], int temp[], int left, int mid, int right) {
int i, left_end, num_elements, tmp_pos; left_end = mid - 1; tmp_pos = left; num_elements = right - left + 1; while ((left
What is quick sort?
Quick sort is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. Quick sort is a divide and conquer algorithm. Quick sort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quick sort can then recursively sort the sub-arrays.
The steps are:
- Pick an element, called a pivot, from the array.
- Reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
- Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
Best case: O(n*log n)
Worst case: O(n^2)
Pros of quick sort?
- It is in-place since it uses only a small auxiliary stack.
- It requires only n log(n) time to sort n items.
- It has an extremely short inner loop
- This algorithm has been subjected to a thorough mathematical analysis, a very precise statement can be made about performance issues.
Cons of quick sort?
- It is recursive. Especially if recursion is not available, the implementation is extremely complicated.
- It requires quadratic (i.e., n^2) time in the worst-case.
- It is fragile i.e., a simple mistake in the implementation can go unnoticed and cause it to perform badly.