Anda di halaman 1dari 8

Name: Behzad Shaukat

Roll no.: 2k16-ele-056

Q1 Briefly explain the main principal of Binary Search Algorithm in


your own words.

In linear search method a loop is run in th array and it checks the elements.
In Binary search method it starts with an interval covering the whole array.
Array is divided in two halves. If the search element is less than the element
in the middle the array should be shorted to the lower half. If the element
is greater than middle than array is narrowed to the upper half.
Array should be in sorted order. It is a limitation.
It is a fast search method.
References: https://www.youtube.com/watch?v=1HIFzve0zCM&t=334s

Q2. Modify the code in Example 2.4 so that it can take user input for the
following

Set the Size of the Array


Set the Elements of the Array
Find a specific key
Delete a specific key

// orderedArray.java

// demonstrates ordered array class

// to run this program: C>java OrderedApp

////////////////////////////////////////////////////////////////

import java.util.Scanner;

classOrdArray

{
private long[] a; // ref to array a

private int nElems; // number of data items

//-----------------------------------------------------------

public OrdArray(int max) // constructor

a = new long[max]; // create array

nElems = 0;

//-----------------------------------------------------------

public int size()

return nElems;

//-----------------------------------------------------------

publicint find(long searchKey)

int lowerBound = 0;

int upperBound = nElems-1;

intcurIn;

while(true)

curIn = (lowerBound + upperBound ) / 2;

if(a[curIn]==searchKey)
return curIn; // found it

else if(lowerBound>upperBound)

return nElems; // cant find it

else // divide range

if (a[curIn] <searchKey)

lowerBound = curIn + 1; // its in upper half

else

upperBound = curIn - 1; // its in lower half

} // end else divide range

} // end while

} // end find()

//-----------------------------------------------------------

public void insert(long value) // put element into array

int j;

for(j=0; j<nElems; j++) // find where it goes

if(a[j] > value) // (linear search)

break;

for(int k=nElems; k>j; k--) // move bigger ones up

a[k] = a[k-1];

a[j] = value; // insert it

nElems++; // increment size


} // end insert()

//-----------------------------------------------------------

public boolean delete(long value)

int j = find(value);

if(j==nElems) // cant find it

return false;

else // found it

for(int k=j; k<nElems; k++) // move bigger ones down

a[k] = a[k+1];

nElems--; // decrement size

return true;

} // end delete()

//-----------------------------------------------------------

public void display() // displays array contents

for(int j=0; j<nElems; j++) // for each element,

System.out.print(a[j] + " "); // display it

System.out.println("");

//-----------------------------------------------------------
} // end class OrdArray

////////////////////////////////////////////////////////////////

classOrderedApp

public static void main(String[] args)

Scanner userinput=new Scanner(System.in);

//-----------------------------------------------------------

System.out.print("Enter the length of array:");

int length = userinput.nextInt(); // array size

OrdArrayarr = new OrdArray(maxSize); // referrence and creation of


array

System.out.print("Enter the number of Elements:");

int elems= userinput.nextInt();

//-----------------------------------------------------------

System.out.print("Enter the Elements:"); // entering the no. of


elements

long value;

for(int j=0;j<elems;j++) // loop for entering the elements

arr.insert(value= userinput.nextLong()); // referreing to method of insert


from OrdArray class

//-----------------------------------------------------------
System.out.print("The numbers are:");

arr.display(); // display items

//-----------------------------------------------------------

System.out.print("Enter the numbers to serach search:");

int searchKey = scan.nextInt(); // taking the element

if(arr.find(searchKey) = arr.size() )

System.out.println("Found " + searchKey);

else

System.out.println("Cant find " + searchKey);

//-----------------------------------------------------------

System.out.print("Enter the number to Delete:");

arr.delete(value= userinput.nextLong()); //deleting the elements

//-----------------------------------------------------------

System.out.print("The numbers after deleting are:");

arr.display(); // display items again

} // end main()

} // end class OrderedApp


////////////////////////////////////////////////////////////////

References:

Robert ford, Arrays in Data Structures and Algorithm in Java,800 East 96th Street,
Indianapolis, Indiana 46240

Q3. Graphically demonstrate how the find function performs search


operation on the array and with every instruction (line-by-line) what
changes the array (step-by-step) undergoes.
First we sort the array.
Then we assign high and low position i.e. the starting index value and the
ending value.

Lower Position Middle number


Upper position

We divided the array in two halves.


The search key is searched in the array at the middle number.
If it is not equal then the half is lessened by one.
High boundary is shifted new middle number made.

Lower position Middle number Upper poistion

Search key will check to it.


If the search key is founded it will tells the location.
If search key is greater than the middle number.
The lower boundary is shifted toward the higher boundary from middle
number.
Then search key is checked to the middle number.

Middle number
Position
Lower Upper Position


2ndHalf

References https://www.youtube.com/watch?v=1HIFzve0zCM&t=334s

Anda mungkin juga menyukai