Searching Algorithms in Data Structure

0

Searching Algorithms in Data Structure


Searching algorithm in Data structure



Linear Search
Binary Search

Linear search program in java

Operations:

1.       arrayinalization()

2.       getdata()

3.       searchInArray()

                                                        

Program Code:

 

import java.util.Scanner;

class LinearSearch

{

               

                Scanner objet= new Scanner(System.in);

                int[] array;

                int num;

                int length;

                public void arrayinalization()

                {

                                System.out.println("\nEnter lenght of array");

                                length=objet.nextInt();

                                array =new int[length];

                }

                               

                public void getdata()

                {

                                System.out.println("Enter elements in array");

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

                                {

                                                array[i]=objet.nextInt();

                                }

                }

                               

public void searchInArray()

{

                                System.out.println("Enter element for search");

                                num=objet.nextInt();

                               

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

                {

                if (array[i]==num)

                                {

                                System.out.println("Number is founded in index "+ i);

                                }

                }

}

                                public static void main(String[] args)

                 {

                 System.out.println("\t\t....Created by Jhangir Mughal....");

                 System.out.println("\n\t\t\t***Linear Search***");

                                LinearSearch object;

                                object= new LinearSearch();

                                object.arrayinalization();

                    object.getdata();

                    object.searchInArray();

                 }

}


Binary search Program in Java:

Operations:                                                   

1.       arrayinalization()

2.       getdata()

3.       search()

 

Program Code:

 

import java.util.Scanner;

class binarySearch

{

   Scanner obj= new Scanner(System.in);

                int[] array;

                int key;

                int length;

               

                public void arrayinalization()

                {

                                System.out.println("\nEnter lenght of array");

                                length=obj.nextInt();

                                array =new int[length];

                }

               

                public void getdata()

                {

                                System.out.println("Enter elements in array");

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

                                {

                                                array[i]=obj.nextInt();

                                }

                               

                System.out.println("Enter element for search");

                                key=obj.nextInt();

                }

                public int search()

                {

                                int start ,end,mid;

                    start =0;

                    end=length-1;

                    while(start<=end)

                    {

                                mid=(start+end)/2;

                   

                      if(key>array[mid])

                                start=mid+1;

                                else if(key<array[mid])

                                end=mid-1;

                   

                                else if(key==array[mid])

                                {

                                System.out.println("element found in index" +mid);

                                break;

                                }

                    }

                    return -1;

                }

               

                               

                public static void main(String[] args)

                 {

                 System.out.println("\t\t....Created by Jhangir Mughal....");

                                 System.out.println("\n\t\t\t***Binary Search***");

                                binarySearch object;

                                object= new binarySearch();

                                object.arrayinalization();

                    object.getdata();

                                object.search();

                 }

}


Post a Comment

0Comments
Post a Comment (0)