Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Merge In Mergesort

Informal explanation of merge in merge sort
We have two arrays that are already sorted: one on the left and the other on the right
We stick them together (left+right array). 
We then compare the terms starting from the 0th([0] term for both left and right arrays.
So left[0] compare with right[0](or more correctly, array[0] with array[mid+1])
We create a blank array
If the left[0] term is bigger, we add left[0] to blank array as its first term. 
If the right[0] term is bigger, we add right[0] to the blank array as its first term.
Assuming left[0] is bigger, We then move to left[1] vs right[0].
We keep doing this until we run out of terms in one of the arrays. 
We then stick the remaining terms of the array where we didn't run out to the end of our (originally, now no longer) blank array. 
The blank array has now been sorted! 
 
PREVIOUS NEXT
Tagged: #Merge #In #Mergesort
ADD COMMENT
Topic
Name
3+3 =