Saturday, 27 February 2016

Types of pyramid programs pattern 1


Program
class Main{
public static void main(String args[]){
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++)
System.out.print("*");
System.out.println("");
}          
}
}

OUTPUT:-
C:\Users\Saty\Desktop\java>javac 1.java
C:\Users\Saty\Desktop\java>java Main
*
**
***
****
*****

Wednesday, 17 February 2016

What is the difference between depth-first-search and breadth-first-search?

1. In BFS the root node is expanded first, then all the successors of the root node are expanded, and in next step all the successors of every node are expanded, the process continues till the goal is achived.   while
In DFS we explore the root node and traverse as far as possible from the root node untill the goal is    achived.

2.In BFS the space complexity is more critical as compared to time complexity.   while
In DFS has lesser space complexity, because at a time it needs to store only single path from the root to leaf node.

3.Breadth First Search can be done with the help of queue i.e, FIFO(First In FirstOut).   while
Depth First Search can be done with the help of stack i.e. LIFO(Last In First Out).

4.BFS is slower than DFS.   while
DFS is more faster than BFS.

5.BFS requires more memory compare to DFS.  while
DFS require less memory compare to BFS.

6.BFS is useful in finding shortest path.  while
DFS in not so useful in finding shortest path.


BFS  Example:-


DFS  Example:-

Monday, 8 February 2016

Java program for insertion of elements using an array



import java.util.Scanner;
class Main{
public static void main(String args[]){
int i,number=8;
int list[]=new int[20];
Scanner input=new Scanner(System.in);
for(i=1;i<=number;i++){
System.out.println("Enter the numbers : ");
list[i]=input.nextInt();
}
System.out.println("Enter the number to be inserted : ");
int num=input.nextInt();
System.out.println("Enter the position");
int pos=input.nextInt();
for(i=number;i>=pos;i--){
list[i+1]=list[i];
}
list[pos]=num;
if(pos>number){
System.out.println("Insertion outside the array");
}
number++;
for(i=1;i<=number;i++){
System.out.println("New array"+list[i]);
}
}
}

OUTPUT:-
C:\Users\saty\Desktop>javac 1.java
C:\Users\saty\Desktop>java Main
Enter the numbers :
12
Enter the numbers :
4
Enter the numbers :
34
Enter the numbers :
54
Enter the numbers :
68
Enter the numbers :
36
Enter the numbers :
42
Enter the numbers :
16
Enter the number to be inserted :
65
Enter the position
5
New array12
New array4
New array34
New array54
New array65
New array68
New array36
New array42
New array16