Future Vision BIE Future Vision BIE


ONE STOP FOR ALL STUDY MATERIALS & LAB PROGRAMS


E MENU Whatsapp Share Join Telegram, to get Instant Updates
× NOTE! Click on MENU to Browse between Subjects...

Advertisement

DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

(Effective from the academic year 2018 -2019)

SEMESTER - IV

Course Code 18CSL47

CIE Marks 40

Number of Contact Hours/Week 0:2:2

SEE Marks 60

Total Number of Lab Contact Hours 36

Exam Hours 03



Advertisement

Experiments 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 non 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.



Advertisement

Advertisement

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package four;
import java.util.Random;
import java.util.Scanner;
public class QuickSort {
	public static void sort(int[] a)
	{
		quicksort(a,0,a.length-1);
	}
	public static void quicksort(int[] a,int low,int high)
	{
		int i=low,j=high,temp,pivot=a[low];
		while(i<=j)
		{
			while(a[i]<pivot)
				i++;
			while(a[j]>pivot)
				j--;
			if(i<=j)
			{
				temp=a[i];
				a[i]=a[j];
				a[j]=temp;
				i++;
				j--;
			}
		}
		if(j>low)
			quicksort(a,low,j);
		if(i<high)
			quicksort(a,i,high);
	}
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int i;
		Random r = new Random();
		System.out.println("Quick Sort\nEnter the Number of times the algorithm should Run");
		int times = scan.nextInt();
		double totaldur=0;
		for(int j=0;j<times;j++)
		{
			System.out.println("Random Number Generated are at POS  "+j+"  as follows : ");
			int[] a = new int[10];
			for(i=0;i<10;i++)
			{
				a[i]=r.nextInt(1000);
				System.out.print(a[i]+" ");
			}
			System.out.println("");
			long StartTime = System.nanoTime();
			sort(a);
			double EndTime = System.nanoTime();
			double duration = (EndTime - StartTime);
			System.out.println("Elements after Sorting are");
			for(i=0;i<10;i++)
				System.out.print(a[i]+" ");
			System.out.println("");
			totaldur=totaldur+duration;
		}
		System.out.println("\nTotal time taken to Sort is :"+totaldur+" Nano Seconds");
		double miliseconds = (totaldur / 1000000);
		System.out.println("\nTotal time taken to Sort is :"+miliseconds+" Mili Seconds");
		double avg=totaldur/times;
		System.out.println("The Average time Spend by the System is : "+avg+" Nano Second");
		double miliavg=(avg/1000000);
		System.out.println("The Avergae time Spend by the System is : "+miliavg+" Mili Seconds");
		scan.close();
	}
}

Advertisement

output
Fig 4: Output .

× Note Please Share the website link with Your Friends and known Students...

-ADMIN

× Note Page Number is specified to navigate between Pages...
T = Text book
QB = Question Bank
AS = Amswer Script


-ADMIN

Advertisement