Midterm gr12
Midterm gr12
International School
Group: 12
Class: INS305003
Subjects: Data Structure & Algorithm with C
Members: Le Thi Bich Ngoc - 22070439
Tran Van Anh - 22070395
Do Huyen Trang - 22070377
Vu Thi Tu Chau - 22070310
Lecture: PhD. Diem Cong Hoang
Topic: Merge sort
1
Thanks
We would like to express my gratitude to lecturer Diem Cong Hoang for imparting
to we the necessary knowledge and skills to complete the midterm report.
Although our group tried very hard, due to our limited professional qualifications,
we encountered many difficulties during the research process and inevitably made
mistakes. Therefore, we look forward to receiving comments and feedback from you
to make our report more complete.
Contents
2
Vietnam National University, Ha Noi........................................................................................1
I. Algorithms introduction......................................................................................................4
1. Definition................................................................................................................................4
2. Applications of Merge sort.....................................................................................................4
II. How Merge Sort algorithm works......................................................................................5
1. The basic steps of Merge Sort.........................................................................................5
2. Example...........................................................................................................................5
III. Code explaination.................................................................................................................7
IV. Algorithm complexity analysis..........................................................................................14
1. Time Complexity Analysis............................................................................................14
2. Space Complexity..........................................................................................................15
V. Advantages and disadvantages of algorithms....................................................................15
1. Advantages...........................................................................................................................15
2. Disadvantages.......................................................................................................................15
VI. Conclusion...........................................................................................................................16
VII. References .........................................................................................................................16
Contribution ..........................................................................................................................17
I. Algorithms introduction
3
1. Definition
Merge sort is a sorting algorithm that follows the divide-and-conquer approach. It
works by recursively dividing the input array into smaller subarrays, sorting those
subarrays, and merging them back together to obtain the sorted array.
- External sorting (when the dataset is too large to fit in memory) is a term for a class of
sorting algorithms that can handle massive amounts of data. It is required when the data
being sorted does not fit into the main memory of a computing device (usually RAM)
and instead must reside in the slower external memory (usually a hard drive).
- Inversion counting: Given an integer array arr[] of size n, find the inversion count in the
array.
Two array elements: arr[i] > arr[j] and i < j.
- Merge Sort and its variations are used in library methods of programming languages. For
example, its variation TimSort is used in Python, Java Android, and Swift. The main
reason it is preferred to sort non-primitive types is stability which is not there in
QuickSort.
- It can be easily parallelized as we can independently sort subarrays and merge them.
4
- The merge function of merge sort efficiently solves the problems like union and
intersection of two sorted arrays: A Union of two arrays is an array having all distinct
elements that are present in either array whereas an Intersection of two arrays is an array
containing distinct common elements between the two arrays
- Conquer: Each subarray is sorted individually using the merge sort algorithm.
- Merge: The sorted subarrays are merged back together in sorted order. The process
continues until all elements from both subarrays have been merged.
2. Example
Let’s sort the array or list [38, 27, 43, 3] using Merge Sort
5
6
III. Code explaination
Merge Function
int i, j, k;
int n1 = middle - left + 1;
int n2 = right - middle;
int L[n1], R[n2];
i, j, k: Index valuables used to traverse the arrays.
n1, n2: The sizes of the two halves of the array (left half and right half).
L[], R[]: Temporary arrays to store the elements of the left and right halves.
n1 = middle - left + 1: When splitting the array, n1 is calculated as middle - left + 1
(adding 1 to include the element at the middle index).
Two for loops copy elements from the main array arr[] into the temporary arrays L[] and
R[].
L[] contains elements from arr[left] to arr[middle].
R[] contains elements from arr[middle + 1] to arr[right].
For loop:
o i = 0: Start the loop with i equal to 0.
o i < n1: Continue the loop while i is less than n1 (size of the left half).
o i++: Increment i by 1 after each iteration.
Copy elements:
L[i] = arr[left + i]: Copy the element from arr[] to the temporary array
L[].
arr[left + i]: Access the element at index left + i in arr[].
L[i]: Assign the value of arr[left + i] to L[i].
Example:
7
Assume left = 0, middle = 2, and the array arr is [10, 20, 30, 40, 50].
n1 = middle - left + 1 = 3.
o When i = 0, L[0] = arr[0] = 10.
o When i = 1, L[1] = arr[1] = 20.
o When i = 2, L[2] = arr[2] = 30.
Result: L[] = [10, 20, 30].
For loop:
o j = 0: Start the loop with j equal to 0.
o j < n2: Continue the loop while j is less than n2 (size of the right half).
o j++: Increment j by 1 after each iteration.
Copy elements:
R[j] = arr[middle + 1 + j]: Copy the element from arr[] to the temporary array
R[].
arr[middle + 1 + j]: Access the element at index middle + 1 + j in
arr[].
R[j]: Assign the value of arr[middle + 1 + j] to R[j].
Example:
Assume middle = 2, right = 4, and the array arr is [10, 20, 30, 40, 50].
n2 = right - middle = 2.
o When j = 0, R[0] = arr[3] = 40.
o When j = 1, R[1] = arr[4] = 50.
Result: R[] = [40, 50].
8
k++;
}
while (i < n1 && j < n2): This loop continues as long as both conditions i < n1 and j <
n2 are true.
o i < n1: Ensures that i is within the bounds of L[].
o j < n2: Ensures that j is within the bounds of R[].
Detailed explanation:
Purpose: This loop copies all remaining elements of the temporary array L[] into the
main array arr[] after merging the elements from both temporary arrays.
Condition: while (i < n1): The loop continues as long as i is less than n1 (number of
elements in L[]).
o arr[k] = L[i]: Assign the value of the element at L[i] to arr[k].
o i++: Increment i to move to the next element of L[].
o k++: Increment k to move to the next position in the main array arr[].
Usage scenario:
If R[] is exhausted, but L[] still has remaining elements, this loop will copy all
remaining elements from L[] to arr[].
9
Copy remaining elements of R[] if any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
Purpose:
This loop copies all remaining elements from the temporary array R[] to the main array
arr[] after merging elements from both temporary arrays.
Condition: while (j < n2): The loop continues running as long as j is less than n2 (the
number of elements in R[]).
arr[k] = R[j];: Assigns the value of the element at R[j] to arr[k].
j++;: Increments the index j by 1 to move to the next element in R[].
k++;: Increments the index k by 1 to move to the next position in the main array
arr[].
Use Case:
If the L[] array has been exhausted but R[] still has remaining elements, this loop will
copy all the remaining elements from R[] into arr[].
Explanation: Call the mergeSort function to sort the first half of the array, from
index left to middle.
Explanation: Call the mergeSort function to sort the second half of the array, from
index middle + 1 to right.
Explanation: Call the merge function to merge the two sorted halves together.
After recursively sorting the two halves, the merge function combines them into one
sorted array.
Explanation:
o void printArray(int arr[], int size): Defines the printArray function with two
parameters:
int arr[]: The array of integers to print.
int size: The size of the array (number of elements in the array).
o int i;: Declares the variable i to be used as an index in the for loop.
Explanation:
o for (i = 0; i < size; i++): The for loop runs from i = 0 to i < size.
i = 0: Initializes the index i to 0.
i < size: The loop continues as long as i is less than size.
i++: Increments the index i by 1 after each iteration.
o printf("%d ", arr[i]);: Prints the element at index i of the array arr to the screen.
%d: Format specifier to print an integer.
arr[i]: The element at position i in the array arr.
" ": Adds a space after each integer to separate the elements.
11
Print Newline After All Elements:
printf("\n");
}
Explanation:
o int n;: Declares the variable n to store the number of elements in the array.
o printf("Enter the number of elements in the array: ");: Prints a prompt to the
screen asking the user to enter the number of elements.
o scanf("%d", &n);: Reads the value entered by the user and assigns it to the
variable n.
Explanation:
o int arr[n];: Declares the array arr with a size of n.
o printf("Enter the elements of the array:\n");: Prints a prompt to the screen asking
the user to enter the elements of the array.
o int i;: Declares the variable i to be used in the for loop.
12
o for (i = 0; i < n; i++) { scanf("%d", &arr[i]); }: The for loop runs from i = 0 to i <
n. In each iteration, it reads the value entered by the user and assigns it to arr[i].
Explanation:
o printf("Initial array: \n");: Prints the string "Initial array:" to the screen.
o printArray(arr, n);: Calls the printArray function to print the elements of the
initial array.
Explanation:
o mergeSort(arr, 0, n - 1);: Calls the mergeSort function to sort the array arr from
index 0 to n - 1.
Explanation:
o printf("Sorted array: \n");: Prints the string "Sorted array:" to the screen.
o printArray(arr, n);: Calls the printArray function to print the elements of the
sorted array.
Explanation:
o return 0;: Returns the value 0 to indicate that the program has ended successfully.
13
The result of the code when displayed on the screen with a custom array.
To analyze the time complexity of Merge Sort, let's consider the mergeSort function:
14
Merge:
o After the array is split into individual elements, the merge steps (merge function)
will combine each pair of sorted halves to create a larger sorted array.
o At each level of the recursion tree, merging the two sorted halves has a complexity
of O(n) (since we have to go through all the elements of the array at each level to
sort them back).
o Therefore, the total time to merge through all levels is:
)
2. Space Complexity
- Additional Space: The merge function in this code uses temporary arrays L[ ] and R[ ]
to store the split halves. Therefore, the algorithm requires additional memory
proportional to the size of the O(n) array to store the temporary elements during the
merge.
- The total space complexity is O(n).
V. Advantages and disadvantages of algorithms
1. Advantages
- Stability: Merge sort is a stable sorting algorithm, which means it maintains the relative
order of equal elements in the input array.
- Worst-case performance guarantee: Merge sort has a worst-case time complexity of O(N
logN), which means it performs well even on large datasets.
- Ease of implementation: The divide-and-conquer approach is very simple.
- Natural parallelism: We merge sub-arrays independently, which makes this algorithm
suitable for parallel processing.
15
2. Disadvantages
- Space complexity: Merge sort requires additional memory to store the merged sub-
arrays during the sorting process.
- Not in-place: Merge sort is not an in-place sorting algorithm, which means it requires
additional memory to store the sorted data. This can be a disadvantage in applications
where memory usage is a concern.
- It's slower than QuickSort in general. QuickSort is more cache-friendly because it works
in place.
VI. Conclusion
In summary, Merge Sort is an efficient, reliable sorting algorithm that uses a divide-and-
conquer approach to break down and merge arrays, achieving consistent O(n log n)
performance across all cases. While it requires extra space, its predictable efficiency
makes it ideal for large datasets and offers key insights into recursion and algorithmic
design.
VII. References
1. Merge Sort – Data Structure and Algorithms Tutorials, Updated : 17 Sep, 2024,
Geeksforgeeks.org, Available at:https://www.geeksforgeeks.org/merge-sort/
2. DSA Merge Sort Time Complexity, Copyright 1999-2024 by Refsnes Data, Available
at: https://www.w3schools.com/dsa/dsa_timecomplexity_mergesort.php
3. DSA Merge Sort, by w3schools.com , Available
at:https://www.w3schools.com/dsa/dsa_algo_mergesort.php
4. What is the Time Complexity of Merge Sort Algorithm?, 12th June, 2024, by Arunav
Goswami, Available at: https://www.almabetter.com/bytes/articles/merge-sort-
time-complexity
5. Merge Sort Algorithm - Java, C, and Python Implementation, Published on August 4,
2022, digitalocean.com, Available at:
https://www.digitalocean.com/community/tutorials/merge-sort-algorithm-java-c-
python
6. Merge Sort Explained: A Data Scientist’s Algorithm Guide, Mar 31, 2022, By
Richmond Alake, Available at: Merge Sort Explained: A Data Scientist’s Algorithm
Guide | NVIDIA Technical Blog
16
Contribution
Members Student’s ID Content Contribution
percentage
Tran Van Anh 22070395 Algorithm 25%
complexity analysis,
Edit slide
Do Huyen Trang 22070377 How Merge Sort 25%
algorithm works,
Conclusion, Edit
report
Le Thi Bich Ngoc 22070439 Run code, Code 25%
explanation, Edit
report
Vu Thi Tu Chau 22070310 Algorithms 25%
introduction,
Advantages and
disadvantages of
algorithms, Edit
slide
17