You are on page 1of 14

/* 1.

java program implement Quicksort


expected output
Enter number of integer elements
5
Enter 5 integer elements
30
20
10
40
70
Sorted array
10 20 30 40 70
*/
import java.util.*;
class Qsort
{

void interchange (int a[], int i , int j)


{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}//end swap

void quicksort( int a[], int p, int q )


{
int j;

if ( p<q )
{
j = partition( a, p, q );
// ...Student-to-write-code-here... to sort i-part of
pivot item (use recursion)
quicksort( a, p, j-1 );
// ...Student-to-write-code-here... to sort j-part of
pivot item (use recursion)
quicksort( a, j+1, q );
}
} //end quicksort

int partition( int a[], int m, int p )


{
int i, j;
int v;
v = i = m;
v = a[m];
j = p;
while ( i < j )
{
while( a[i] >= v )
{i++;
}

while( a[j] <= v)


{j--;}
// Move j_index backward while item > pivot

if ( i< j)
{ interchange(a,i,j); }
}
// j is final position for the pivot
a[m] = a[j];
a[j] =v ;

return j;
}
void printArray(int a[], int n)
{
int i;
for (i=0; i<n; i++)
System.out.print(" "+a[i]);

}//end printarray
}
public class Quick_sort
{

public static void main(String args[])


{

Scanner scan = new Scanner( System.in );

int n, i;
/* Accept number of elements */
System.out.println("Enter number of integer elements");
n = scan.nextInt();
/* Create array of n elements */
int arr[] = new int[n];
/* Accept elements */
System.out.println("\nEnter "+ n +" integer elements");
for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
/* Call method sort */
Qsort ob = new Qsort();
ob.quicksort(arr, 0, arr.length-1);
System.out.println("\nSorted array");
ob.printArray(arr,n);

/*2.sort given array elements using mergesort


expected output
Merge Sort Test
Enter number of integer elements
7
Enter 7 integer elements
30
10
20
50
60
70
80
Sorted array
10 20 30 50 60 70 80*/

import java.util.*;

public class Mergesort

void mergeSort(int[] array, int low, int high){

if(low < high){

int middle = (low + high) / 2;

mergeSort(array, low, middle);


mergeSort(array, middle+1, high);

merge(array, low, middle, high);

void merge(int[] array, int low, int middle, int high){

int[] helper = new int[array.length];

for (int i = low; i <= high; i++) {

helper[i] = array[i];

int helperLeft = low;

int helperRight = middle+1;

int current = low;

while (helperLeft <= middle && helperRight <=high) {

if(helper[helperLeft] <= helper[helperRight]){

array[current] = helper[helperLeft];

helperLeft++;

}else{

array[current] = helper[helperRight];

helperRight++;
}

current ++;

int remaining = middle - helperLeft;

for (int i = 0; i <= remaining; i++) {

array[current+i] = helper[helperLeft+ i];

public void printArray(int arr[])

for(int i=0;i<arr.length;i++)

System.out.print(arr[i]+" ");

System.out.println();

public static void main(String args[])

Scanner sc=new Scanner(System.in);

Mergesort ob=new Mergesort();

System.out.println("enter the size of an array");

int n=sc.nextInt();

int arr[]=new int[n];

System.out.println("enter the elements of an array");


for(int i=0;i<n;i++)

arr[i]=sc.nextInt();

ob.mergeSort(arr,0,n-1);

System.out.println("after sorting array elements are");

ob.printArray(arr);

BFS program

import java.util.InputMismatchException;

import java.util.LinkedList;

import java.util.Queue;

import java.util.Scanner;

class BFS

private Queue<Integer> queue;

public BFS()

queue = new LinkedList<Integer>();

}
public void bfs(int adjacency_matrix[][], int source)

int no_of_nodes = adjacency_matrix[source].length-1;

int[] visited = new int[no_of_nodes + 1];

int i,element;

visited[source] = 1;

queue.add(source);

while (!queue.isEmpty())

element = queue.remove();

i = element;

System.out.print(i + "\t");

while (i <= no_of_nodes)

if (adjacency_matrix[element][i] == 1 && visited[i] == 0)

queue.add(i);

visited[i] = 1;
}

i++;

DFS program

import java.util.InputMismatchException;

import java.util.Scanner;

import java.util.Stack;

public class DFS

private Stack<Integer> stack;

public DFS()

stack = new Stack<Integer>();

public void dfs(int adjacency_matrix[][], int source)

int no_of_nodes = adjacency_matrix[source].length - 1;


int visited[] = new int[no_of_nodes + 1];

int element = source;

int i = source;

System.out.print(element + "\t");

visited[source] = 1;

stack.push(source);

while (!stack.isEmpty())

element = stack.peek();

i = element;

while (i <= no_of_nodes)

if (adjacency_matrix[element][i] == 1 && visited[i] == 0)

stack.push(i);

visited[i] = 1;

element = i;

i = 1;

System.out.print(element + "\t");

continue;

}
i++;

stack.pop(); }

import java.util.Scanner;

class SumOfSubsets {

int[] w;

int[] x;

int sum,n;

int total = 0;

SumOfSubsets()

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of elements:");

n = sc.nextInt();

w = new int[n + 1];

x = new int[n + 1];

}
boolean issafe()

Scanner sc = new Scanner(System.in);

System.out.println("Enter "+n+" Elements:");

for (int i = 1; i < n + 1; i++) {

w[i] = sc.nextInt();

total += w[i];

System.out.println("Enter the sum to be obtained:");

sum = sc.nextInt();

if (total < sum) {

return false;

return true;

void sumofsubsetUtil(int s, int k, int r) {

int i = 0;

x[k] = 1;

if (s + w[k] == sum) {

System.out.println("\nsolution set is:");

for (i = 1; i <= k; i++) {


System.out.print(x[i]+" ");

else if ((s + w[k] + w[k + 1]) <= sum)

sumofsubsetUtil(s + w[k], k + 1, r - w[k]);

if ((s + r - w[k]) >= sum && (s + w[k + 1]) <= sum) {

x[k] = 0;

sumofsubsetUtil(s, k + 1, r - w[k]);

void solveSum()

if(issafe()==false)

System.out.println("Not possible to obtain the subset!!!");

sumofsubsetUtil(0, 1, total);

import static java.lang.Math.abs;

class Main
{
int N=8,count=0;
static int x[]=new int[8];
int place(int k,int i,int x[])
{
int j;
for(j=0;j<k;j++)
{
if(i==x[j] || abs(k-j)==abs(i-x[j]))
return 0;
}
return 1;
}
void Nqueen(int k)
{
int i,j;

for(i=0;i<N;i++)
{
if(place(k,i,x)==1)
{
x[k]=i;
if(k==N-1)
{
count++;
for(j=0;j<=k;j++) System.out.print(x[j]+" ");

System.out.println();
}
else
Nqueen(k+1);
}

}
public static void main (String[] args)
{
Main o= new Main();
o.Nqueen(0);
System.out.println("Total "+o.count+" solutions are possible\n");
}
}

public class SosTest

public static void main(String[] args)

new SumOfSubsets().solveSum();
}

You might also like