Insertion - The insertion sort, like the , builds the list one at a time. But instead of searching for the lowest, then second to lowest items, it simply puts the items in order as the items are encountered.

- Example - Implementation

public static void insertionSort (int[] ary) { for (int i = 1; i < ary.length; i++) insertInt(ary, i); }

private static void insertInt (int[] ary, int i) { int tmp = ary[i]; while (i > 0 && tmp < ary[i-1]) { ary[i] = ary[i-1]; i--; } ary[i] = tmp; }

- • The insertInt is O(N). It is called on segments of the array ranging from length 1 up to N, so that averages out to arrays of length N/2. • The time complexity of insertion sort is O(N2) because it uses a for loop with N iterations, that calls a method of O(N).

Heap Sort - Read elements of the given array to build a , and then remove all elements from the heap to construct a .

- Implementation

/** * : sort an array using heap * Ying Li * 04/17/2018 */ import java.util.Comparator; import java.util.Random; public class HeapSort { // sort array using a heap public void heapSort (Object[] ary, Comparator comp) { MaxHeap mh = new MaxHeap(ary.length, comp);

for (int i = 0; i < ary.length; i++) { mh.add((T)ary[i]); }

//for (int i = ary.length-1; i >= 0; i--) { //get ascending array for (int i = 0; i < ary.length; i++) { //get descending array ary[i] = mh.remove(); } }

- Time complexity • The time complexity of the heap sort depends on the add and remove methods. • The time complexity of one call of add method is O(log n). • The time complexity of one call of remove method is O(log n). • The heap sort algorithm calls add and remove n times for each. So the time complexity is O(n log n).

Merge Sort - is a divided and conquer algorithm. It divides the input array into n sublists, each containing one element. The list of one element is considered sorted. Then, repeatedly merge sublists to produce new sorted sublists until there is only one sublist remaining, which will be the sorted list.

- Example

- Implementation

public static void mergeSort (int[] ary) { int[] tmp = new int[ary.length]; mergeSort(ary, tmp, 0, ary.length-1); }

private static void mergeSort(int[] ary, int[] tmp, int left, int right) { if (left < right) { int center = (left + right)/2; mergeSort(ary, tmp, left, center); mergeSort(ary, tmp, center+1, right); merge(ary, tmp, left, center+1, right); } }

private static void merge (int[] ary, int[] tmp, int left, int right, int rightEnd) { int leftEnd = right - 1; int i = left; int num = rightEnd - left + 1;

while (left <= leftEnd && right <= rightEnd) { if (ary[left] <= ary[right]) tmp[i++] = ary[left++]; else tmp[i++] = ary[right++]; }

while (left <= leftEnd) // copy rest of first half tmp[i++] = ary[left++];

while (right <= rightEnd) // copy rest of right half tmp[i++] = ary[right++];

// copy tmp back to ary for (int j = 0; j < num; j++, rightEnd--) ary[rightEnd] = tmp[rightEnd]; }

- Time complexity • The time complexity of the merge sort is O(n log n) • The original array of size N is eventually split into N subarrays of size 1. Merging two of those subarrays into a subarray of size 2 requires 1 + 1 = 2 steps. • We must perform this merge operation 1/2N times. So the total number of steps to create all of the sorted two-element subarray is O(N) since (2 * 1/2N = N). • It takes O(N) steps to perform merging at each “level” of the merging, and there are log n levels.

REF: https://www.cs.cmu.edu/~adamchik/15-121/lectures/Sorting%20Algorithms/.html Disclaimer: Notes adapted from previous CS 231 lecture materials at Colby College.