Green University of Bangladesh
Department of Computer Science and Engineering(CSE)
Faculty of Sciences and Engineering
Sheak Al-Amin Antor 232002013
Semester: (Spring, Year:2025), B.Sc. in CSE (Day)
Course Title : Algorithms Lab
Course Code : CSE-208
Section : 232-D5
Lab Report-3
Student Details :
Name ID
Fahima Akter 232002222
Submission Date : 15-04-2025
Course Teachers Name : Md. Abu Rumman Refat
Lab Report Status
Marks: ……………………. Signature: …………………………..
Comments: …………….. Date: ………………………………….
Customized by S.A.A. Antor
PROBLEM-01: Draw the step-by-step solution of Merge Sort for the following array
20 7 15 9 35 4 1 11 7 16
And implement and obtain the output of Merge Sort for the same
array.
1. OBJECTIVES:
• Grasp the principles of the Merge Sort algorithm and its divide-and-conquer
approach.
• Illustrate the step-by-step process of dividing and merging arrays during sorting.
• Write and execute a Merge Sort function in a programming language.
• Sort a given array using Merge Sort and verify the output
2. PROCEDURE:
Algorithm 1 : Merge Sort Algorithm
Input: An array A, index lef t and right defining sorting range
Output: Sorted array A within index range lef t and right
1. IF left < right THEN
2. mid ← (left + right)/2
3. mergesort(A, left, mid)
4. mergesort(A, mid + 1, right)
5. merge(A, left, mid, right)
6.END
Algorithm 2 : Merge algorithm
Input: An array A, two sorted arrays denoted by index lef t to mid and mid to right
Output: Two sorted arrays merged into one sorted array A
1. n1 ← mid − left + 1
2. n2 ← right − mid
3. Create arrays L[1 ... n1 + 1] and R[1 ... n2 + 1]
4. FOR i ← 1 to n1 DO
5. L[i] ← A[left + i − 1]
6. END
7. FOR j ← 1 to n2 DO
8. R[j] ← A[mid + j]
9. END
10. L[n1 + 1] ← ∞
11. R[n2 + 1] ← ∞
12. i ← 1
13. j ← 1
14. FOR k ← left to right DO
15. IF L[i] ≤ R[j] THEN
16. A[k] ← L[i]
17. i ← i + 1
18. END
19. ELSE
20. A[k] ← R[j]
21. j←j+1
22. END
23. END
3 IMPLEMENTATION in JAVA
Code( Merge Sort) :
package lab2;
public class Margesort {
static void merge_sort(int arr[], int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
merge_sort(arr, left, mid);
merge_sort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
static void merge(int arr[], int left, int mid, int right) {
int n1 = (mid - left) + 1;
int n2 = (right - mid);
int leftArr[] = new int[n1];
int rightArr[] = new int[n2];
for (int i = 0; i < n1; i++) {
leftArr[i] = arr[left + i];
}
for (int j = 0; j < n2; j++) {
rightArr[j] = arr[mid + 1 + j];
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i = i + 1;
} else {
arr[k] = rightArr[j];
j = j + 1;
}
k++;
}
while (i < n1) {
arr[k] = leftArr[i];
i++;
k++;
}
while (j < n2) {
arr[k] = rightArr[j];
j++;
k++;
}
public static void main(String[] args) {
int arr[] = {20, 7, 15, 9, 35, 4, 1, 11, 7, 16};
int len = arr.length - 1;
merge_sort(arr, 0, len);
System.out.println("The sorting array:");
for (int i = 0; i <= len; i++) {
System.out.print(arr[i] + " ");
}
}
}
Output:
DISCUSSION : Merge Sort is a classic sorting algorithm that employs the divide-and-
conquer strategy to sort elements in an array or list. It is particularly known for its
efficiency and stability, making it a popular choice in various applications. Below is a
detailed discussion on Merge Sort, covering its principles, characteristics, advantages,
disadvantages, and use cases.
PROBLEM-02: Draw the step-by-step solution of Quick Sort for the following array
20 7 15 9 35 4 1 11 7 16
And implement and obtain the output of Quick Sort for the same
array.
1. OBJECTIVES:
• Performing a step-by-step sorting of the given array using Quick Sort.
• Implementing the Quick Sort algorithm in code.
• Obtaining the sorted output of the given array.
2. PROCEDURE:
Algorithm 1 : Merge Sort Algorithm
Input: An array A, index lo and hi defining range
Output: Sorted array A within index range lo and hi
1. if lo < hi then
2. p ← partition(A, lo, hi)
3. quicksort(A, lo, p − 1)
4. quicksort(A, p + 1, hi)
5. end
Algorithm 2: Partition Algorithm
Input: An array A, index lo and hi defining range
Output: A pivot p, with all low element on left and height element on right
1. pivot ← A[(hi + lo)/2]
2. i ← lo − 1
3. j ← hi + 1
4. for forever do
5. i ← i + 1
6. while A[i] < pivot do
7. j ← j − 1
8. end
9. while A[j] > pivot do
10. if i ≥ j then
11. return j
12. end
13. end
14. swap A[i] with A[j]
15. end
3 IMPLEMENTATION in JAVA
Code( Quick Sort) :
public class Quicksort {
static void quick_sort(int arr[], int left,int right){
if(left<right){
int pos = partition(arr,left,right);
quick_sort(arr,left,pos-1);
quick_sort(arr,pos+1,right);
}
}
static int partition(int arr[], int left, int right){
int pivot=arr[left];
int k=right;
for(int i=right;i>left;i--){
if(arr[i]>pivot){
int temp=arr[i];
arr[i]=arr[k];
arr[k]=temp;
k--;
}
}
arr[left]=arr[k];
arr[k]=pivot;
return k;
}
public static void main(String[] args) {
int arr[] = {20, 7, 15, 9, 35, 4, 1, 11, 7, 16};
int len=arr.length-1;
quick_sort(arr,0,len);
System.out.println("The sorting array:");
for(int i=0;i<=len;i++){
System.out.print(arr[i]+" ");
}
}
}
Output:
DISCUSSION : Quick Sort is a widely used sorting algorithm that employs the divide-
and-conquer strategy to efficiently sort elements in an array or list. It is known for its
speed and efficiency in practice, making it a popular choice for many applications.
Below is a detailed discussion on Quick Sort, covering its principles, characteristics,
advantages, disadvantages, and use cases.