2. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
[2,4,5,8] [1,3,6,7]
[]
3. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
[2,4,5,8] [1,3,6,7]
[]
How?
- Compare the first element in
each array
4. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
[2,4,5,8] [3,6,7]
[1, ]
How?
- Compare the first element in
each array
- Move whichever is less to the
back of a sorted array
5. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
[4,5,8] [3,6,7]
[1, 2]
How?
- Compare the first element in
each array
- Move whichever is less to the
back of a sorted array
6. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
[4,5,8] [6,7]
[1, 2, 3]
How?
- Compare the first element in
each array
- Move whichever is less to the
back of a sorted array
7. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
[5,8] [6,7]
[1, 2, 3, 4]
How?
- Compare the first element in
each array
- Move whichever is less to the
back of a sorted array
- Works for non-perfect
alternations
8. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
[8] [6,7]
[1, 2, 3, 4, 5]
How?
- Compare the first element in
each array
- Move whichever is less to the
back of a sorted array
- Works for non-perfect
alternations
9. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
[8] [7]
[1, 2, 3, 4, 5, 6]
How?
- Compare the first element in
each array
- Move whichever is less to the
back of a sorted array
- Works for non-perfect
alternations
10. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
[8] []
[1, 2, 3, 4, 5, 6, 7]
How?
- Compare the first element in
each array
- Move whichever is less to the
back of a sorted array
- Works for non-perfect
alternations
- For termination need to deal with
less than or is the only one that
exists
11. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
[] []
[1, 2, 3, 4, 5, 6, 7, 8]
How?
- Compare the first element in
each array
- Move which ever is less to the
back of a sorted array
- Works for non-perfect
alternations
- For termination need to deal with
when one array has run out of
elements, then we can move all
remain elements immediately
12. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
How?
- Compare the first element in
each array
- Move whichever is less to the
back of a sorted array
- Works for non-perfect
alternations
- For termination need to deal with
less than or is the only one that
exists
function merge2(arr1, arr2) {
while (arr1.length ||
arr2.length) {
if (arr1[0] < arr2[0])
{
}
}
}
13. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
How?
- Compare the first element in
each array
- Move whichever is less to the
back of a sorted array
- Works for non-perfect
alternations
- For termination need to deal with
less than or is the only one that
exists
function merge2(arr1, arr2) {
let sorted = [];
while (arr1.length || arr2.length) {
if (arr1[0] < arr2[0]) {
sorted.push(arr1.
} else {
sorted.push(arr2.
}
}
}
14. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
How?
- Compare the first element in
each array
- Move whichever is less to the
back of a sorted array
- Works for non-perfect
alternations
- For termination need to deal with
less than or is the only one that
exists
function merge2(arr1, arr2) {
let sorted = [];
while (arr1.length || arr2.length) {
if (!arr2.length || arr1[0] < a
{
sorted.push(arr1.
} else {
sorted.push(arr2.
}
}
}
15. Assertion: it is easy to turn 2 arrays that are sorted
into 1 array that is sorted
How?
- Compare the first element in
each array
- Move whichever is less to the
back of a sorted array
- Works for non-perfect
alternations
- For termination need to deal with
less than or is the only one that
exists
function merge2(arr1, arr2) {
let sorted = [];
while (arr1.length || arr2.length) {
if (!arr2.length || arr1[0] < a
{
sorted.push(arr1.
} else {
sorted.push(arr2.
}
}
return sorted;
}
16. What does this have to do with merge sort?
● Merge sort relies on repeatedly combining small sorted arrays into larger
sorted array until eventually the entire array is sorted
● Relies on the assertion that an array of length 1 is sorted
● Arrays of length 1 can be produced iteratively (bottom-up) or recursively (top-
down)
17. Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[4,1,3,7]
[8,2,6,5]
HALF!
18. Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[4,1,3,7]
[8,2,6,5]
[4, 1] [3, 7]
HALF!
37. Top Down Method: (recursive)
mergeSort(array) {
//split array in halves
//merge sorted versions of those halves
}
38. Top Down Method: (recursive)
mergeSort(array) {
let first = array.slice(0, Math.floor(array.length / 2))
let second = array.slice(Math.floor(array.length / 2), array.length)
//merge sorted versions of those halves
}
39. Top Down Method: (recursive)
mergeSort(array) {
let first = array.slice(0, Math.floor(array.length / 2))
let second = array.slice(Math.floor(array.length / 2), array.length)
return merge2(mergeSort(first), mergeSort(second))
}
Recursive
step
40. Top Down Method: (recursive)
mergeSort(array) {
if(array.length < 2) {
return array;
}
let first = array.slice(0, Math.floor(array.length / 2))
let second = array.slice(Math.floor(array.length / 2), array.length)
return merge2(mergeSort(first), mergeSort(second))
}
Add a base
case
48. Bottom-Up Method: (iterative)
function mergeSort(array) {
// split up into sub arrays of length one
// while array of arrays still has multiple sub-arrays
// merge sub-array pairs and store result
// need to handle array of arrays with odd numbers of arrays
//replace original array of arrays with merge results
// return array of arrays[0]
}
49. Bottom-Up Method: (iterative)
function mergeSort(array) {
let container=[];
for (let el of array) { // split up into sub arrays of length one
container.push([el])
}
// while array of arrays still has multiple sub-arrays
// merge sub-array pairs and store result
// need to handle array of arrays with odd numbers of arrays
//replace original array of arrays with merge results
// return array of arrays[0]
}
50. Bottom-Up Method: (iterative)
function mergeSort(array) {
let container=[];
for (let el of array) { // split up into sub arrays of length one
container.push([el])
}
while (container.length > 1) { // while array of arrays still has multiple sub-
arrays
// merge sub-array pairs and store result iterate through container?
// need to handle array of arrays with odd numbers of arrays
// replace original array of arrays with merge results
}
// return array of arrays[0]
}
51. function mergeSort(array) {
let container=[];
for (let el of array) { // split up into sub arrays of length one
container.push([el])
}
while (container.length > 1) { // while array of arrays still has multiple sub-arrays
let newContainer = [] //store results
for (let i = 0; i < container.length; i+=2) { // iterate through container
newContainer.push(merge2(container[i], container[i + 1]))//merge sub-array pairs
// need to handle array of arrays with odd numbers of arrays
}
// replace original array of arrays with merge results
}
// return array of arrays[0]
}
52. function mergeSort(array) {
let container=[];
for (let el of array) { // split up into sub arrays of length one
container.push([el])
}
while (container.length > 1) { // while array of arrays still has multiple sub-arrays
let newContainer = [] //store results
for (let i = 0; i < container.length; i+=2) { // iterate through container
if (container[i+1] !== undefined) { // check for end of array odds
newContainer.push(merge2(container[i], container[i + 1])) //merge sub-arrays
} else {
newContainer.push(container[i]) // pass odd ends to next round
}
}
//replace original array of arrays with merge results
}
// return array of arrays[0]
}
53. function mergeSort(array) {
let container=[];
for (let el of array) { // split up into sub arrays of length one
container.push([el])
}
while (container.length > 1) { // while array of arrays still has multiple sub-arrays
let newContainer = [] //store results
for (let i = 0; i < container.length; i+=2) { // iterate through container
if (container[i+1] !== undefined) { // check for end of array odds
newContainer.push(merge2(container[i], container[i + 1])) //merge sub-arrays
} else {
newContainer.push(container[i]) // pass odd ends to next round
}
}
container = newContainer //replace original array of arrays with merge results
}
// return array of arrays[0]
}
54. function mergeSort(array) {
let container=[];
for (let el of array) { // split up into sub arrays of length one
container.push([el])
}
while (container.length > 1) { // while array of arrays still has multiple sub-arrays
let newContainer = [] //store results
for (let i = 0; i < container.length; i+=2) { // iterate through container
if (container[i+1] !== undefined) { // check for end of array odds
newContainer.push(merge2(container[i], container[i + 1])) //merge sub-arrays
} else {
newContainer.push(container[i]) // pass odd ends to next round
}
}
container = newContainer //replace original array of arrays with merge results
}
return container[0] // return array of arrays[0]
}