CSE 205 Lab Manual 8 Merge sort
CSE 205 Lab Manual 8 Merge sort
Algorithms Lab
CSE 206
2 Problem analysis
A divide-and-conquer algorithm recursively breaks down a problem into two or more sub-problems of the same
or related type, until these become simple enough to be solved directly. The solutions to the sub-problems are
then combined to give a solution to the original problem. This technique can be divided into the following three
parts:
Merge Sort is a Divide and Conquer algorithm. It divides the input array into two halves, calls itself for
the two two sub-arrays, and then these sub-arrays into even smaller sub-arrays, until multiple sub-arrays with
single element in them are obtained. Now, the idea here is that an array with a single element is already sorted.
Finally, merge all these sorted sub-arrays, step by step to form one single sorted array.
4 Implementation in Java
1 package mergesort;
2
3 public class MergeSort {
4 void merge(int arr[], int left, int mid, int right) {
5 int l = mid − left + 1;
6 int r = right − mid;
7
8 int leftArray[] = new int[l]; // New array for Left elements
9 int rightArray[] = new int[r]; // New array for right elements
10
11 for (int i = 0; i < l; ++i) { // Copying elements in leftArray
12 leftArray[i] = arr[left + i];
13 }
14
15 for (int j = 0; j < r; ++j) { // Copying elements in leftArray
16 rightArray[j] = arr[mid + 1 + j];
17 }
18
19 int i = 0, j = 0;
20 int k = left;
21 while (i < l && j < r) { // Copying the smaller element in
array
22 if (leftArray[i] <= rightArray[j]) {
23 arr[k] = leftArray[i];
24 i++;
25 } else {
26 arr[k] = rightArray[j];
27 j++;
28 }
29 k++;
30 }
31 while (i < l) { // Copying any remainnig left
element in array
32 arr[k] = leftArray[i];
33 i++;
34 k++;
35 }
36
37 while (j < r) { // Copying any remainnig right
element in array
38 arr[k] = rightArray[j];
39 j++;
40 k++;
41 }
42 }
43
44 void sort(int arr[], int left, int right) {
45 if (left < right) { // Continue recursion if this
condition satisfies
46 int mid = (left + right) / 2; // Index middle elemnt of array
47 sort(arr, left, mid); // Recursive call left
48 sort(arr, mid + 1, right); // Recursive call right
49 merge(arr, left, mid, right); // Mergin the left and right array
50 }
51 }
52
53 public static void main(String args[]) {
54 int arr[] = {90, 23, 101, 45, 65, 23, 67, 89, 34, 23};
55 MergeSort ob = new MergeSort(); // Creating Object of MergeSort
56 ob.sort(arr, 0, arr.length − 1); // Calling the method
57
58 // Printing the output
59 System.out.println("Sorted array");
60 for (int i = 0; i < arr.length; i++) {
61 System.out.println(arr[i] + "");
62 }
63 }
64 }
5 Sample Input/Output (Compilation, Debugging & Testing)
Output:
Sorted array
23
23
23
34
45
65
67
89
90
101
7 Lab Task (Please implement yourself and show the output to the
instructor)
1. Draw the step-by-step solution of Merge Sort for the following array.
20 7 15 9 35 4 1 11 7 16
2. Implement and obtain the output of Merge Sort for the same array.
3. Execute the merge algorithm with the following two arrays and justify the output obtained in each case.
2 6 8 9 1 5 9 11
8 6 2 9 1 5 11 9
9 Policy
Copying from internet, classmate, seniors, or from any other source is strongly prohibited. 100% marks will be
deducted if any such copying is detected.