Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Subject :Design and Analysis of Algorithms Subject Code : BTCO13403

Name : Harsh Boghani Enrollment No: ET22BTCO009

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;

public class Prog5_A {


// best 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];

Random rd = new Random();


for (int i = 0; i < n; i++) {
arr[i] = rd.nextInt();
}

Arrays.sort(arr);

for (int i = 0; i < n; i++) {


arr1[i] = arr[i];
arr2[i] = arr[i];
// arr3[i] = arr[i];
}

long startTime1 = System.nanoTime();


mergeSort(arr1,0,n-1);
long stopTime1 = System.nanoTime();

long startTime2 = System.nanoTime();


quickSort(arr2,0,n-1);

Page No: 1
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009

long stopTime2 = System.nanoTime();

// long startTime3 = System.nanoTime();


// insertionSort(arr3);
// long stopTime3 = System.nanoTime();

System.out.println("The Best time for merge sort is : " + (stopTime1 - startTime1));


System.out.println("The Best time for quick sort is : " + (stopTime2 - startTime2));
// System.out.println("The Best time for insertion sort is : " + (stopTime3 - startTime3));

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

public static void mergeSort(int[] a, int low, int high) {


if (low >= high) {
return;
}
int mid = (low + high) / 2;
mergeSort(a, low, mid);
mergeSort(a, mid + 1, high);
merge(a, low, mid, high);
}

public static int function(int[] a, int low, int high) {


int pivot = low;
int i = low;
int j = high;
while (i < j) {
while (a[i] <= a[pivot] && i < high) {
i++;
}
while (a[j] > a[pivot] && j >= low) {
j--;
}
if (i < j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
int c = a[j];
a[j] = a[pivot];
a[pivot] = c;
return j;
}

public static void quickSort(int[] a, int low, int high) {


if (low < high) {
int partitian = function(a, low, high);
quickSort(a, low, partitian - 1);
quickSort(a, partitian + 1, high);
}
}

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];

Random rd = new Random();


for (int i = 0; i < n; i++) {
arr[i] = rd.nextInt();
}

for (int i = 0; i < (n) / 2; i++) {


int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;

Page No: 4
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009

for (int i = 0; i < n; i++) {


arr1[i] = arr[i];
arr2[i] = arr[i];
// arr3[i] = arr[i];
}

long startTime1 = System.nanoTime();


mergeSort(arr1, 0, n - 1);
long stopTime1 = System.nanoTime();

long startTime2 = System.nanoTime();


quickSort(arr2, 0, n - 1);
long stopTime2 = System.nanoTime();

// long startTime3 = System.nanoTime();


// insertionSort(arr3);
// long stopTime3 = System.nanoTime();

System.out.println("The Worst time for merge sort is : " + (stopTime1 - startTime1));


System.out.println("The Worst time for quick sort is : " + (stopTime2 - startTime2));
// System.out.println("The Worst time for insertion sort is : " + (stopTime3 -
// startTime3));

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];
}

public static void mergeSort(int[] a, int low, int high) {


if (low >= high) {
return;
}
int mid = (low + high) / 2;
mergeSort(a, low, mid);
mergeSort(a, mid + 1, high);
merge(a, low, mid, high);
}

public static int function(int[] a, int low, int high) {


int pivot = low;
int i = low;
int j = high;
while (i < j) {
while (a[i] <= a[pivot] && i < high) {
i++;
}
while (a[j] > a[pivot] && j >= low) {
j--;
}
if (i < j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
int c = a[j];
a[j] = a[pivot];

Page No: 6
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009

a[pivot] = c;
return j;
}

public static void quickSort(int[] a, int low, int high) {


if (low < high) {
int partitian = function(a, low, high);
quickSort(a, low, partitian - 1);
quickSort(a, partitian + 1, high);
}
}

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;

public class Prog5_C {


// Average 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];

Random rd = new Random();


for (int i = 0; i < n; i++) {
arr[i] = rd.nextInt();
}

for (int i = 0; i < n; i++) {


arr1[i] = arr[i];
arr2[i] = arr[i];
// arr3[i] = arr[i];
}

long startTime1 = System.nanoTime();


mergeSort(arr1, 0, n - 1);
long stopTime1 = System.nanoTime();

long startTime2 = System.nanoTime();


quickSort(arr2, 0, n - 1);
long stopTime2 = System.nanoTime();

// long startTime3 = System.nanoTime();


// insertionSort(arr3);
// long stopTime3 = System.nanoTime();

System.out.println("The Average time for merge sort is : " + (stopTime1 - startTime1));


System.out.println("The Average time for quick sort is : " + (stopTime2 - startTime2));
// System.out.println("The Averagetime for insertion sort is : " + (stopTime3 -
// startTime3));

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];
}

public static void mergeSort(int[] a, int low, int high) {


if (low >= high) {
return;
}
int mid = (low + high) / 2;
mergeSort(a, low, mid);
mergeSort(a, mid + 1, high);
merge(a, low, mid, high);
}

Page No: 9
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009

public static int function(int[] a, int low, int high) {


int pivot = low;
int i = low;
int j = high;
while (i < j) {
while (a[i] <= a[pivot] && i < high) {
i++;
}
while (a[j] > a[pivot] && j >= low) {
j--;
}
if (i < j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
int c = a[j];
a[j] = a[pivot];
a[pivot] = c;
return j;
}

public static void quickSort(int[] a, int low, int high) {


if (low < high) {
int partitian = function(a, low, high);
quickSort(a, low, partitian - 1);
quickSort(a, partitian + 1, high);
}
}

Page No: 10
Subject :Design and Analysis of Algorithms Subject Code : BTCO13403
Name : Harsh Boghani Enrollment No: ET22BTCO009

Output:

Observation:

n(number of Merge Sort (ns) Quick Sort (ns)


inputs

Best 1000 36510 58004

Case 10000 391301 1883501

------------------------ 100000 StackOverFlow StackOverFlow

Average 1000 382425 264224

Case 10000 1635316 1457556

------------------------ 100000 13761801 13593010

Worst 1000 386556 270115

Case 10000 1730278 1594000

------------------------ 100000 15376469 40559810

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

You might also like