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



Experiments 1A

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 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
package five;
import java.util.Scanner;
import java.util.Random;
public class MergeSortExp {
	public static void mergeSort(int[] a,int low,int high)
	{
		int N=high-low;
		if(N<=1)
			return;
		int mid=low+(N/2);
		mergeSort(a,low,mid);
		mergeSort(a,mid,high);
		int[] temp=new int[N];
		int i=low, j=mid;
		for(int k=0;k<N;k++)
		{
			if(i==mid)
				temp[k]=a[j++];
			else if(j==high)
				temp[k]=a[i++];
			else if(a[j]<a[i])
				temp[k]=a[j++];
			else
				temp[k]=a[i++];
		}
		for(int k=0;k<N;k++)
			a[low++]=temp[k];
	}
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int i;
		Random r = new Random();
		System.out.println("Enter 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();
			mergeSort(a,0,10);
			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 5: 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