Algorithm_Lab_Report_Edited_Antor (1)
Algorithm_Lab_Report_Edited_Antor (1)
Section : 232-D5
Lab Report-3
Student Details :
Name ID
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:
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
package lab2;
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++;
}
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:
}
}
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.