0% found this document useful (0 votes)
103 views18 pages

Group 36 - ALG Group Assignment - Part A

The document describes algorithms and complexity analysis. It provides: 1) An overview of merge sort and insertion sort algorithms, including their steps and time complexity. Merge sort divides the array into halves, recursively sorts the halves, and then merges the sorted halves. Insertion sort iterates through the array and inserts each element into the sorted portion. 2) Code snippets demonstrating merge sort and insertion sort algorithms. The merge sort code shows the divide, conquer, and merge stages. The insertion sort code shows comparing and swapping elements to insert them in the sorted portion. 3) Analysis of the time complexity of merge sort and insertion sort. Merge sort has O(n log n) time complexity, making it efficient

Uploaded by

Wong Hui Chuo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views18 pages

Group 36 - ALG Group Assignment - Part A

The document describes algorithms and complexity analysis. It provides: 1) An overview of merge sort and insertion sort algorithms, including their steps and time complexity. Merge sort divides the array into halves, recursively sorts the halves, and then merges the sorted halves. Insertion sort iterates through the array and inserts each element into the sorted portion. 2) Code snippets demonstrating merge sort and insertion sort algorithms. The merge sort code shows the divide, conquer, and merge stages. The insertion sort code shows comparing and swapping elements to insert them in the sorted portion. 3) Analysis of the time complexity of merge sort and insertion sort. Merge sort has O(n log n) time complexity, making it efficient

Uploaded by

Wong Hui Chuo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

ASSIGNMENT

TECHNOLOGY PARK MALAYSIA

CT065-3-3-ALG

ALGORITHMICS

APU3F2209CS

HAND OUT DATE: 10 OCTOBER 2022

HAND IN DATE: 24 NOVEMBER 2022

WEIGHTAGE: 30%

INSTRUCTIONS TO CANDIDATES:

1 Submit your assignment at the administrative counter.


2 Students are advised to underpin their answers with the use of references
(cited using the Harvard Name System of Referencing).
3 Late submission will be awarded zero (0) unless Extenuating Circumstances
(EC) are upheld.
4 Cases of plagiarism will be penalized.
5 The assignment should be bound in an appropriate style (comb bound or
stapled).
6 Where the assignment should be submitted in both hardcopy and softcopy,
the softcopy of the written assignment and source code (where appropriate)
should be on a CD in an envelope / CD cover and attached to the hardcopy.
7 You must obtain 50% overall to pass this module.
Algorithms Assignment CT065-3-3-ALG

Table of contents
TABLE OF CONTENTS.........................................................................................................................1

PART A: ALGORITHMS AND COMPLEXITY.................................................................................2

1.0MERGE SORT...................................................................................................................................2

1.1 INTRODUCTION TO MERGE SORT.....................................................................................................2


1.2MERGE SORT ALGORITHM STEPS......................................................................................................3
1.3 EXAMPLE OF CODE SNIPPETS (MERGE SORT).................................................................................5

2.0 INSERTION SORT...........................................................................................................................8

2.1 INTRODUCTION TO INSERTION SORT................................................................................................8


2.2 INSERTION SORT ALGORITHM STEPS...............................................................................................8
2.3 EXAMPLE OF CODE SNIPPET (INSERTION SORT).............................................................................12

2.0 TIME COMPLEXITY....................................................................................................................14

2.1 MERGE SORT TIME COMPLEXITY...................................................................................................14


2.2 MERGE SORT EFFECTIVENESS........................................................................................................15
2.3 INSERTION SORT.............................................................................................................................15
2.4 INSERTION SORT EFFECTIVENESS...................................................................................................16

2
Algorithms Assignment CT065-3-3-ALG

Part A: Algorithms and Complexity

1.0 Merge Sort


1.1 Introduction to Merge Sort

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

1.2Merge Sort Algorithm Steps

Figure 1 Examples of merge sort (javatpoint, 2022)

The merge sort algorithm's operating principle can be better understood by


considering an unsorted array. With the help of examples, merge sort can be
better understood. A sample array contains the following elements: 12, 31, 25, 8,
32, 17, 40, and 42. By applying a merge sort to the provided array, divide it into
two equal halves. To merge sort the list, it must be divided into equal portions
until it can no longer be divided. Dividing an array into two 4 by 8 arrays is the
first step in dividing it. In the next step, divide the array into halves again, and
divide them into new arrays of size 2 since they are of size 4. To obtain the
atomic value, divide arrays of size 2 again to obtain arrays of size 3 and so on.

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

1.3 Example of Code Snippets (Merge Sort)

Figure 2 Merge Sort Code Snippets (javatpoint, 2022)

6
Algorithms Assignment CT065-3-3-ALG

Figure 3 Merge sort example code snippets (javatpoint, 2022)

7
Algorithms Assignment CT065-3-3-ALG

2.0 Insertion Sort


2.1 Introduction to Insertion Sort

Insertion Sort is a sorting algorithm, which uses a sorted algorithm based on


comparison to set the final sorted array or list. It put the input element in its
appropriate position in each pass.
Insertion sort works like we sort cards in a card game. When we get an unsorted
deck of the cards, we select an unsorted card and compare it to the cards in hand. It
will be sorted on the right if it is greater, otherwise, to the left. After that, we sort
other cards using the same method until the cards are put in their right place. Insertion
sort same as this method.
Insertion sort is preferred to be used when the array has a small number of
elements and need to sort a few elements only. (Programiz, 2020) It is because
insertion sort can skip sorted values and become fast when some data has been or is
close to being sorted in the algorithm. (GeeksforGeeks, 2020)

2.2 Insertion Sort Algorithm Steps

We can use an example to understand how insertion sort algorithm work in an array
list.

8
Algorithms Assignment CT065-3-3-ALG

Figure 4 Insertion Sort Steps (Programiz, n.d.)

We assume that there is an example shows an array list which includes 9, 5, 1, 4, 3.


From the array list, we can see the first element, 9 in the array is assumed to be sorted.
So that, we take the second element 5, and store it separately in “key”. Compare
“key” to the first element, 9. We can see that the first element, 9 is greater than the
“key” value, 5. So that, they are swapped. The element, 5 become the first place and
the element, 9 become second place in the array.

Figure 5 Insertion sort step (Programiz, n.d.)

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

Figure 6 Insertion sort step (Programiz, n.d.)

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

Figure 7 Insertion sort step (Programiz, n.d.)

11
Algorithms Assignment CT065-3-3-ALG

Figure 8 Insertion sort step (Programiz, n.d.)

12
Algorithms Assignment CT065-3-3-ALG

2.3 Example of code snippet (Insertion Sort)

13
Algorithms Assignment CT065-3-3-ALG

(GeeksforGeeks, 2020)

14
Algorithms Assignment CT065-3-3-ALG

2.0 Time Complexity

2.1 Merge Sort time complexity

For Merge Sort, its time complexity is analyzed as follows:


Suppose there are n (n>1) elements in the sequence that need to be sorted.
Because T(n) = O(1) when n=1
When n > 1, the time T is decomposed as follows:
Decomposition steps:
 First, this step needs to find the middle position in the sequence, where the
time required is a constant O (1)
 The second step solves two series of size n/2, and the time required is 2T
(n/2).
Merge step:
 In the case of a sequence with n items, Merge Sort can be completed in O(n)
time
Adding all the steps of the above two steps yields the equation for the worst-case time
T required for Merge Sort to sort n elements.
T(n) = 2T(n/2) + O(n) n>1
=2(2T(n/4)+O(n/2))+O(n) x=2
=4T(n/4)+2O(n)
=4(2T(n/8) + O(n/4)) + 2O(n) x=3
=8T(n/8)+3O(n)
= ......

Derive the equation: T(n) = 2XT(n/2x) +xO(n)

Let n=2x,then x=nlogn

It follows that T(n) = nT(1) + nlogn

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.

2.2 Merge Sort effectiveness

When developing a program, recursive structures should be avoided because they


constantly need to rebuild the function's stack space. Merge Sort is very effective at
sorting because the best, worst, and average complexity are all O(nlogn), but it is not
appropriate for large sets because it needs double the memory to handle sorting, and
hardware resources will be taxed and the stack will overflow if there are too many
levels of recursion.

2.3 Insertion Sort

The time complexity of insertion sort is as follows.


The worst case: for insertion sort worst case is that inserted arrays are arranged
completely in reverse order:

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)

2.4 Insertion Sort effectiveness

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

You might also like