Group 36 - ALG Group Assignment - Part A
Group 36 - ALG Group Assignment - Part A
CT065-3-3-ALG
ALGORITHMICS
APU3F2209CS
WEIGHTAGE: 30%
INSTRUCTIONS TO CANDIDATES:
Table of contents
TABLE OF CONTENTS.........................................................................................................................1
1.0MERGE SORT...................................................................................................................................2
2
Algorithms Assignment CT065-3-3-ALG
Merge Sort is a sorting algorithm based on the Divide and Conquer principle. It
consists of splitting the array into two equal halves, combining them sort wise,
then recombining them. In addition, the sub-lists will be divided into halves
repeatedly until they cannot be divided anymore. Sub-lists are created first by
splitting the list into parts. The ‘merge’ stage begins after that. In this step, the
sub-lists are matched up and sorted according to their order. (GeeksForGeeks,
2022)
Unlike bubble sort and insertion sort, merge sort traverses the entire list fewer
times so it is faster for larger lists. It follows a set schedule and performs different
parts of a stage at the same time
3
Algorithms Assignment CT065-3-3-ALG
4
Algorithms Assignment CT065-3-3-ALG
Combine the elements in the same way they were broken after dividing until
they cannot be divided again. Comparing the elements of each array first, then
merging them in sorted order is a good way to merge arrays. The first comparison
should be made between 12 and 31, which both occur in sorted order. Next,
compare 25 with 8, and place 8 first among the two values. Then, rank 32 and 17
so that 17 comes out on top. Finally, arrange them logically based on a
comparison between 42 and 40. In the following iteration of combining, compare
the arrays containing two data values and combine them into a set of found values
in sorted order. As a result of the final merge, the array order will be 8, 12, 17, 25,
31, 32, 40, 42. (javatpoint, 2022)
5
Algorithms Assignment CT065-3-3-ALG
6
Algorithms Assignment CT065-3-3-ALG
7
Algorithms Assignment CT065-3-3-ALG
We can use an example to understand how insertion sort algorithm work in an array
list.
8
Algorithms Assignment CT065-3-3-ALG
After the first two elements are sorted, we take the third element, 1 and compare it
with its left elements. We can see that the third element, 1 is smaller than its left
elements. So that, we put the element, 1 become the first place, 5 and 9 move back
one place.
9
Algorithms Assignment CT065-3-3-ALG
After that, we sorted the element left according to the algorithm steps above. The
element 4 is smaller than elements 5 and 9, so that the element 4 is put on the left
place of 5 and 9. The element 3 is smaller than elements 4, 5, 9, so that the element 3
is put on the left place of 4, 5, and 9. Then, the array is sorted. This is how the
insertion sort algorithm works.
10
Algorithms Assignment CT065-3-3-ALG
11
Algorithms Assignment CT065-3-3-ALG
12
Algorithms Assignment CT065-3-3-ALG
13
Algorithms Assignment CT065-3-3-ALG
(GeeksforGeeks, 2020)
14
Algorithms Assignment CT065-3-3-ALG
15
Algorithms Assignment CT065-3-3-ALG
= n+nlogn
That is, the time complexity of the merge sort is O(nlogn), regardless of the best case,
worst case, and average case.
1 0
2 1
3 2
4 3
5 4
... ...
n n-1
that is, 0+1+2+3+4+5+... +n-1=[n(n-1)]/2
So the time complexity of the worst case of insertion sort is O(n2)
The best case: After sorting every element, the time complexity is Ω(n)
16
Algorithms Assignment CT065-3-3-ALG
The average case: for A[k] at position k, compare once, at position k-1, compare
twice... Position 2 is compared k-1 times, position 1 is compared k-1 times, so the
average number of comparisons is 1/k(1+2+3+4+... +k-1+k-1)=(k-1)/2
Hence,for n elements compare is ∑nk=1(k-1)/2=(n2-n)/4 = θ(n2)
Summery for Insertion Sort, the time complexity of best case is: Ω(n)
Worst case is: O(n2)
Average case is: θ(n2)
The time spent on sorting in insertion sort is usually more than one times the code
actually loops, this is because a failed comparison is made and then the loop is
launched, for example , in a loop like for while loop where the loop in for such is set
to n times and the others are n-1, however it is carried to the while loop where the
loop is also divided into cases. In worst case the time complexity is O(n 2) the growth
magnitude is n2, so insertion sort is more advantageous in case of less number of runs,
and this advantage also requires that the elements are basically ordered.
17
Algorithms Assignment CT065-3-3-ALG
3.0 References
Coder, A. (2020, January 12). Bubbling, selection, insertion and sorting and their application
scenarios. Retrieved from CSDN:
https://blog.csdn.net/zb987570437/article/details/103950738?ops_request_misc=%257B
%2522request%255Fid%2522%253A%2522166927892116782388020682%2522%252C
%2522scm%2522%253A
%252220140713.130102334..%2522%257D&request_id=166927892116782388020682
&biz_id=0&utm_me
GeeksForGeeks. (2022, November 15). Merge Sort Algoritms. Retrieved from GeeksForGeeks:
https://www.geeksforgeeks.org/merge-sort/#:~:text=The%20Merge%20Sort
%20algorithm%20is,combined%20in%20a%20sorted%20manner.
javatpoint. (2022, March 22). Merge Sort Algorithms. Retrieved from javatpoint:
https://www.javatpoint.com/merge-sort
Len, M. (2019, Apri 05). Merge Sort algorithm, a stable and fast sorting algorithm. Retrieved
from Megic Len: https://magiclen.org/merge-sort
Programiz. (n.d.). Insertion Sort Algorithm. Retrieved from Programiz:
https://www.programiz.com/dsa/insertion-sort
18