Showing posts with label Data Structures. Show all posts
Showing posts with label Data Structures. 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]