0% found this document useful (0 votes)
23 views

Java (Assignment1)

This document contains code for performing a binary search on an array. The code sorts the array in ascending order using a bubble sort. It then performs a binary search to find the index of a target value in the sorted array, iterating the search space until the target is found or the space is exhausted, at which point it reports whether the target was found.

Uploaded by

Irfan Memon
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Java (Assignment1)

This document contains code for performing a binary search on an array. The code sorts the array in ascending order using a bubble sort. It then performs a binary search to find the index of a target value in the sorted array, iterating the search space until the target is found or the space is exhausted, at which point it reports whether the target was found.

Uploaded by

Irfan Memon
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

ASSIGNMENT LINEARY AND BINARY SEARCH

BINARY SEARCH CODE Irfanullah Memon Roll # 10SW19

class Bin { public static void main( String args[]) { int array[] = { 16, 23, 02, 03, 8, 04, 06, 07, 65, 9, 10, 05}; int beg=0, end=array.length; int temp, loc=0, var=5, mid; for ( int i=0;i<array.length;i++) { int ptr=0; while ( ptr < array.length-1) { if ( array[ptr]>array[ptr+1]) { temp = array [ptr]; array [ptr] = array[ptr+1]; array [ptr+1] = temp; } ptr = ptr+1; } } mid = (int)(array.length/2); while ( beg <= end && array[mid]!=var) { if ( var < array[mid]) { end = mid - 1; } else { beg = mid + 1; } mid = (int)( beg + end/2); } if ( array[mid]==var) { loc = mid; System.out.println( loc + " is the index of content "); } else { System.out.println(" Content is not present " ); } } }

Irfanullah Memon Roll # 10SW19

You might also like