Tuesday, May 31, 2016

Stack Implementation program in java

Hi guys , today we are going to learn how to implement stack using array in java. this is one of the most popular java interview question
So lets see how to implement in java using array.

Program 

public class StackOperation {
static int top=-1;
static int[] dataList ;
public static void main(String[] args)
{
// int[] dataList ={10,23,45,5,3,2};
dataList = new int[6];
push(2);
push(3);
push(5);
push(10);
push(23);
push(45);
push(48);
// push(15);
push(3);
pop();
pop();
peek();
}
public static void push(int data )
{
if(top != dataList.length-1)
{
top++;
System.out.println("pushing data "+data);
dataList[top] =data;
}else{
System.out.println("Stack Overflow");
}
}
public static void pop()
{
if(top!=-1)
{
top--;
}else{
System.out.println("Stack Underflow");
}
}
public static void peek()
{
System.out.println(dataList[top]);
}
Hope you guys are able to understand the code if you have any problem , do let me know by comment below.

Thanks for reading

-Noeik

Thursday, May 26, 2016

Tower of Hanoi program in java

Hi Guys , Hope you all are doing well , as we all know that the most populate and most favorite question of interview is Tower of hanoi problem and most of the guy fells that the tower of hanoi program is very much complex.

in my earlier post I have talked about
Bubble Sort 
Selection Sort
Insertion Sort 

So lets see how complex the tower of hanoi program is

Java code of Tower of Hanoi Program 

public class TowerOfHanoi {
public static void main(String[] args) {
move(3 ,'A','C','B');
}
public static void move(int noOfDisc ,char from,char to,char inter)
{
if(noOfDisc==1) System.out.println("Moving Disk "+noOfDisc+" from "+from+" to "+to);
else{
move(noOfDisc-1 ,from, inter,to);
System.out.println("Moving Disk "+noOfDisc+" from "+from+" to "+to);
move (noOfDisc-1,inter,to,from);
// System.out.println("Moving Disk "+noOfDisc+" from "+from+" to "+to);
}
}
Output:-
Moving Disk 1 from A to C
Moving Disk 2 from A to B
Moving Disk 1 from C to B
Moving Disk 3 from A to C
Moving Disk 1 from B to A
Moving Disk 2 from B to C
Moving Disk 1 from A to C

In this program we use the recursion algorithm to calculate the steps required to move the plates from one to another tower.
If you have any problem , do comment below

Thank for reading

-Noeik

Saturday, May 21, 2016

Insertion Sort Program in Java using Array

Hi Everyone ,
In Previous post we have discussed about the Selection Sort its program and  its time complexity, Today we are going to code insertion sort using array list, This is the most popular and most used Sort in all after mert sort.
So lets see the code.

Implementation of Insertion Sort Using Array

/*
 @author noeik
 */
public class InsertionSort {
   
    static int[] data = {10,23,45,5,3,2};// global data integer array
    public static void main(String[] args) {
       
        sort(data);
       
    }
   
    public static void sort(int[] data)
    {
        int j;
        int current;
        for(int i=0;i<data.length;i++)
        {
            current =data[i];
            j=i-1;
            while(j>=0&&data[j]>current)
            {
                data[j+1]=data[j];
                j--;
               
            }
            data[j+1]=current;
            display();
        }
    }
//    displaying the array method
    public static void display()
    {
        String result = "[";
        for(int d :data)
        {
            result = result +d+",";
        }
        result =result+"]";
        System.out.println(result);
    }

}

Output:
[10,23,45,5,3,2,]
[10,23,45,5,3,2,]
[10,23,45,5,3,2,]
[5,10,23,45,3,2,]
[3,5,10,23,45,2,]
[2,3,5,10,23,45,]
[2,3,5,10,23,45,]


Time Complexity:-  The Time Complexity of Insertion Sort is O(n^2)  as there are two loop used.

So Guys hope you all understand the code and if you have any problem just leave comment.
In Next Post , will share the code of Merge Sort.

Thanks for reading 
-Noeik 

Thursday, May 19, 2016

Selection Sort Program Implementation Program in java

Hello Guys ,
In my previous post ,  I discussed about bubble sort program implementation in java using array , Today we will learn the selection sort program implementation in java using array again
So lets get start

Program Implementation of Selection Sort in java 


/*
 @author noeik
 */
public class SelectionSort {
   
    static int[] data = {10,23,45,5,3,2};// global data integer array
    public static void main(String[] args) {
      
        sort(data);
        display();      
    }
//    selection sorting method
    public static void sort(int[] data)
    {
        for(int i=0;i<data.length-1;i++)
        {
            int minIndex =i;
            for(int j=i+1;j<data.length;j++)
            {
                System.out.println("before sorting element");
                display();
                if(data[j]<data[minIndex])
                {
                    int temp =data[j];
                    data[j]=data[minIndex];
                    data[minIndex]=temp;
                    System.out.println("After sorting element");
                    display();
                }
            }
        }
    }
//    displaying the array method
    public static void display()
    {
        String result = "[";
        for(int d :data)
        {
            result = result +d+",";
        }
        result =result+"]";
        System.out.println(result);
    }

}

Output
before sorting element
[10,23,45,5,3,2,]
before sorting element
[10,23,45,5,3,2,]
before sorting element
[10,23,45,5,3,2,]
After sorting element
[5,23,45,10,3,2,]
before sorting element
[5,23,45,10,3,2,]
After sorting element
[3,23,45,10,5,2,]
before sorting element
[3,23,45,10,5,2,]
After sorting element
[2,23,45,10,5,3,]
before sorting element
[2,23,45,10,5,3,]
before sorting element
[2,23,45,10,5,3,]
After sorting element
[2,10,45,23,5,3,]
before sorting element
[2,10,45,23,5,3,]
After sorting element
[2,5,45,23,10,3,]
before sorting element
[2,5,45,23,10,3,]
After sorting element
[2,3,45,23,10,5,]
before sorting element
[2,3,45,23,10,5,]
After sorting element
[2,3,23,45,10,5,]
before sorting element
[2,3,23,45,10,5,]
After sorting element
[2,3,10,45,23,5,]
before sorting element
[2,3,10,45,23,5,]
After sorting element
[2,3,5,45,23,10,]
before sorting element
[2,3,5,45,23,10,]
After sorting element
[2,3,5,23,45,10,]
before sorting element
[2,3,5,23,45,10,]
After sorting element
[2,3,5,10,45,23,]
before sorting element
[2,3,5,10,45,23,]
After sorting element
[2,3,5,10,23,45,]
[2,3,5,10,23,45,]
 
 Time Complexity : The Time Complexity of the Selection Sort Algorithm is O(n^2) ,Because we are using two for loop in the algo.

Hope you guys understand the program well and if you have any problem do comment below.

In next Post I will share the Program of Insertion Sort Using Array.

Thanks for Reading
-Noeik

Bubble Sort using array Program in Java

Hi Guys!!
We know that there are some sorting algorithm in Data Structure under which the easiest one is bubble sort.
so lets see the bubble sort in java using array.


Implementaion of Bubble Sort Using Array

/*
 *
 @author noeik
 *
 */

public class BubbleSort {
   
    static int[] data = {10,23,45,5,3,2};// global data integer array
    public static void main(String[] args) {
       
        sort(data);
        display();   
}
//main sort method
    public static void sort(int[] data)
    {
        for(int i =0;i<data.length-1;i++)
        {
            for(int j=0;j<data.length-1-i;j++)
            {
                if(data[j]>data[j+1])
                {
                    int temp =data[j+1];
                    data[j+1] =data[j];
                    data[j] =temp;
                   
                }
            }
        }
    }
 // display method to show the array
    public static void display()
    {
        String result = "[";
        for(int d :data)
        {
            result = result +d+",";
        }
        result =result+"]";
        System.out.println(result);
    }

}

Output
[2,3,5,10,23,45,] )
Time Complexity:- The Time Complexity of Bubble Sort is O(n^2) because here in program you can see that the code is having 2 loops


In Next Post I will share the program of Selection Sort with its time complexity and all .

Thanks for Reading , Hope you Understand the code and if you have any problem do comment in Comment section below

-Noeik

Monday, May 16, 2016

Installing java 8 in Ubuntu 14.04 LTS

Hi Everyone ,
Most of the Programmers uses Ubuntu Operating System and most of them don't know how to install java in Ubuntu , So this post is regarding with the Installing Java in Ubuntu OS as it is very easy to install java in Windows.

Here are the things you have to do to install java in Ubuntu.

  • Open the Terminal Using CTRL+ALT+T or type terminal in search Launchpad.
  • In terminal you need to first add the ppa repository of webupd8team which is maintaining the Java repository for the Ubuntu OS.
  • update the system repository 
  • install the java 8 installer

    So Run the Below Commands one by one 

    sudo add-apt-repository ppa:webupd8team/java
    sudo apt-get update
    sudo apt-get install oracle-java8-installer 
    To check whether the java installed Successfully run below command
    java -version


    This Confirm whether the Java with version 8 is successfully installed.


    Hope this post will help you guys, If facing any problem feel free to ask your problem I will try to revert back with solution.

Saturday, May 14, 2016

Java Hello World - Program in java

Java is considered as one of the most powerful and popular programming language of all time because of below features.
  • Security
  • Portability
  • Robust 
  • Object Oriented
  • Architecture-neutral and etc
 So Java is the one programming language which programmer can choice for the future career and whenever we talk about coding of any programming language we first code very first code "Hello World " Program

 Here we go .

Code-

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello World");

    }

Where to write the code-
  1. Open notepad (open run and type notepad hit enter)
  2. write the above code and save the file as HelloWorld.java(same as the class name ).
How to Run the Program
  1. open command prompt
  2. navigate to the file where you save the HelloWorld.java file in console.
  3. write below command
    javac HelloWorld.java
  4. The above command will compile the program and create a file HelloWorld.class
  5. now run
    java HelloWorld
    Your Program will run and you will see in console "Hello World"

    This is very basic and beginner's first program in java world.
    Hope you guys will able to understand the program and if you are facing any problem do comment.