Professional Documents
Culture Documents
Fakultə: Mühəndislik
Kafedra: Kompüter və İnformasiya Texnologiyaları
İxtisas: İnformasiya Texnologiyaları
Fərdi fəaliyyət
« Merge Sort »
mövzusu üzrə
HESABAT
(imza,tarix)
BMU-2022
2
CONTENT
I. Introduction................................................................................................3
I.1. What is a computer algorithm?........................................................3
I.2. Sorting algorithms............................................................................5
II. II. Merge sort..............................................................................................6
II.1. Divide and conquer..........................................................................6
II.2. Brief explanation of merge sort algorithm.......................................7
II.3. The merge step of merge sort...........................................................8
II.4. Writing merge algorithm’s code…………......................................8
III. III. Problems and explanation of merge sort ………................................12
IV. IV. Merge sort vs Quick sort…………………………………………..…16
V. V. Features of merge sort.............................................. ...........................17
VI. V.1. Complexity........................................................................................17
VII. V.2. Applications.......................................................................................17
VIII. V.3. Advantages and disadvantages..........................................................18
IX. Summary...................................................................................................18
X. References.................................................................................................19
3
INTRODUCTİON
4) Types of algorithms
There are 4 types of computer algorithms:
1.Computational algorithms . An algorithm whose resolution depends on the
calculation, and which can be developed by a calculator or computer without
difficulties.
2.No computational algorithms . Those who do not require the processes of a
computer to resolve, or whose steps are exclusive to the resolution by a human
being .
3.Qualitative algorithms . It is an algorithm whose resolution does not involve
numerical calculations, but logical and / or formal sequences.
Quantitative algorithms . On the contrary, it is an algorithm that depends on
mathematical calculations to find its resolution.
5) Characteristics of the algorithms
An algorithm must offer a result based on its functions.
The algorithms have the following characteristics:
1. 2. Sorting algorithms
The “Merge Sort” uses a recursive algorithm to achieve its results. The divide-and-
conquer algorithm breaks down a big problem into smaller, more manageable pieces
that look similar to the initial problem. It then solves these subproblems recursively
and puts their solutions together to solve the original problem.
Let’s suppose we had to sort an array C. C sub-problem would be to sort a sub-
section of this array starting at index p and ending at index r, represented by C[p...r].
Divide: If q is the half-way point between p and r, then we can divide the subarray
C[p..r] into two arrays C[p..q] and C[q+1, r].
Conquer: this step, we try to sort both the subarrays C[p..q] and C[q+1, r]. If we
haven't yet reached the base case, we again divide both these subarrays and try to sort
them.
Combine: When the conquer step reaches the base step and we get two sorted
subarrays(like this: C[p..q] and C[q+1, r]) for array C[p..r], we combine the results by
creating a sorted array C[p..r] using two sorted subarrays (C[p..q] and C[q+1, r]).
Picture.1
7
When we attempt to use the Merge sort function on a subarray of size 1, for
instance p == r, a condition that requires the array to be split into two halves is not
satisfied. Then, until the whole array is merged, it expands the smaller, individually
sorted sub arrays into bigger arrays.
1. If(p<r) Check for base case
2. Then q → [(p + r) / 2] Divide step
3. Merge sort (A, p, q) Conquer step
4. Merge sort (A, q+1,r) Conquer step
5. Merge (A, p, q, r) Conquer step
Table 1.
Dividing procedure:
Picture 2.
8
As seen in the picture above, the merge sort algorithm splits the array in half until
the base condition is satisfied, at which point there is only one element remaining in
the array. The merge function then takes the subarrays that have been sorted and
merges them to sort the main array.
2. 3. The merge step of merge sort
Each and every recursive algorithm depends on a base case and the capacity to mix
the outcomes of base cases. The same applies to merge sort. But the merging step is
the most crucial stage in the merge sort algorithm.The merge step provides a
straightforward solution to the issue of combining two sorted lists (arrays) into a
single, massive sorted list (array).
If we have not reached the end of any of the arrays, so we have to compare current
elements of both arrays, copy smaller element into sorted array and then move pointer
of element containing smaller element. But if we have reached so we have to copy
all remaining elements of non-empty array.
Idea: 1)Divide the unsorted list into N sublists, each containing 1 element.
2)Take adjacent pairs of two singleton lists and merge them to form a list of 2
elements. N will now convert into N/2 lists of size 2.
3)Repeat the process till a single sorted list of obtained.
As seen in the graphic below, a list of size is divided into sublists of size at each
step until there is no more division possible. Consider a smaller array containing the
items to help you understand.
This lengthy list is first broken into two smaller lists, the first of which is
composed of elements and the second of which is composed of items. The first list of
elements is now further broken down into sublists of elements and correspondingly.
We now begin to combine these lists because it is impossible to further decompose
this list and because each sublist only has a small number of elements. The sub-lists
created in the previous phase are then combined in sorted order using the methods
9
described above, creating a new list. In order to create the new sorted list, we must
merge the list that includes the element with this list.
Picture.3
10
As seen in the image above, a list of size M is divided into two sublists of size M/2
at each step until no further division is possible. Consider a smaller array A that
contains the items to help us understand (9,7,8).This list of size 3 is split into two
sublists at the first step, the first of which contains the items (9,7) and the second of
which is (8). Now, the first list of components (9, 7) is further divided into two
sublists, each of which contains elements (9) and (7). As no further breakdown of
this list can be done, as each sublist consists of a maximum of 1 element, we now
start to merge these lists. The 2 sub-lists formed in the last step are then merged
together in sorted order using the procedure mentioned above leading to a new
list (7,9). Backtracking further, we then need to merge the list consisting of
element (8) too with this list, leading to the new sorted list (7,8,9).
Here, we'll use the merge function to combine two array portions, one of which has
starting and ending places that range from start to mid, and another whose positions
range from mid+1 to end.
The initial elements of both arrays are combined to form a beginning. such as p and
q. The element with the lesser value will then be put in the auxiliary array after the
respective elements of both portions have been compared (Arr[ ]). All the items of
another part of the array are added in the auxiliary array in the same order they exist
if at some point one part of the array comes to an end.
Let’s consider the following 2 branched recursive function :
void merge_sort (int A[ ] , int start , int end ) {
if ( start < end ) {
int mid = (start + end ) / 2 ; // defines the current array in 2 parts
merge_sort (A, start , mid ) ; // sort the 1st part of array
merge_sort (A,mid+1 , end ) ; // sort the 2nd part of array
// merge the both parts by comparing elements of both the parts.
merge(A,start , mid , end );
}}
12
Algorithm:
merge(array, left, middle, right)
Input: The data set array, left, middle and right index
Begin
nLeft := m - left+1
nRight := right – m
define arrays leftArr and rightArr of size nLeft and nRight respectively
for i := 0 to nLeft do
done
for j := 0 to nRight do
done
i := 0, j := 0, k := left
array[k] = leftArr[i]
i := i+1
13
else
array[k] = rightArr[j]
j := j+1
k := k+1
done
array[k] := leftArr[i]
i := i+1
k := k+1
done
array[k] := rightArr[j]
j := j+1
k := k+1
done
End
Begin
End
Code example:
#include<iostream>
using namespace std;
void swapping(int &a, int &b) { //swap the content of a and b
int temp;
temp = a;
a = b;
b = temp;}
void display(int *array, int size) {
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;}
void merge(int *array, int l, int m, int r) {
int i, j, k, nl, nr;
//size of left and right sub-arrays
nl = m-l+1; nr = r-m;
int larr[nl], rarr[nr];
//fill left and right sub-arrays
for(i = 0; i<nl; i++)
larr[i] = array[l+i];
for(j = 0; j<nr; j++)
rarr[j] = array[m+1+j];
i = 0; j = 0; k = l;
//marge temp arrays to real array
while(i < nl && j<nr) {
if(larr[i] <= rarr[j]) {
array[k] = larr[i];
i++; }else{
array[k] = rarr[j]; j++; }
k++; }
while(i<nl) { //extra element in left array
array[k] = larr[i];
i++; k++;}
15
OUTPUT:
It perform in O(n log n) in the worst case. It is stable, very independent from the
initial list's structure, and suitable for linked lists. Can be put into practice so that data
is retrieved in a consecutive order.
5.1. Complexity
Time complexity:
1. Best Case Complexity: O(n*log n)
2. Worst Case Complexity: O(n*log n)
3. Average Case Complexity: O(n*log n)
Space complexity:
1. The space complexity of merge sort is O(n)
5.2. Aplications
-Merge Sort is useful for sorting linked lists in O(N log N) time.In the case of
linked lists, the case is different mainly due to the difference in memory
allocation of arrays and linked lists. Unlike arrays, linked list nodes may not be
adjacent in memory. Unlike an array, in the linked list, we can insert items in the
middle in O(1) extra space and O(1) time. Therefore, the merge operation of
merge sort can be implemented without extra space for linked lists.
In arrays, we can do random access as elements are contiguous in memory. Let
us say we have an integer (4-byte) array A and let the address of A[0] be x then
to access A[i], we can directly access the memory at (x + i*4). Unlike arrays, we
can not do random access in the linked list. Quick Sort requires a lot of this kind
of access. In a linked list to access i’th index, we have to travel each and every
node from the head to i’th node as we don’t have a contiguous block of memory.
18
Therefore, the overhead increases for quicksort. Merge sort accesses data
sequentially and the need of random access is low.
-Inversion count problem
-Used in External sorting
Advantages:
Merge sort algorithm is best case for sorting slow-access data e.g) tape drive.
Merge sort algorithm is better at handling sequential - accessed lists.
Disadvantages:
The running time of merge sort algorithm is 0(n log n). which turns out to be
the worse case.
Merge sort algorithm requires additional memory spance of 0(n) for the
temporary array TEMP.
VI. SUMMARY
REFERENCES
https://www.geeksforgeeks.org/merge-sort/
https://en.wikipedia.org/wiki/Merge_sort
https://www.programiz.com/dsa/merge-sort
https://www.javatpoint.com/merge-sort
https://www.educba.com/merge-sort-algorithm/
https://www.khanacademy.org/computing/computer-science/algorithms/merge-sort/
a/overview-of-merge-sort
https://www.tutorialspoint.com/data_structures_algorithms/
merge_sort_algorithm.htm
https://www.bing.com/search?q=merge+sort+step+by+step&FORM=QSRE1
https://stackoverflow.com/questions/36135277/merge-sort-step-by-step-execution
https://www.pcmag.com/encyclopedia/term/merge-sort
https://www.geeksforgeeks.org/quick-sort-vs-merge-sort/
https://short-facts.com/what-are-the-advantages-and-disadvantages-of-a-merge-sort/
https://solsarin.com/merge-sort-advantages-and-disadvantages/