Showing posts with label Data Structures In Java. Show all posts
Showing posts with label Data Structures In Java. Show all posts

Monday, December 23, 2013

Simple Program For Binary Search In Java Programming

Algorithm Definition:


A straightforward implementation of binary search is recursive. The initial call uses the indices of the entire array to be searched. The procedure then calculates an index midway between the two indices, determines which of the two subarrays to search, and then does a recursive call to search that subarray. Each of the calls is tail recursive, so a compiler need not make a new stack frame for each call. The variables imin and imax are the lowest and highest inclusive indices that are searched.

Example Program For Binary Search In Java




import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;


// Simple Program For Binary Search In Java Using Class


public class BinarySearchExample {


    public static void main(String arg[]) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        BinarySearch binarySearch = new BinarySearch(10);

        binarySearch.readData();

        System.out.println("Enter the Element to search");

        int find = Integer.parseInt(br.readLine());

        int index = binarySearch.search(find);


        if (index != -1) {

            System.out.println("Element found Position : " + (index+1));

        } else {

            System.out.println("Element not found");

        }

    }

}


class BinarySearch {


    private int data[];

    private int size;


    public BinarySearch(int size) {

        this.data = new int[size];

        this.size = size;

    }


    public void readData() throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Please Enter Values");

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

            System.out.println("Enter Value #"+(i+1)+" : ");

            data[i] = Integer.parseInt(br.readLine());

        }

    }


    public int search(int find) {

        int start = 0;

        int end = data.length - 1;

        int mid;

        while (start <= end) {

            mid = (start + end) / 2;

            if (data[mid] == find) {

                return mid;

            } else if (data[mid] < find) {

                start = mid + 1;

            } else if (data[mid] > find) {

                end = mid - 1;

            }

        }

        return -1;

    }

}


Sample Output:


Please Enter Values

Enter Value #1 :

34

Enter Value #2 :

11

Enter Value #3 :

22

Enter Value #4 :

45

Enter Value #5 :

78

Enter the Element to search

45

Element found Position : 4

 

Simple Program For Binary Search In Java ( Simple Way )

Binary Search Algorithm Definition:


A straightforward implementation of binary search is recursive. The initial call uses the indices of the entire array to be searched. The procedure then calculates an index midway between the two indices, determines which of the two subarrays to search, and then does a recursive call to search that subarray. Each of the calls is tail recursive, so a compiler need not make a new stack frame for each call. The variables imin and imax are the lowest and highest inclusive indices that are searched.


Simple Program For Binary Search In Java




import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;


// Simple Program For Binary Search In Java Using Class


public class BinarySearchExample {


    public static void main(String arg[]) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter number of elements");

        int num = Integer.parseInt(br.readLine());

        int a[] = new int[num];

        System.out.println("Please enter");

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

            a[i] = Integer.parseInt(br.readLine());

        }

        System.out.println("Enter the element to search");

        int find = Integer.parseInt(br.readLine());

        int index = search(a, find);

        if (index != -1) {

            System.out.println("Element found : " + index);

        } else {

            System.out.println("Element not found");

        }

    }


    public static int search(int ar[], int find) {

        int start = 0;

        int end = ar.length - 1;

        int mid;

        while (start <= end) {

            mid = (start + end) / 2;

            if (ar[mid] == find) {

                return mid;

            } else if (ar[mid] < find) {

                start = mid + 1;

            } else if (ar[mid] > find) {

                end = mid - 1;

            }

        }

        return -1;

    }

}


Simple Program For Binary Search Sample Output:



Please Enter Values


Enter No Of Elements

5

Please enter

Enter Value #1 :

67

Enter Value #2 :

34

Enter Value #3 :

45

Enter Value #4 :

90

Enter Value #5 :

101

Enter the Element to search

45

Element found Position : 3

Simple Example Program For Queue In Java Using Array and Class

Queue Wiki Definition:


In each of the cases, the customer or object at the front of the line was the first one to enter, while at the end of the line is the last to have entered. Every time a customer finishes paying for their items (or a person steps off the escalator, or the machine part is removed from the assembly line, etc.) that object leaves the queue from the front. This represents the queue “dequeue” function. Every time another object or customer enters the line to wait, they join the end of the line and represent the “enqueue” function. The queue “size” function would return the length of the line, and the “empty” function would return true only if there was nothing in the line.

Simple Example Program For Queue In Java Using Array and Class




import java.io.*;


class QueueAction {


    BufferedReader is = new BufferedReader(new InputStreamReader(System.in));

    int items[];

    int i, front = 0, rear = 0, noOfItems, item, count = 0;


    void getdata() {

        try {

            System.out.println("Enter the Limit :");

            noOfItems = Integer.parseInt(is.readLine());

            items = new int[noOfItems];

        } catch (Exception e) {

            System.out.println(e.getMessage());

        }

    }


    void enqueue() {

        try {

            if (count < noOfItems) {

                System.out.println("Enter Queue Element :");

                item = Integer.parseInt(is.readLine());

                items[rear] = item;

                rear++;

                count++;

            } else {

                System.out.println("Queue Is Full");

            }

        } catch (Exception e) {

            System.out.println(e.getMessage());

        }

    }


    void dequeue() {

        if (count != 0) {

            System.out.println("Deleted Item :" + items[front]);

            front++;

            count--;

        } else {

            System.out.println("Queue IS Empty");

        }

        if (rear == noOfItems) {

            rear = 0;

        }

    }


    void display() {

        int m = 0;

        if (count == 0) {

            System.out.println("Queue IS Empty");

        } else {

            for (i = front; m < count; i++, m++) {

                System.out.println(" " + items[i]);

            }

        }

    }

}


class QueueProgram {


    public static void main(String arg[]) {

        DataInputStream get = new DataInputStream(System.in);

        int choice;

        QueueAction queue = new QueueAction();

        queue.getdata();

        System.out.println("Queue\n\n");

        try {

            do {

                System.out.println("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");

                System.out.println("Enter the Choice : ");

                choice = Integer.parseInt(get.readLine());

                switch (choice) {

                    case 1:

                        queue.enqueue();

                        break;

                    case 2:

                        queue.dequeue();

                        break;

                    case 3:

                        queue.display();

                        break;

                }

            } while (choice != 4);

        } catch (Exception e) {

            System.out.println(e.getMessage());

        }

    }

}


Sample Output:


Enter the Limit :

4

Queue


1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

1

Enter Queue Element :

45

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

1

Enter Queue Element :

67

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

1

Enter Queue Element :

89

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

1

Enter Queue Element :

567

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

1

Queue Is Full

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

3

 45

 67

 89

 567

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

2

Deleted Item :45

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

2

Deleted Item :67

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

2

Deleted Item :89

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

2

Deleted Item :567

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

2

Queue IS Empty

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

4

Simple Example Program For Queue In Java

Queue Wiki Definition:


In each of the cases, the customer or object at the front of the line was the first one to enter, while at the end of the line is the last to have entered. Every time a customer finishes paying for their items (or a person steps off the escalator, or the machine part is removed from the assembly line, etc.) that object leaves the queue from the front. This represents the queue “dequeue” function. Every time another object or customer enters the line to wait, they join the end of the line and represent the “enqueue” function. The queue “size” function would return the length of the line, and the “empty” function would return true only if there was nothing in the line.

Simple Example Program For Queue In Java




import java.io.IOException;

import java.util.LinkedList;

import java.util.Queue;


public class QueueExample {


    public static void main(String arg[]) throws IOException {

        Queue<Integer> q = new LinkedList<Integer>();

        

        // Add in Queue

        q.add(23);

        q.add(33);

        System.out.println(q);

        System.out.println("Queue Element :"+q.element());

        // Offer In Queue 

        q.offer(34);

        q.offer(98);

        q.offer(77);


        System.out.println(q);

        System.out.println("Queue Element :"+q.element());

        q.poll();

        System.out.println("Queue Element :"+q.element());

        System.out.println("After poll : " + q);

    }

}


Sample Output:


[23, 33]

Queue Element :23

[23, 33, 34, 98, 77]

Queue Element :23

Queue Element :33

After poll : [33, 34, 98, 77]

Sunday, December 22, 2013

Simple Example Program For Stack in Java Using Array and Class

A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.

Simple Example Program For Stack in Java Using Array and Class




// Simple Example Program For Stack in Java Using Array and Class

// Coded By Thiyagaraaj M.P


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;


class Stack {


    private int top;

    private int item[];


    Stack(int size) {

        top = -1;

        item = new int[size];

    }


    void pushItem(int data) {

        if (top == item.length - 1) {

            System.out.println("Stack is Full");

        } else {

            item[++top] = data;

            System.out.println("Pushed Item :" + item[top]);

        }

    }


    int popItem() {

        if (top < 0) {

            System.out.println("Stack Underflow");

            return 0;

        } else {

            System.out.println("Pop Item : " + item[top]);

            return item[top--];

        }

    }

}


class StackExample {


    public static void main(String[] args) throws IOException {

        Stack stk = new Stack(5);

        boolean yes=true;

        int choice;

        BufferedReader is = new BufferedReader(new InputStreamReader(System.in));

        

        do{

            System.out.println("1).Push\n2).Pop\n3).Exit\n\nEnter Choice");

            choice = Integer.parseInt(is.readLine());

            

            switch(choice)

            {

                case 1: System.out.println("Enter Push Item: ");

                        stk.pushItem(Integer.parseInt(is.readLine()));

                        break;

                case 2: stk.popItem();break;

                case 3: yes = false;break;

                default: System.out.println("Invalid Choice");

            }

        }while(yes==true);

        

    }

}


Sample Output:


1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

14

Pushed Item :14

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

567

Pushed Item :567

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

67

Pushed Item :67

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

789

Pushed Item :789

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

56

Pushed Item :56

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

99

Stack is Full

1).Push

2).Pop

3).Exit


Enter Choice

2

Pop Item : 56

1).Push

2).Pop

3).Exit


Enter Choice

2

Pop Item : 789

1).Push

2).Pop

3).Exit


Enter Choice

2

Pop Item : 67

1).Push

2).Pop

3).Exit


Enter Choice

2

Pop Item : 567

1).Push

2).Pop

3).Exit


Enter Choice

2

Pop Item : 14

1).Push

2).Pop

3).Exit


Enter Choice

2

Stack Underflow

1).Push

2).Pop

3).Exit


Enter Choice

3

Simple Example Program For Stack in Java Stack Utils

Definition:


A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.

Simple Example Program For Stack in Java Stack Utils




// Simple Example Program For Stack in Java Stack Utils

// Coded By Thiyagaraaj M.P


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Stack;


class StackProgram {


    public static void main(String[] args) throws IOException {

        Stack stk = new Stack();

        stk.setSize(5);

        

        boolean yes=true;

        int choice;

        BufferedReader is = new BufferedReader(new InputStreamReader(System.in));

        

        do{

            System.out.println("1).Push\n2).Pop\n3).Exit\n\nEnter Choice");

            choice = Integer.parseInt(is.readLine());

            

            switch(choice)

            {

                case 1: System.out.println("Enter Push Item: ");

                        stk.push(Integer.parseInt(is.readLine()));

                        break;

                case 2: System.out.println("Poped Item : "+stk.pop());break;

                case 3: yes = false;break;

                default: System.out.println("Invalid Choice");

            }

        }while(yes==true);

        

    }

}


Sample Output:


run:

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

23

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

45

1).Push

2).Pop

3).Exit


Enter Choice

2

Poped Item : 45

1).Push

2).Pop

3).Exit


Enter Choice

2

Poped Item : 23

1).Push

2).Pop

3).Exit

Simple Example Program For Stack in Java( Simple Way Of Use )

Definition:


A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.

Simple Example Program For Stack In Java


// Simple Example Program For Stack ( Simple Way Of Use )

// Coded By Thiyagaraaj M.P

import java.util.Stack;


public class SimpleStack {


    public static void main(String[] args) {

        Stack stack = new Stack();


        System.out.println("Stack Items \n" + stack);

        System.out.println("Stack Size :" + stack.size());


        System.out.println("Stack Push \n");

        stack.push("A");

        stack.push(new Integer(20));

        stack.push("Example");


        System.out.println("Stack Items \n" + stack);

        System.out.println("Stack Size :" + stack.size());


        System.out.println("Stack Pop \n");

        System.out.println("Pop Data :" + stack.pop());

        System.out.println("Pop Data " + stack.pop());

        System.out.println("Pop Data " + stack.pop());


        System.out.println("Stack Items \n" + stack);

        System.out.println("Stack Size :" + stack.size());

    }

}

Sample Output:


Stack Items

[]

Stack Size :0

Stack Push


Stack Items

[A, 20, Example]

Stack Size :3

Stack Pop


Pop Data :Example

Pop Data 20

Pop Data A

Stack Items

[]

Stack Size :0