Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Binary Search

$
0
0
Hello again :)
I am to do an exercice where a user has to enter a value (ZIP code) and then I have to print out the matching address. The way we had to do this was very specific, so please don't critize my way of going around the problem.

Now here is the whole code

class Assignement6 {
	
public static void main (String[] args) {


String Address[] = {"17 3FR Rue Bonaparte 75001", "91 4FL Rue Joseph Bara 75006", "85 2FL Rue Rivoli 75004", "75 4FR Rue De L'Opera 75012", "29 9FR Rue Port-Royal 75018", "59 1FL Rue Pierre Nicole 75005", "58 7FR Rue Monge 75002", "15 6FR Rue Voltaire 75009", "76 7FL Rue Auguste 75003", "53 4FR Rue Des Saints-Pere 75008"};


String[] ZIPStr = new String [10]; //variable to hold zip as a string

int[] ZIPInt = new int [10]; //variable to hold zip as int

int temp;//for bub sort

String tempstr;//for the bub to switch addresses

int minIndex=0; //for bub


for(int i=0; i<10; i++)
{
	ZIPStr[i] = Address[i].substring(Address[i].length()-5); //separates the ZIP code from the address
}


for (int i=0; i<10; i++)
{
	ZIPInt[i] = Integer.parseInt(ZIPStr[i]); //Converts the string array w/ the zip code into an array w/ zips as int
}


//STARTING SORTING
for(int i=0; i<ZIPInt.length; i++)
{
minIndex=i; //creates a variable that hold i for now

	for(int j=i+1; j<ZIPInt.length; j++)//to control innerr and outer w/j and i
	{
		if(ZIPInt[j]>ZIPInt[minIndex])//if the value that is after i (i.e. j) is bigger then the previous one
		minIndex=j; 
	}
	
		if(minIndex != i)//if minindex not equal i
		{
			temp = ZIPInt[minIndex]; //swap a with c
			ZIPInt[minIndex] = ZIPInt[i];//swap c and b
			ZIPInt[i] = temp;// swap b and a
		} //done with sorting the ZIPS
		
		
		
			if(minIndex != i)//if minindex not equal i
		{
			tempstr = Address[minIndex]; //swap a with c
			Address[minIndex] = Address[i];//swap c and b
			Address[i] = tempstr;// swap b and a
		}	//switching addresses so they match the zip
	
} //DONE SORTING



int search;

System.out.println("Enter a ZIP code [75001...75020]");
search=TextIO.getInt(); // accepts user input


//STARTING BS


int low=0;
int high=ZIPInt.length-1; //search may appear from low to high
int mid= (low+high)/2; //finding middle index

	
while(low<=high)
{//start while	

	
	if(search == ZIPInt[mid])
	{
		System.out.println("Addresses found at that ZIP code: " + Address[mid]);
		break;	
	}
	


	else if(search<ZIPInt[mid])//to the right
	{
		low= mid +1;
		mid= (low+high)/2;
		System.out.println("Addresses found at that ZIP code: " + Address[mid]);	
		break;	
	}
	
	
	
	else if(search>ZIPInt[mid])//to the lift
	{
		high=mid-1;
		mid=(low+high);
		System.out.println("Addresses found at that ZIP code: " + Address[mid]);
		break;
	}


	else
	{
		System.out.println("No matching addresses to the ZIP code");
		break;
	}



}//END BS























}
}



My problems are there
//STARTING BS


int low=0;
int high=ZIPInt.length-1; //search may appear from low to high
int mid= (low+high)/2; //finding middle index

	
while(low<=high)
{//start while	

	
	if(search == ZIPInt[mid])
	{
		System.out.println("Addresses found at that ZIP code: " + Address[mid]);
		break;	
	}
	


	else if(search<ZIPInt[mid])//to the right
	{
		low= mid +1;
		mid= (low+high)/2;
		System.out.println("Addresses found at that ZIP code: " + Address[mid]);	
		break;	
	}
	
	
	
	else if(search>ZIPInt[mid])//to the lift
	{
		high=mid-1;
		mid=(low+high);
		System.out.println("Addresses found at that ZIP code: " + Address[mid]);
		break;
	}


	else
	{
		System.out.println("No matching addresses to the ZIP code");
		break;
	}



}//END BS



I have narrowed down and attributed my problem to the fact that I am not printing the write position in the address.
Any help would be welcome.
Thank you

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>