made by Joanne Tseng 2014/08/24
Performance : How to make things fast.
//pseudo code of Insertion Sort
for j=2 to n
{ key <- A[j]
i <- j-1
while (i > 0 and A[i] > key)
{
A[i+1] <- A[i]
i <- i-1
}
A[i+1] <- key
}
j=2 : 8, 2, 4, 9, 3, 6 --> 2, 8, 4, 9, 3, 6
j=3 : 2, 8, 4, 9, 3, 6 --> 2, 4, 8, 9, 3, 6
j=4 : 2, 4, 8, 9, 3, 6 --> 2, 4, 8, 9, 3, 6
j=5 : 2, 4, 8, 9, 3, 6 --> 2, 3, 4, 8, 9, 6
j=6 : 2, 3, 4, 8, 9, 6 --> 2, 3, 4, 6, 8, 9
// pseudo code of Merge
Merge(A, p, q, r)
n1 = q - p + 1
n2 = r - q
Let L[1,...,n1+2] and R[1,...,n2+1] be new arrays
for i=1 to n1
L[i] = A[p + i - 1]
for j=1 to n2
R[j] = A[q + j]
L[n1+1] = inf
R[n2+1] = inf
i = 1
j = 1
for k=p to r
if (L[i] <= R[j])
A[k] = L[i]
i = i+1
else
A[k] = R[j]
j = j+1
// pseudo code of Merge Sort
MergeSort(A,p,r)
if (p<r)
{
q = (p+r)/2
MergeSort(A , p , q)
MergeSort(A , q+1 , r)
Merge(A , p , q , r)
}