Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Daa 4th and 5th PRG

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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