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

Daa Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

QUICK SORT

package sanj;
import java.util.Random;
import java.util.Scanner;
public class P1 {
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] = random.nextInt(1000);
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
long startTime = System.nanoTime();
QuickSortAlgorithm(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 (Quick Sort):");
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
input.close();
}
public static void QuickSortAlgorithm(int p, int r)
{
int i, j, temp, pivot;
if (p < r)
{
i = p;
j = r;
pivot = a[p];
while(true)
{
i++;
while (a[i] < pivot&&i<r)
{
i++;
}
while(a[j]> pivot)
{
j--;
}
if(i<j)
{
temp=a[i];
a[i]=a[j];a[j]=temp;
}
else
{
break;
}
}
a[p]=a[j];
a[j]=pivot;
QuickSortAlgorithm(p,j-1);
QuickSortAlgorithm(j+1,r);
}
}

}
MERGE SORT
package sanj;
import java.util.Random;
import java.util.Scanner;
public class P2 {
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] = random.nextInt(1000);
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
long startTime = System.nanoTime();
MergeSortAlgo(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 MergeSortAlgo(int low, int high)
{
int mid;
if (low < high)
{
mid = (low + high) / 2;
MergeSortAlgo(low, mid);
MergeSortAlgo(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];
}
}
FLOYD
package sanj; scanner.close();
import java.util.Scanner; }
public class P3 { public static void getPath()
static int a[][]; {
static int n; for (int k = 0; k < n; k++)
public static void main(String {
args[]) for (int i = 0; i < n; i++)
{ for (int j = 0; j < n; j++)
System.out.println("Enter the if ((a[i][k] + a[k][j]) < a[i][j])
number of vertices\n"); a[i][j] = a[i][k] + a[k][j];
Scanner scanner = new }
Scanner(System.in); }
n = scanner.nextInt(); public static void PrintMatrix()
a = new int[n][n]; {
System.out.println("Enter the Cost System.out.println("The All Pair
Matrix (999 for infinity) \n"); Shortest Path Matrix is:\n");
for (int i = 0; i < n; i++) for(int i=0; i<n; i++)
{ {
for (int j = 0; j < n; j++) for(int j=0; j<n; j++)
{ System.out.print(a[i][j] +" ");
a[i][j] = scanner.nextInt(); System.out.println();
} }
} }
getPath(); }
PrintMatrix();
WARSHALL
package sanj; }
import java.util.Scanner; public static void getClosure()
public class P4 { {
static int a[][]; for (int k = 0; k < n; k++)
static int n; {
public static void main(String for (int i = 0; i < n; i++)
args[]) for (int j = 0; j < n; j++)
{ if(a[i][j]==1 || (a[i][k]==1 &&
System.out.println("Enter the a[k][j]==1))
number of vertices\n"); a[i][j]=1;
Scanner scanner = new }
Scanner(System.in); }
n = scanner.nextInt(); public static void PrintMatrix()
a = new int[n][n]; {
System.out.println("Enter the Cost System.out.println("Transitive
Matrix (0's and 1's) \n"); Closure:\n");
for (int i = 0; i < n; i++) for(int i=0; i<n; i++)
{ {
for (int j = 0; j < n; j++) for(int j=0; j<n; j++)
{ System.out.print(a[i][j] +" ");
a[i][j] = scanner.nextInt(); System.out.println();
} }
} }
getClosure(); }
PrintMatrix();
scanner.close();

You might also like