busqueda binaria
Hola necesito ayuda desesperadamente
Necesito el codigo de busqueda binaria implementada en archivos es decir que el arreglo se guarde en un archivo txt y el programa lo lea (lo ordene) y busque el elemento
Porfavor ayuda ya he intentado todo, buscarlo en internet y programarlo yo misma
Ezte metodo lo termine hoy
Public int buscar(int elemento)
{
int vector[] = new int[matriz. Length*matriz[0]. Length];
int mitad,min,max,posi=0;
for(int i=0;i<this.matriz.length;i++)
{
for(int j=0;j<this. Matriz[i]. Length;j++)
{
vector[posi]=this. Matriz[i][j];
posi++;
}//2 for
}//1 for
min=0;
max=vector. Length;
mitad=(min+max)/2;
while(vector[mitad]! =elemento)
{
if(vector[mitad]<elemento)
{
min=mitad+1;
mitad=(min+max)/2;
}//if
else
{
max=mitad-1;
mitad = (min+max)/2;
}//else
if(min>max)
{
break;
}//if
}//while
return vector[mitad];
}//fin del metodo
El truko ez zimple. Generaz un vektor ke almazena el total de la matriz (previamente ordenada).. I la dividez en doz x la mitad.. Preguntaz zi el elemento ke buzkaz ez = a la mitad del vector.. Zi ez azi ya enkontrazte lo ke buzkaz i zi no. Puez entonzez pregunta zi el numero buzkado ez mayor o menor a la mitad de el vector.. Zi ez mayor, la mitad la tomaz komo minimo, ziendo en el kazo kontrario ke la mitad la tomaz komo maximo.. I azi lo hazez hazta ke enkuentrez el elemento dezeado..
Pd. La mitad de la matriz la enkuentraz zumando el minimo maz el maximo i dividiendolo en doz.
Ezpero ke ezto ayude a loz nuevoz programadorez en java (komo yo) ke deban hazer ezte metodo... La verdad ez ke la primera vez zi ke kuezta
por Laura Cabrera Amador (Noviembre 2006)
hay una clase de metodos estaticos llamada Arrays, puedes hacer lo siguiente
Arrays . Sort (arreglo);
Import
java. Io. *;
/**
* Write a description of class matburbuja here.
* metodo que ordena una matriz ejemplo:
9 -8 8 5 -2
-2 -8 5 8 9
* @author (your name)
* @version (a version number or a date)
*/
public class matburbuja()
{
int fil, col, tamFil, tamCol;
int matBur[][];
public matburbuja( int valTamFil, int valTamCol)
{
tamFil=valTamFil;
tamCol=valTamCol;
matBur= new int [tamFil][tamCol];
}
public void llenarMatBurbuja()
{
for (fil=0;fil<tamFil;fil++)
{
for (col=0;col<tamCol;col++)
{
matBur[fil][col]=( int )(Math. Random()*20);
}
}
}
public void impriMatBurbuja()
{
for (fil=0;fil<tamFil;fil++)
{
for (col=0;col<tamCol;col++)
{
System.out.print(matBur[fil][col]+ " " );
}
System.out.println();
}
System.out.println();
}
public void ordeBurbuja()
{
int eleComFil, eleComCol, eleResFil, eleResCol;
int copia;
for (fil=0;fil<tamFil;fil++)
{
for (eleComCol=0;eleComCol<=tamCol-2;eleComCol++)
{
for (eleResCol=eleComCol+1;eleResCol<=tamCol-1;eleResCol++)
{
if
(matBur[fil][eleComCol]<matBur[fil][eleResCol]) Import java. Io. *;
{
copia=matBur[fil][eleComCol];
matBur[ fil][eleComCol]=matBur[fil][eleResCol];
matBur[fil][eleResCol]=copia;
}
}
}
}
}
}
Su principal
/**
* Write a description of class principal here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class principal
{
public static void main(String args[]) throws IOException
{
DataInputStream teclado = new DataInputStream(System. In);
String cad;
int nfil, ncol;
System.out.println( "Ingrese numero de filas " );
do
{
cad=teclado. ReadLine();
if (cad. Length()==0)
{
System.out.println( "no se permite vacios intente nuevamente" );
}
else
{
if (Integer. ParseInt(cad)<0)
{
System.out.println( "no se permite negativo intente nuevamete" );
}
}
} while (cad. Length()==0||Integer. ParseInt(cad)<=0);
nfil=Integer. ParseInt(cad);
System.out.println( "Ingrese numero de columnas " );
do
{
cad=teclado. ReadLine();
if (cad. Length()==0)
{
System.out.print( "no se permite vacios intente nuevamente" );
}
else
{
if (Integer. ParseInt(cad)<0)
{
System.out.print( "no se permite negativo intente nuevamete" );
}
}
} while (cad. Length()==0||Integer. ParseInt(cad)<=0);
ncol=Integer. ParseInt(cad);
matburbuja burbuja = new matburbuja(nfil,ncol);
burbuja. LlenarMatBurbuja();
burbuja. ImpriMatBurbuja();
System.out.println( "antes de ordenar " );
System.out.println();
burbuja. OrdeBurbuja();
burbuja. ImpriMatBurbuja();
System.out.println( " ordenado" );
System.out.println();
System.out.println( "------------**************-------------" );
}
}
Hola soy vladimir de Ecuador por favor ayundenme con codigos de metodos de ordenacion y busqueda de matrices que sean faciles y muy entendibles ayuda!