These are part of my notes for the Coursera course: Algorithms, Pt I (Princeton).
##Basic Mergesort
####Concept
-
halve array, creating 2 sub-arrays
-
sort each sub array
-
merge the sub arrays back together
-
this algorithm is recursive
- to
sort
: half arrays, andsort
sub-arrays - each sort halves the arrays until you are left with only 2 elements
- these elements are then merged together into the first sorted sub array
- from here, the algorithm steps back up through the recursion, merging arrays
- to
####Implementation
Two main functions need to be written: merge and sort.
Merge combines two sorted sub-arrays into a single, sorted array
- create auxiliary array, in which you copy the elements to be merged
- have 3 pointers:
i
- keeps track of the items in the first sub-arrayj
- keeps track of the items in the second sub-arrayk
- keeps track of position in the final, sorted array
- iterate through the positions of the final array (
k
) - for each position, compare elements of first and second sub-arrays at
i
andj
, respectively- if element at
i
is smaller than element atj
, place element ati
into final array atk
- increment
i
- if element at
1. Copy
original array [9] [2] [5] [4] [6] [3] [7]
aux array [9] [2] [5] [4] [6] [3] [7]
2. Divide
original array [3] [2] [4] [6] [1] [7] [8]
k
aux array [3] [2] [4] [6] | [1] [7] [8]
i j
3. Compare
aux[i] = 3, aux[j] = 1
aux[j] is smaller
place aux[j] into orig[k]
increment j
increment k
original array [1] [2] [4] [6] [1] [7] [8]
k
aux array [3] [2] [4] [6] | [1] [7] [8]
i j
4. Compare Again
Until k
reaches the end of the original array.
Sort does the recursion.
- divide the array into two
- calls sort on the two sub-arrays (recursion)
- merge the sorted sub-arrays
####Analysis
Mergesort takes time ~N log(N)
.
- for each recursion, we will need
N
compares in the merging operation.- even if we half, we then have 2
N/2
compares - still sums toN
total compares
- even if we half, we then have 2
- the maximum amount of recursions is equal to the amount of times we can divide the arrays by 2:
logN
times
Mergesort can only sort data as large as half the memory (other half is needed for the aux
array).
####Minor Improvements
- for small sub-arrays, use Insertion-sort, as recursion is relatively costly in terms of access time and memory for small arrays.
- insertion sort is non-recursive
- insertion sort is ~ linear for small arrays
- check if sorted sub-arrays are already pre-sorted
- check if last element of sub-array 1 is smaller than first element of sub-array 2
####See Also