Professional Documents
Culture Documents
Prog - 5
Prog - 5
Practical : 5
Problem Statement:
Implementation and Time analysis of sorting algorithm.
1.Best Case:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
Arrays.sort(arr);
Page No: 1
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009
public static void merge(int[] a, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int left = low;
int right = mid + 1;
int k = 0;
while (left <= mid && right <= high) {
if (a[left] <= a[right]) {
temp[k] = a[left];
left++;
k++;
} else {
temp[k] = a[right];
right++;
k++;
}
}
while (left <= mid) {
temp[k] = a[left];
left++;
k++;
}
while (right <= high) {
temp[k] = a[right];
right++;
k++;
}
for (int i = low; i <= high; i++) {
a[i] = temp[i - low];
}
Page No: 2
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009
Page No: 3
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009
Output:
2.Worst Case:
public class Prog5_B {
// worst case:
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the size of the array");
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
int[] arr1 = new int[n];
int[] arr2 = new int[n];
// int[] arr3 = new int[n];
Page No: 4
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009
public static void merge(int[] a, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int left = low;
int right = mid + 1;
int k = 0;
while (left <= mid && right <= high) {
if (a[left] <= a[right]) {
temp[k] = a[left];
left++;
k++;
} else {
temp[k] = a[right];
right++;
k++;
}
}
while (left <= mid) {
Page No: 5
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009
temp[k] = a[left];
left++;
k++;
}
while (right <= high) {
temp[k] = a[right];
right++;
k++;
}
for (int i = low; i <= high; i++) {
a[i] = temp[i - low];
}
Page No: 6
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009
a[pivot] = c;
return j;
}
Output:
Page No: 7
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009
3.Average Case:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
Page No: 8
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009
public static void merge(int[] a, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int left = low;
int right = mid + 1;
int k = 0;
while (left <= mid && right <= high) {
if (a[left] <= a[right]) {
temp[k] = a[left];
left++;
k++;
} else {
temp[k] = a[right];
right++;
k++;
}
}
while (left <= mid) {
temp[k] = a[left];
left++;
k++;
}
while (right <= high) {
temp[k] = a[right];
right++;
k++;
}
for (int i = low; i <= high; i++) {
a[i] = temp[i - low];
}
Page No: 9
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009
Page No: 10
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009
Output:
Observation:
Page No: 11
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009
Conclusion:
The experimental comparison between Merge sort and Quick sort indicates that the
Merge Sort method of sorting takes less time at lower input size whereas Quick Sort
method of sorting takes less time at higher input size. This Uniformity is followed in all
the three cases.
Page No: 12