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?
Cons of insertion sort?
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?
Cons of selection sort?
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:
Best case: O(n * log n)
Worst case: O(n*log n)
Pros of merge sort?
Cons of merge sort?
- 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:
Best case: O(n*log n)
Worst case: O(n^2)
Pros of quick sort?
Cons of quick sort?
Code for quick sort.
int partition(int arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2]; while (i pivot)
j--;
if (iWhat is heap sort?
Heap sort is a comparison-based sorting algorithm. Heap sort can be thought of as an improved selection sort: like that algorithm, it divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. The improvement consists of the use of a heap data structure rather than a linear-time search to find the maximum.
Best case: Ω(n), O(nlog n)
Worst case: O(nlog n)
What is the heap property?
In a heap, for every node i other than the root, the value of a node is greater than or equal (at most) to the value of its parent.
A[PARENT (i)] ≥ A[i]
Thus, the largest element in a heap is stored at the root.
Pros of heap sort?
Cons of heap sort?
- Quite a lot of code to implement the heap operations
Code for heap sort.
void heapSort(int numbers[], int array_size)
{
int i, temp;
//heapify
for (i = (array_size / 2)-1; i >= 0; i–)
siftDown(numbers, i, array_size);
//sortdown
for (i = array_size-1; i >= 1; i--)
{
temp = numbers[0];
numbers[0] = numbers[i];
numbers[i] = temp;
siftDown(numbers, 0, i-1);
}
}
//sink
void siftDown(int numbers[], int root, int bottom)
{
int done, maxChild, temp;
done = 0;
while ((root*2 numbers[root * 2 + 1])
maxChild = root * 2;
else
maxChild = root * 2 + 1;if (numbers[root]
What is bubble sort?
Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
Best case: O(n)
Worst case: O(n^2)
Pros of bubble sort?
Cons of bubble sort?