我该怎么做我的功课与数组,函数和二进制搜索? [英] How do I do my homework with Arrays, Functions and Binary Search?

查看:132
本文介绍了我该怎么做我的功课与数组,函数和二进制搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我想要做的是,询问他有多大希望他的数组是并为用户介绍数组中的值,用户的程序。然后我希望用户能够引入一个数量和方案确定,其中阵列的数目是或者确定它不存在。下面的程序目前产生阵列的随机值和用户介绍他想要阵列多少元素使用。但我不知道如何使这什么我previously解释说。

 包laboratorio9;进口了java.util.Random;
进口java.util.Arrays中;公共类ArregloBinario
{
    私人诠释[] DATOS;
    私有静态随机generador =新的随机();    公共ArregloBinario(INT tamanio)
    {
        DATOS =新INT [tamanio]        的for(int i = 0; I< tamanio;我++)
            DATOS [I] = 10 + generador.nextInt(90);        Arrays.sort(DATOS);
    }    公众诠释busquedaBinaria(INT elementoBusqueda)
    {
        INT劣= 0;
        INT优越= datos.length-1;
        INT德尔梅迪奥=(劣+卓越+ 1)/ 2;
        INT ubicacion = -1;        // **我怎样才能改变以下NTO递归函数> **
        做
        {
            System.out.print(elementosRestantes(逊色,上级));            的for(int i = 0; I<德尔梅迪奥;我++)
                System.out.print();
            的System.out.println(*);            如果(elementoBusqueda == DATOS [德尔梅迪奥])
                ubicacion =德尔梅迪奥;
            否则,如果(elementoBusqueda< D​​ATOS [德尔梅迪奥])
                卓越=内 - 1;
            其他
                劣质=德尔梅迪奥+ 1;            德尔梅迪奥=(劣+卓越+ 1)/ 2;
        }而((劣< =卓越)及及(ubicacion == -1));
        返回ubicacion;
    }    公共字符串elementosRestantes(INT逊色,INT上级)
    {
        StringBuilder的时间=新的StringBuilder();        的for(int i = 0; I<劣势;我++)
            temporal.append();        的for(int i =劣; I< =优越;我++)
            temporal.append(DATOS [I] +);        temporal.append(\\ n);
        返回temporal.toString();
    }    公共字符串的toString()
    {
        返回elementosRestantes(0,datos.length-1);
    }
}//主类//包laboratorio9;进口java.util.Scanner中;公共类PruebaBusquedaBinaria {
    公共静态无效的主要(字串[] args)
    {
        扫描仪ENTRADA =新的扫描仪(System.in);        INT enteroABuscar;
        INT职位;        的System.out.println(请写元件的数目的阵列中);
        INT数= entrada.nextInt();        ArregloBinario arregloBusqueda =新ArregloBinario(数);
        的System.out.println(arregloBusqueda);        System.out.print(写一个值(-1),以走出去:);
        enteroABuscar = entrada.nextInt();
        的System.out.println();        而(enteroABuscar!= -1)
        {
            职位= arregloBusqueda.busquedaBinaria(enteroABuscar);            如果(职位== - 1)
                的System.out.println(值+ enteroABuscar +未找到\\ n);
            其他
                的System.out.println(值+ enteroABuscar +
                        在适当的位置被发现+职位+)\\ n;            System.out.print(
                    写一个数字(-1冲出去了):);
            enteroABuscar = entrada.nextInt();
            的System.out.println();
        }
    }
}


解决方案

首先,这里是与间距原来ArregloBinario类固定起来:

 包laboratorio9;进口了java.util.Random;
进口java.util.Arrays中;公共类ArregloBinario
{
    私人诠释[] DATOS;
    私有静态随机generador =新的随机();    公共ArregloBinario(INT tamanio)
    {
        DATOS =新INT [tamanio]        的for(int i = 0; I< tamanio;我++)
            DATOS [I] = 10 + generador.nextInt(90);        Arrays.sort(DATOS);
    }    公众诠释busquedaBinaria(INT elementoBusqueda)
    {
        INT劣= 0;
        INT优越= datos.length-1;
        INT德尔梅迪奥=(劣+卓越+ 1)/ 2;
        INT ubicacion = -1;        // **我怎样才能改变以下NTO递归函数> **
        做
        {
            System.out.print(elementosRestantes(逊色,上级));            的for(int i = 0; I<德尔梅迪奥;我++)
                System.out.print();
            的System.out.println(*);            如果(elementoBusqueda == DATOS [德尔梅迪奥])
                ubicacion =德尔梅迪奥;
            否则,如果(elementoBusqueda< D​​ATOS [德尔梅迪奥])
                卓越=内 - 1;
            其他
                劣质=德尔梅迪奥+ 1;            德尔梅迪奥=(劣+卓越+ 1)/ 2;
        }而((劣< =卓越)及及(ubicacion == -1));
        返回ubicacion;
    }    公共字符串elementosRestantes(INT逊色,INT上级)
    {
        StringBuilder的时间=新的StringBuilder();        的for(int i = 0; I<劣势;我++)
            temporal.append();        的for(int i =劣; I< =优越;我++)
            temporal.append(DATOS [I] +);        temporal.append(\\ n);
        返回temporal.toString();
    }    公共字符串的toString()
    {
        返回elementosRestantes(0,datos.length-1);
    }
}

这是一个递归版本:

 包laboratorio9;进口了java.util.Random;
进口java.util.Arrays中;公共类ArregloBinario
{
    私人诠释[] DATOS;
    私有静态随机generador =新的随机();    公共ArregloBinario(INT tamanio)
    {
        DATOS =新INT [tamanio]        的for(int i = 0; I< tamanio;我++)
            DATOS [I] = 10 + generador.nextInt(90);        Arrays.sort(DATOS);
    }    私人诠释递归(INT ELEM,诠释天道酬勤,INT SUP,INT MED){
        System.out.print(elementosRestantes(INF,SUP));        的for(int i = 0; I<配有;我++)
            System.out.print();
        的System.out.println(*);        如果(INF> SUP)
            返回-1;        如果(ELEM == DATOS [配有])
            返回MED;        如果(ELEM< D​​ATOS [配有])
            返回递归(ELEM,INF),MED(-1,(INF + MED)/ 2);    返回递归(ELEM),MED(+ 1,SUP,(MED + SUP + 2)/ 2);
    }    公众诠释busquedaBinaria(INT elementoBusqueda)
    {
        INT INF = 0;
        INT SUP = datos.length-1;
        INT MED =(INF + SUP + 1)/ 2;
        INT UBI = -1;        返回递归(elementoBusqueda,INF,SUP,MED);
    }    公共字符串elementosRestantes(INT逊色,INT上级)
    {
        StringBuilder的时间=新的StringBuilder();        的for(int i = 0; I<劣势;我++)
            temporal.append();        的for(int i =劣; I< =优越;我++)
            temporal.append(DATOS [I] +);        temporal.append(\\ n);
        返回temporal.toString();
    }    公共字符串的toString()
    {
        返回elementosRestantes(0,datos.length-1);
    }
}

下面是Eclipse的下样品运行作为密集的测试它经历: - )

 请写出数组中元素的个数。
20
14 18 19 20 22 31 43 50 55 58 58 59 62 71 72 74 85 92 95 98写一个值(-1),以走出去:95
14 18 19 20 22 31 43 50 55 58 58 59 62 71 72 74 85 92 95 98
                               *
                                 59 62 71 72 74 85 92 95 98
                                              *
                                                85 92 95 98
                                                       *
值95在18位被发现。写一个数字(-1出去):-1

So basically what I want to do is a program that asks the user how big he wants his array to be and for the user to introduce the values inside the array. Then I want the user to be able to introduce a number and for the program to determine in which array the number is or determine that it doesn't exist. The following program currently generates the values of the arrays randomly and the user introduces how many array elements he wants to use. But I don't know how to make this in to what I previously explained.

package laboratorio9;

import java.util.Random;
import java.util.Arrays;

public class ArregloBinario
{
    private int[] datos;
    private static Random generador = new Random();

    public ArregloBinario (int tamanio)
    {
        datos = new int[tamanio];

        for (int i=0; i<tamanio; i++)
            datos[i] = 10 + generador.nextInt(90);

        Arrays.sort(datos);
    }

    public int busquedaBinaria(int elementoBusqueda)
    {
        int inferior = 0;
        int superior = datos.length-1;
        int medio = (inferior + superior + 1 ) / 2;
        int ubicacion = -1;

        // **HOW CAN I CHANGE THE FOLLOWING NTO A RECURSIVE FUNCTION>**
        do 
        {
            System.out.print(elementosRestantes(inferior,superior));

            for (int i = 0; i<medio; i++)
                System.out.print("  ");
            System.out.println(" * ");

            if (elementoBusqueda == datos[medio])
                ubicacion=medio;
            else if (elementoBusqueda<datos[medio])
                superior = medio-1;
            else 
                inferior = medio+1;

            medio = (inferior + superior + 1) / 2;
        } while ((inferior <=superior) && (ubicacion == -1));
        return ubicacion;                
    }

    public String elementosRestantes(int inferior, int superior)
    {
        StringBuilder temporal = new StringBuilder();

        for (int i = 0; i < inferior; i++)
            temporal.append( "  " );

        for (int i = inferior; i <= superior; i++)
            temporal.append( datos[i] + " ");

        temporal.append("\n");
        return temporal.toString();
    }

    public String toString()
    {
        return elementosRestantes(0, datos.length-1);
    }
}

// MAIN CLASS //

package laboratorio9;

import java.util.Scanner;

public class PruebaBusquedaBinaria {
    public static void main(String[] args)
    {
        Scanner entrada = new Scanner(System.in);

        int enteroABuscar;
        int posicion;

        System.out.println("Please write the number of elements in the array.");
        int number = entrada.nextInt();

        ArregloBinario arregloBusqueda = new ArregloBinario(number);
        System.out.println(arregloBusqueda);

        System.out.print("Write a value (-1) to go out: ");
        enteroABuscar = entrada.nextInt();
        System.out.println();

        while (enteroABuscar != -1)
        {
            posicion = arregloBusqueda.busquedaBinaria(enteroABuscar);

            if (posicion==-1)
                System.out.println("The value " + enteroABuscar + " was not found.\n");
            else
                System.out.println("The value " + enteroABuscar +
                        " was found in position " + posicion + ".\n");

            System.out.print(
                    "Write a number (-1 to go out): ");
            enteroABuscar = entrada.nextInt();
            System.out.println();
        }
    }
}

解决方案

For a start, here is the original ArregloBinario class with the spacing fixed up:

package laboratorio9;

import java.util.Random;
import java.util.Arrays;

public class ArregloBinario
{
    private int[] datos;
    private static Random generador = new Random();

    public ArregloBinario (int tamanio)
    {
        datos = new int[tamanio];

        for (int i=0; i<tamanio; i++)
            datos[i] = 10 + generador.nextInt(90);

        Arrays.sort(datos);
    }

    public int busquedaBinaria(int elementoBusqueda)
    {
        int inferior = 0;
        int superior = datos.length-1;
        int medio = (inferior + superior + 1 ) / 2;
        int ubicacion = -1;

        // **HOW CAN I CHANGE THE FOLLOWING NTO A RECURSIVE FUNCTION>**
        do 
        {
            System.out.print(elementosRestantes(inferior,superior));

            for (int i = 0; i<medio; i++)
                System.out.print("   ");
            System.out.println(" * ");

            if (elementoBusqueda == datos[medio])
                ubicacion=medio;
            else if (elementoBusqueda<datos[medio])
                superior = medio-1;
            else 
                inferior = medio+1;

            medio = (inferior + superior + 1) / 2;
        } while ((inferior <=superior) && (ubicacion == -1));
        return ubicacion;                
    }

    public String elementosRestantes(int inferior, int superior)
    {
        StringBuilder temporal = new StringBuilder();

        for (int i = 0; i < inferior; i++)
            temporal.append( "   " );

        for (int i = inferior; i <= superior; i++)
            temporal.append( datos[i] + " ");

        temporal.append("\n");
        return temporal.toString();
    }

    public String toString()
    {
        return elementosRestantes(0, datos.length-1);
    }
}

And here's a recursive version:

package laboratorio9;

import java.util.Random;
import java.util.Arrays;

public class ArregloBinario
{
    private int[] datos;
    private static Random generador = new Random();

    public ArregloBinario (int tamanio)
    {
        datos = new int[tamanio];

        for (int i=0; i<tamanio; i++)
            datos[i] = 10 + generador.nextInt(90);

        Arrays.sort(datos);
    }

    private int recursive (int elem, int inf, int sup, int med) {
        System.out.print(elementosRestantes(inf,sup));

        for (int i = 0; i<med; i++)
            System.out.print("   ");
        System.out.println(" * ");

        if (inf > sup)
            return -1;

        if (elem == datos[med])
            return med;

        if (elem<datos[med])
            return recursive (elem,inf,med-1,(inf + med) / 2);

    return recursive (elem,med+1,sup,(med + sup + 2) / 2);
    }

    public int busquedaBinaria(int elementoBusqueda)
    {
        int inf = 0;
        int sup = datos.length-1;
        int med = (inf + sup + 1 ) / 2;
        int ubi = -1;

        return recursive (elementoBusqueda,inf,sup,med);
    }

    public String elementosRestantes(int inferior, int superior)
    {
        StringBuilder temporal = new StringBuilder();

        for (int i = 0; i < inferior; i++)
            temporal.append( "   " );

        for (int i = inferior; i <= superior; i++)
            temporal.append( datos[i] + " ");

        temporal.append("\n");
        return temporal.toString();
    }

    public String toString()
    {
        return elementosRestantes(0, datos.length-1);
    }
}

Here's a sample run under Eclipse as the intensive testing it underwent :-)

Please write the number of elements in the array.
20
14 18 19 20 22 31 43 50 55 58 58 59 62 71 72 74 85 92 95 98 

Write a value (-1) to go out: 95
14 18 19 20 22 31 43 50 55 58 58 59 62 71 72 74 85 92 95 98 
                               * 
                                 59 62 71 72 74 85 92 95 98 
                                              * 
                                                85 92 95 98 
                                                       * 
The value 95 was found in position 18.

Write a number (-1 to go out): -1

这篇关于我该怎么做我的功课与数组,函数和二进制搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆