You are on page 1of 19

AZƏRBAYCAN RESPUBLİKASI TƏHSİL NAZİRLİYİ

Bakı Mühəndislik Universiteti

Fakultə: Mühəndislik
Kafedra: Kompüter və İnformasiya Texnologiyaları
İxtisas: İnformasiya Texnologiyaları

Hesabat qəbul edilir:


Qiymət (Bal)
kafedranın baş müəllimi

Həsənov Əli Vahid


(imza,tarix) (S.A.A)

Fənn: « Computer Algorithms »

Fərdi fəaliyyət
« Merge Sort »
mövzusu üzrə

HESABAT

3 kurs tələbəsi Qurbanova Nigar Mustafa


(S.A.A)

(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

1. 1. What is a computer algorithm?

An algorithm is a set of sequential instructions used in computers to carry out


certain operations and respond to predetermined demands or judgments. These are
sequential, organized, and finite sequences of steps that enable issue solving or
decision making.
Since the same algorithm or flowchart may be expressed in several programming
languages, the algorithms have nothing to do with programming languages; rather,
they are an ordering before programming.
When seen in this light, a program is nothing more than a sophisticated set of
algorithms that have been organized and programmed using a programming language
for eventual execution on a computer.
Algorithms are frequently used in mathematics and logic and serve as the foundation
for user guides, instruction manuals, and other types of writing. Its name is a
combination of the Persian mathematician Al-last Juarismi's name and the Latin
algorithmus. The procedure credited to Euclid to find the greatest common divisor of
two positive integers, or the so-called Euclidean algorithm, is one of the most well-
known in mathematics.
2) Parts of an algorithm
Every algorithm must consist of the following parts:

 Input or input . The input of the data that the algorithm needs to operate.


 Process . This is the formal logical operation that the algorithm will undertake with
the input received.
 Output or output . The results obtained from the process on the input, once the
execution of the algorithm is finished.
3) What is an algorithm for?
Simply put, an algorithm serves to solve a problem step by step . It is a series of
4

ordered and sequenced instructions to guide a particular process.


In Computer Science , however, algorithms constitute the skeleton of the processes
that will then be encoded and programmed to be performed by the computer.

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. Sequential . The algorithms operate in sequence, must be processed one at a time.


2. Accurate . The algorithms must be precise in their approach to the subject, that is,
they cannot be ambiguous or subjective.
3. Ordered . The algorithms must be established in the precise and exact sequence so
that their reading makes sense and the problem is solved.
4. Finite . Every sequence of algorithms must have a specific purpose, it cannot be
extended to infinity.
5. Concrete . Every algorithm must offer a result based on the functions it fulfills.
6. Defined . The same algorithm before the same input elements must always give the
same results.
5

İn short, to make a computer do anything, we have to write a computer program.


To write a computer program, we have to tell the computer, step by step, exactly what
we want to do it. The computer then “executes” the program, following each step
mechanically, to accomplish the end goal. When we are telling the computer what to
do, we also get to choose how it is going to do it. That is where computer algorithms
come in. The algorithm is the basic technique used to get the job done.
In computer programming, there are often many different ways (algorithms) to
accomplish any given task. Each algorithm has advantages and disadvantages in
different situations. Sorting is one place where a lot of research has been done,
because computers spend a lot of time sorting lists.

1. 2. Sorting algorithms

As implied by the name of the algorithm, the goal of sorting algorithms is to


quickly arrange the data we have, either from largest to smallest or from smallest to
largest. This is sorted using a variety of methods. Some are too fast for a tiny quantity
of data, some are very fast but complex to write, and some are simple to write.
With constrained memory and computing power, it is to ensure that any number of
different types of data are sorted in a specific sequence. Finding an algorithm that
would provide the best memory and performance combination is crucial here.
Many applications in our daily lives employ sorting and searching strategies to keep
the elements organized. One may use registers and phone books as examples of these
applications. The goal may be thought of as maintaining the relevant information in
the memory sequentially and expediting searches as needed.
And these are examples of commonly used sorting algorithm: Selection sort, Quick
sort, Merge sort, Bubble sort, Radix sort, Heap sort, Bucket sort, Insertion sort,
Counting sort, Shell sort, Comb sort . Additionally I want to mention that memory
efficiency and time efficiency are most important criteria for sorting algorithms.
6

II. MERGE SORT

2. 1. Divide and conquer

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

I. 2. Brief explanation of merge sort algorithm

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.

2.4. Writing merge algorithm’s code

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).

void merge(int A[ ] , int start, int mid, int end) {


//stores the starting position of both parts in temporary variables
int p = start ,q = mid+1;
int Arr[end-start+1] , k=0;
for (int i = start ; i <= end; i++) {
if(p > mid) //checks if first part comes to an end or not
Arr[ k++ ] = A[ q++] ;
else if ( q > end) //checks if second part comes to an end or not
Arr[ k++ ] = A[ p++ ];
else if( A[ p ] < A[ q ]) //checks which part has smaller element
Arr[ k++ ] = A[ p++ ];
else
Arr[ k++ ] = A[ q++];
}
for (int p=0 ; p< k ;p ++) {
/* Now the real array has elements in sorted manner including both
parts*/
A[ start++ ] = Arr[ p ] ; }
11

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

III. PROBLEMS AND EXPLANATION OF MERGE SORT

Sort the numbers from smallest to largest:

Input − The unsorted list: 14 20 78 98 20 45

Output − Array after Sorting: 14 20 20 45 78 98

Algorithm:
merge(array, left, middle, right)
Input: The data set array, left, middle and right index

Output: The merged list

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

      leftArr[i] := array[left +1]

   done

   for j := 0 to nRight do

      rightArr[j] := array[middle + j +1]

   done

   i := 0, j := 0, k := left

   while i < nLeft AND j < nRight do

      if leftArr[i] <= rightArr[j] then

         array[k] = leftArr[i]

         i := i+1
13

      else

         array[k] = rightArr[j]

j := j+1

         k := k+1

   done

while i < nLeft do

array[k] := leftArr[i]

i := i+1

k := k+1

done

while j < nRight do

array[k] := rightArr[j]

j := j+1

k := k+1

done

End

mergeSort(array, left, right)


Input: An array of data, and lower and upper bound of the array

Output: The sorted Array

Begin

if lower < right then

mid := left + (right - left) /2

mergeSort(array, left, mid)

mergeSort (array, mid+1, right)


14

merge(array, left, mid, right)

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

while(j<nr) { //extra element in right array


array[k] = rarr[j];
j++; k++; }
}
void mergeSort(int *array, int l, int r) {
int m;
if(l < r) {
int m = l+(r-l)/2;
// Sort first and second arrays
mergeSort(array, l, m);
mergeSort(array, m+1, r);
merge(array, l, m, r); }}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n]; //create an array with given number of elements
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++) {
cin >> arr[i];
}
cout << "Array before Sorting: ";
display(arr, n);
mergeSort(arr, 0, n-1); //(n-1) for last index
cout << "Array after Sorting: ";
display(arr, n);
}

OUTPUT:

Enter the number of elements: 6


Enter elements:
14 20 78 98 20 45
Array before Sorting: 14 20 78 98 20 45
Array after Sorting: 14 20 20 45 78 9
16

IV. MERGE SORT VS QUICK SORT

Merge Sort Quick Sort


Partition of elements in  The array is parted The array is parted into
the array into just 2 halves any ratio
Worst case complexity Worst case and The worst case
average case has same complexity of quick sort
complexities O(n logn) is O(n2) as there is need
of lot of comparisons in
the worst condition
Usage with datasets Merge sort can work The quick sort cannot
well on any type of work well with large
data sets irrespective of datasets
its size
Preferred for Merge sort is preferred Quick sort is preferred
for linked lists for arrays
Efficiency Merge sort is more Quick sort is more
efficient and works efficient and works faster
faster than quick sort than merge sort in case of
in case of larger array smaller array size or
size or datasets. datasets
Stability Merge sort is stable as Quick sort is unstable in
two elements with this scenario. But it can
equal value appear in be made stable using
the same order in some changes in code
sorted output as they
were in the input
unsorted array
Table.2.
17

V. FEATURES OF MERGE SORT

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

5.3. Advantages and disadvantages

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

-Merge sort was invented by John Von Neumann (1903-1957)


-Merge sort is divide & conquer technique of sorting element
-Merge sort is one of the most efficient sorting algorithm
-Time complexity of merge sort is O(n log n)
19

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/

You might also like