Data Structures
Home / My courses / BCA204A21T / Unit 2 - SEARCHING & SORTING - Merge Sort / MERGE SORT
MERGE SORT
Definition
Example
Algorithm
Definition: Merge Sort
Merge sort is one of the most efficient sorting algorithms. It works on the principle of Divide and Conquer. Merge sort repeatedly breaks down a list into several sublists
until each sublist consists of a single element and merging those sublists in a manner that results into a sorted list.
Conceptually, a merge sort works as follows:
1. Divide the unsorted list into ‘n’ sublists, each containing one element (a list of one element is considered sorted).
2. Repeatedly merge sublists to produce new sorted sublists until there is only one sublist remaining. This will be the sorted list.
Example
◄ Selection Sort Demo Jump to... E book on Data Structures ►
Data Structures
Home / My courses / BCA204A21T / Unit 2 - SEARCHING & SORTING - Merge Sort / MERGE SORT
MERGE SORT
Definition
Example
Algorithm
Algorithm: Merge Sort
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
Algorithm:
procedure mergesort( var a as array )
if ( n == 1 ) return a
var l1 as array = a[0] ... a[n/2]
var l2 as array = a[n/2+1] ... a[n]
l1 = mergesort( l1 )
l2 = mergesort( l2 )
return merge( l1, l2 )
end procedure
procedure merge( var a as array, var b as array )
var c as array
while ( a and b have elements )
if ( a[0] > b[0] )
add b[0] to the end of c
remove b[0] from b
else
add a[0] to the end of c
remove a[0] from a
end if
end while
while ( a has elements )
add a[0] to the end of c
remove a[0] from a
end while
while ( b has elements )
while ( b has elements )
add b[0] to the end of c
remove b[0] from b
end while
return c
end procedure
Challenge Questions
◄ Selection Sort Demo Jump to... E book on Data Structures ►
Data Structures
Home / My courses / BCA204A21T / Unit 2 - SEARCHING & SORTING - Merge Sort / MERGE SORT
MERGE SORT
Definition
Example
Algorithm
Example: Merge Sort
Step 1: Merge sort first divides the whole array iteratively into equal halves unless the atomic values are achieved.
Step 2: This does not change the sequence of appearance of items in the original. Now divide these two arrays into halves.
Step 3: Further divide these arrays and we achieve atomic value which can no more be divided.
Step 4: First compare the element for each list and then combine them into another list in a sorted manner. We see that 14 and 33 are in sorted positions. We compare 27
and 10 and in the target list of 2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42 and 44 are placed sequentially.
Step 5: In the next iteration of the combining phase, compare lists of two data values, and merge them into a list of found data values placing all in a sorted order.
Step 6: After the final merging.
Merge sort keeps on dividing the list into equal halves until it can no more be divided. By definition, if it is only one element in the list, it is sorted. Then, merge sort combines
the smaller sorted lists keeping the new list sorted too.
Algorithm
◄ Selection Sort Demo Jump to... E book on Data Structures ►