SlideShare a Scribd company logo
1 of 54
Merge Sort
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]
[]
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
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
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
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
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
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
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
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
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
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])
{
}
}
}
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.
}
}
}
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.
}
}
}
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;
}
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)
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[4,1,3,7]
[8,2,6,5]
HALF!
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!
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[4,1,3,7]
[8,2,6,5]
[4, 1] [3, 7]
[4] [1]
HALF!
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[4,1,3,7]
[8,2,6,5]
[4, 1] [3, 7]
[4] [1]
MERGE
Are sorted!
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[4,1,3,7]
[8,2,6,5]
[1, 4] [3, 7]MERGE
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[4,1,3,7]
[8,2,6,5]
[1, 4] [3, 7]
[3] [7]
MERGE
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[4,1,3,7]
[8,2,6,5]
[1, 4] [3, 7]MERGE
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[4,1,3,7]
[8,2,6,5]
[1, 4] [3, 7]
MERGE
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[1,3,4,7]
[8,2,6,5]MERGE
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[1,3,4,7]
[8,2,6,5]
[8,2] [6,5]
HALF!
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[1,3,4,7]
[8,2,6,5]
[8,2] [6,5]
[8]
[2]
HALF!
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[1,3,4,7]
[8,2,6,5]
[8,2] [6,5]
[8]
[2]
MERGE
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[1,3,4,7]
[8,2,6,5]
[2,8] [6,5]
MERGE
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[1,3,4,7]
[8,2,6,5]
[2,8] [6,5]
[6] [5]
HALF!
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[1,3,4,7]
[8,2,6,5]
[2,8] [6,5]
[6] [5]
MERGE
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[1,3,4,7]
[8,2,6,5]
[2,8] [5,6]
MERGE
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[1,3,4,7]
[8,2,6,5]
[2,8] [5,6]
MERGE
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[1,3,4,7]
[2,5,6,8] MERGE
Top Down Method: (recursive)
[4, 1, 3, 7, 8, 2, 6, 5]
[1,3,4,7]
[2,5,6,8]
MERGE
Top Down Method: (recursive)
[1, 2, 3, 4, 6, 7, 8]
MERGE
Top Down Method: (recursive)
mergeSort(array) {
//split array in halves
//merge sorted versions of those halves
}
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
}
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
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
Bottom-Up Method: (iterative)
[4, 1, 3, 7, 8, 2, 6, 5]
Bottom-Up Method: (iterative)
[4, 1, 3, 7, 8, 2, 6, 5]
[[4], [1], [3], [7], [8], [2], [6], [5]]
SPLIT?
Bottom-Up Method: (iterative)
[4, 1, 3, 7, 8, 2, 6, 5]
[[4], [1], [3], [7], [8], [2], [6], [5]]MERGE MERGE MERGE MERGE
Bottom-Up Method: (iterative)
[4, 1, 3, 7, 8, 2, 6, 5]
[[4], [1], [3], [7], [8], [2], [6], [5]]
[[1,4], [3,7], [2,8],[5,6]]
MERGE MERGE
Bottom-Up Method: (iterative)
[4, 1, 3, 7, 8, 2, 6, 5]
[[4], [1], [3], [7], [8], [2], [6], [5]]
[[1,4], [3,7], [2,8],[5,6]]
[[1,3,4,7], [2,5,6,8]]
MERGE
Bottom-Up Method: (iterative)
[4, 1, 3, 7, 8, 2, 6, 5]
[[4], [1], [3], [7], [8], [2], [6], [5]]
[[1,4], [3,7], [2,8],[5,6]]
[[1,3,4,7], [2,5,6,8]]
[[1,2,3,4,5,6,7,8]]
DONE!
Bottom-Up Method: (iterative)
[4, 1, 3, 7, 8, 2, 6, 5]
[[4], [1], [3], [7], [8], [2], [6], [5]]
[[1,4], [3,7], [2,8],[5,6]]
[[1,3,4,7], [2,5,6,8]]
[[1,2,3,4,5,6,7,8]]
[1,2,3,4,5,6,7,8]
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]
}
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]
}
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]
}
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]
}
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]
}
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]
}
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]
}

More Related Content

What's hot

Generalized Carleson Operator and Convergence of Walsh Type Wavelet Packet Ex...
Generalized Carleson Operator and Convergence of Walsh Type Wavelet Packet Ex...Generalized Carleson Operator and Convergence of Walsh Type Wavelet Packet Ex...
Generalized Carleson Operator and Convergence of Walsh Type Wavelet Packet Ex...IJERA Editor
 
Lecture 6 radial basis-function_network
Lecture 6 radial basis-function_networkLecture 6 radial basis-function_network
Lecture 6 radial basis-function_networkParveenMalik18
 
Lecture 2 fuzzy inference system
Lecture 2  fuzzy inference systemLecture 2  fuzzy inference system
Lecture 2 fuzzy inference systemParveenMalik18
 
A brief introduction to finite difference method
A brief introduction to finite difference methodA brief introduction to finite difference method
A brief introduction to finite difference methodPrateek Jha
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data typeMegha V
 
Lecture 1 computational intelligence
Lecture 1  computational intelligenceLecture 1  computational intelligence
Lecture 1 computational intelligenceParveenMalik18
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...Tosin Amuda
 
Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)IJERD Editor
 
[1062BPY12001] Data analysis with R / April 19
[1062BPY12001] Data analysis with R / April 19[1062BPY12001] Data analysis with R / April 19
[1062BPY12001] Data analysis with R / April 19Kevin Chun-Hsien Hsu
 
Functions of severable variables
Functions of severable variablesFunctions of severable variables
Functions of severable variablesSanthanam Krishnan
 
On Certain Classess of Multivalent Functions
On Certain Classess of Multivalent Functions On Certain Classess of Multivalent Functions
On Certain Classess of Multivalent Functions iosrjce
 
Class 9: Consistent Hashing
Class 9: Consistent HashingClass 9: Consistent Hashing
Class 9: Consistent HashingDavid Evans
 
Exercices calculs de_primitives
Exercices calculs de_primitivesExercices calculs de_primitives
Exercices calculs de_primitivesZaakXO
 

What's hot (18)

Generalized Carleson Operator and Convergence of Walsh Type Wavelet Packet Ex...
Generalized Carleson Operator and Convergence of Walsh Type Wavelet Packet Ex...Generalized Carleson Operator and Convergence of Walsh Type Wavelet Packet Ex...
Generalized Carleson Operator and Convergence of Walsh Type Wavelet Packet Ex...
 
Lecture 6 radial basis-function_network
Lecture 6 radial basis-function_networkLecture 6 radial basis-function_network
Lecture 6 radial basis-function_network
 
Lecture 2 fuzzy inference system
Lecture 2  fuzzy inference systemLecture 2  fuzzy inference system
Lecture 2 fuzzy inference system
 
A brief introduction to finite difference method
A brief introduction to finite difference methodA brief introduction to finite difference method
A brief introduction to finite difference method
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Fuzzy Set
Fuzzy SetFuzzy Set
Fuzzy Set
 
Lecture 1 computational intelligence
Lecture 1  computational intelligenceLecture 1  computational intelligence
Lecture 1 computational intelligence
 
MA3696 Lecture 5
MA3696 Lecture 5MA3696 Lecture 5
MA3696 Lecture 5
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
Module 6 the chain rule
Module 6 the chain ruleModule 6 the chain rule
Module 6 the chain rule
 
Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)
 
[1062BPY12001] Data analysis with R / April 19
[1062BPY12001] Data analysis with R / April 19[1062BPY12001] Data analysis with R / April 19
[1062BPY12001] Data analysis with R / April 19
 
Functions of severable variables
Functions of severable variablesFunctions of severable variables
Functions of severable variables
 
On Certain Classess of Multivalent Functions
On Certain Classess of Multivalent Functions On Certain Classess of Multivalent Functions
On Certain Classess of Multivalent Functions
 
Class 9: Consistent Hashing
Class 9: Consistent HashingClass 9: Consistent Hashing
Class 9: Consistent Hashing
 
Exercices calculs de_primitives
Exercices calculs de_primitivesExercices calculs de_primitives
Exercices calculs de_primitives
 
Chapter 4 Cyclic Groups
Chapter 4 Cyclic GroupsChapter 4 Cyclic Groups
Chapter 4 Cyclic Groups
 

Similar to Merge sort

Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printAbdii Rashid
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Flynce Miguel
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfArjunSingh81957
 
javascript-Array.ppsx
javascript-Array.ppsxjavascript-Array.ppsx
javascript-Array.ppsxVedantSaraf9
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...BhumikaBiyani1
 
Shell sorting
Shell sortingShell sorting
Shell sortingTUC
 
Merge and merge sorting
Merge and merge sortingMerge and merge sorting
Merge and merge sortingSaniaRana6
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsAakash deep Singhal
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updatedvrgokila
 

Similar to Merge sort (20)

CSPC/ PPS Sorting methods
CSPC/ PPS Sorting methodsCSPC/ PPS Sorting methods
CSPC/ PPS Sorting methods
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
 
Recursion Lecture in Java
Recursion Lecture in JavaRecursion Lecture in Java
Recursion Lecture in Java
 
F# array searching
F#  array searchingF#  array searching
F# array searching
 
javascript-Array.ppsx
javascript-Array.ppsxjavascript-Array.ppsx
javascript-Array.ppsx
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Sorting.ppt read only
Sorting.ppt read onlySorting.ppt read only
Sorting.ppt read only
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
 
Shell sorting
Shell sortingShell sorting
Shell sorting
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 
Merge and merge sorting
Merge and merge sortingMerge and merge sorting
Merge and merge sorting
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
 

Recently uploaded

INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...ThinkInnovation
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 

Recently uploaded (20)

INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 

Merge sort

  • 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!
  • 19. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [4,1,3,7] [8,2,6,5] [4, 1] [3, 7] [4] [1] HALF!
  • 20. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [4,1,3,7] [8,2,6,5] [4, 1] [3, 7] [4] [1] MERGE Are sorted!
  • 21. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [4,1,3,7] [8,2,6,5] [1, 4] [3, 7]MERGE
  • 22. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [4,1,3,7] [8,2,6,5] [1, 4] [3, 7] [3] [7] MERGE
  • 23. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [4,1,3,7] [8,2,6,5] [1, 4] [3, 7]MERGE
  • 24. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [4,1,3,7] [8,2,6,5] [1, 4] [3, 7] MERGE
  • 25. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [1,3,4,7] [8,2,6,5]MERGE
  • 26. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [1,3,4,7] [8,2,6,5] [8,2] [6,5] HALF!
  • 27. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [1,3,4,7] [8,2,6,5] [8,2] [6,5] [8] [2] HALF!
  • 28. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [1,3,4,7] [8,2,6,5] [8,2] [6,5] [8] [2] MERGE
  • 29. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [1,3,4,7] [8,2,6,5] [2,8] [6,5] MERGE
  • 30. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [1,3,4,7] [8,2,6,5] [2,8] [6,5] [6] [5] HALF!
  • 31. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [1,3,4,7] [8,2,6,5] [2,8] [6,5] [6] [5] MERGE
  • 32. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [1,3,4,7] [8,2,6,5] [2,8] [5,6] MERGE
  • 33. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [1,3,4,7] [8,2,6,5] [2,8] [5,6] MERGE
  • 34. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [1,3,4,7] [2,5,6,8] MERGE
  • 35. Top Down Method: (recursive) [4, 1, 3, 7, 8, 2, 6, 5] [1,3,4,7] [2,5,6,8] MERGE
  • 36. Top Down Method: (recursive) [1, 2, 3, 4, 6, 7, 8] MERGE
  • 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
  • 41. Bottom-Up Method: (iterative) [4, 1, 3, 7, 8, 2, 6, 5]
  • 42. Bottom-Up Method: (iterative) [4, 1, 3, 7, 8, 2, 6, 5] [[4], [1], [3], [7], [8], [2], [6], [5]] SPLIT?
  • 43. Bottom-Up Method: (iterative) [4, 1, 3, 7, 8, 2, 6, 5] [[4], [1], [3], [7], [8], [2], [6], [5]]MERGE MERGE MERGE MERGE
  • 44. Bottom-Up Method: (iterative) [4, 1, 3, 7, 8, 2, 6, 5] [[4], [1], [3], [7], [8], [2], [6], [5]] [[1,4], [3,7], [2,8],[5,6]] MERGE MERGE
  • 45. Bottom-Up Method: (iterative) [4, 1, 3, 7, 8, 2, 6, 5] [[4], [1], [3], [7], [8], [2], [6], [5]] [[1,4], [3,7], [2,8],[5,6]] [[1,3,4,7], [2,5,6,8]] MERGE
  • 46. Bottom-Up Method: (iterative) [4, 1, 3, 7, 8, 2, 6, 5] [[4], [1], [3], [7], [8], [2], [6], [5]] [[1,4], [3,7], [2,8],[5,6]] [[1,3,4,7], [2,5,6,8]] [[1,2,3,4,5,6,7,8]] DONE!
  • 47. Bottom-Up Method: (iterative) [4, 1, 3, 7, 8, 2, 6, 5] [[4], [1], [3], [7], [8], [2], [6], [5]] [[1,4], [3,7], [2,8],[5,6]] [[1,3,4,7], [2,5,6,8]] [[1,2,3,4,5,6,7,8]] [1,2,3,4,5,6,7,8]
  • 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] }