0% found this document useful (0 votes)
56 views6 pages

Daa 4th and 5th PRG

The document describes programs to analyze the time complexity of Quicksort and Mergesort sorting algorithms. It implements the algorithms in Java, runs them on input sizes over 5000, and records the time taken. It plots graphs of time vs size and demonstrates the divide-and-conquer approach for each along with analysis of worst, average, and best cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views6 pages

Daa 4th and 5th PRG

The document describes programs to analyze the time complexity of Quicksort and Mergesort sorting algorithms. It implements the algorithms in Java, runs them on input sizes over 5000, and records the time taken. It plots graphs of time vs size and demonstrates the divide-and-conquer approach for each along with analysis of worst, average, and best cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Program – 4

Sort a given set of n integer elements using Quick Sort method and compute its time
complexity. Run the program for varied values of n > 5000 and record the time taken to
sort. Plot a graph of the time taken versus n on graph sheet. The elements can be read from
a file or can be generated using the random number generator. Demonstrate using Java
how the divide-and-conquer method works along with its time complexity analysis: worst
case, average case and best case.

import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;
public class QuickSortComplexity
{
static final int MAX = 10005;
static int[] a = new int[MAX];
public static void main(String[ ] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter Max array size: ");
int n = input.nextInt();
Random random = new Random();
// System.out.println("Enter the array elements: ");
for (int i = 0; i < n; i++)
// a[i] = input.nextInt(); // for keyboard entry
a[i] = random.nextInt(1000); // generate
random numbers – uniform distribution
a = Arrays.copyOf(a, n); // keep only non zero elements
// QuickSortAlgorithm(0, n - 1);// for worst-case time complexity
// System.out.println("Input Array:");
// for (int i = 0; i < n; i++)
// System.out.print(a[i] + " ");
// set start time
long startTime = System.nanoTime( );
QuickSortAlgorithm(0, n - 1);
long stopTime = System.nanoTime( );
long elapsedTime = stopTime - startTime;
/* System.out.println("\nSorted Array:");
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
System.out.println( ); */
System.out.println("Time Complexity in ms for
n=" + n + " is: " + (double) elapsedTime / 1000000);
}
public static void QuickSortAlgorithm(int p, int r)
{
int i, j, temp, pivot;
if (p < r)
{
i = p;
j = r + 1;
pivot = a[p]; // mark first element as pivot
while (true)
{
i++;
while (a[i] < pivot && i < r)
i++;
j--;
while (a[j] > pivot)
j--;
if (i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
break; // partition is over
}
a[p] = a[j];
a[j] = pivot;
QuickSortAlgorithm(p, j - 1);
QuickSortAlgorithm(j + 1, r);
}
}
}

OUTPUT : Basic – QuickSort

Enter Max array size: 5


Enter the array elements:
42673
Input Array:
42673
Sorted Array:
23467
Time Complexity in ms for n=5 is: 0.009682.
OUTPUT : Best & Average Case – QuickSort

Enter Max array size: 5000


Time Complexity in ms for n=5000 is: 4.713954

Enter Max array size: 6000


Time Complexity in ms for n=6000 is: 7.386643

Enter Max array size: 7000


Time Complexity in ms for n=7000 is: 7.838167

Enter Max array size: 8000


Time Complexity in ms for n=8000 is: 8.819339

Enter Max array size: 9000


Time Complexity in ms for n=9000 is: 8.910286

Enter Max array size: 10000


Time Complexity in ms for n=10000 is: 11.698069

OUTPUT : Worst Case – QuickSort

Enter Max array size: 5000


Time Complexity in ms for n=5000 is: 36.561776

Enter Max array size: 6000


Time Complexity in ms for n=6000 is: 41.323717

Enter Max array size: 7000


Time Complexity in ms for n=7000 is: 55.416783

Enter Max array size: 8000


Time Complexity in ms for n=8000 is: 60.235093

Enter Max array size: 9000


Time Complexity in ms for n=9000 is: 70.381734

Enter Max array size: 10000


Time Complexity in ms for n=10000 is: 80.835902
Program – 5

Sort a given set of n integer elements using Merge Sort method and compute its time
complexity. Run the program for varied values of n > 5000, and record the time taken to
sort. Plot a graph of the time taken versus n on graph sheet. The elements can be read from
a file or can be generated using the random number generator. Demonstrate using Java
how the divide-and-conquer method works along with its time complexity analysis: worst
case, average case and best case.

import java.util.Random;
import java.util.Scanner;
public class MergeSort2
{
static final int MAX = 10005;
static int[] a = new int[MAX];
public static void main(String[ ] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter Max array size: ");
int n = input.nextInt( );
Random random = new Random( );
// System.out.println("Enter the array elements: ");
for (int i = 0; i < n; i++)
//a[i] = input.nextInt(); // for keyboard entry
a[i] = random.nextInt(1000); // generate random numbers –
// uniform distribution
// MergeSortAlgorithm(0, n - 1);
long startTime = System.nanoTime();
MergeSortAlgorithm(0, n - 1);
long stopTime = System.nanoTime();
long elapsedTime = stopTime - startTime;
System.out.println("Time Complexity (ms) for n = " +
n + " is : " + (double) elapsedTime / 1000000);
// System.out.println("Sorted Array (Merge Sort):");
// for (int i = 0; i < n; i++)
// System.out.print(a[i] + " ");
input.close();
}
public static void MergeSortAlgorithm(int low, int high)
{
int mid;
if (low < high)
{
mid = (low + high) / 2;
MergeSortAlgorithm(low, mid);
MergeSortAlgorithm(mid + 1, high);
Merge(low, mid, high);
}
}

public static void Merge(int low, int mid, int high)


{
int[ ] b = new int[MAX];
int i, h, j, k;
h = i = low;
j = mid + 1;
while ((h <= mid) && (j <= high))
if (a[h] < a[j])
b[i++] = a[h++];
else
b[i++] = a[j++];
if (h > mid)
for (k = j; k <= high; k++)
b[i++] = a[k];
else
for (k = h; k <= mid; k++)
b[i++] = a[k];
for (k = low; k <= high; k++)
a[k] = b[k];
}
}

OUTPUT : Basic - Merge Sort

Enter Max array size: 8


Enter the array elements:
30 25 15 50 28 49 88 33
Time Complexity (ms) for n = 8 is : 0.353131
Sorted Array (Merge Sort):
15 25 28 30 33 49 50 88

OUTPUT : Best & Averege Case - Merge Sort

Enter Max array size: 5000


Time Complexity (ms) for n = 5000 is : 48.192483

Enter Max array size: 6000


Time Complexity (ms) for n = 6000 is : 55.245321

Enter Max array size: 7000


Time Complexity (ms) for n = 7000 is : 63.571278
Enter Max array size: 8000
Time Complexity (ms) for n = 8000 is : 68.213534

Enter Max array size: 9000


Time Complexity (ms) for n = 9000 is : 76.808363

Enter Max array size: 10000


Time Complexity (ms) for n = 10000 is : 80.498525

OUTPUT : Worst Case - Merge Sort

Enter Max array size: 5000


Time Complexity (ms) for n = 5000 is : 65.638676

Enter Max array size: 6000


Time Complexity (ms) for n = 6000 is : 68.691028

Enter Max array size: 7000


Time Complexity (ms) for n = 7000 is : 76.250782

Enter Max array size: 8000


Time Complexity (ms) for n = 8000 is : 85.291611

Enter Max array size: 9000


Time Complexity (ms) for n = 9000 is : 108.015192

Enter Max array size: 10000


Time Complexity (ms) for n = 10000 is : 112.477093

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy