有些问题涉及二分搜索,有人能看看吗?

huangapple go评论75阅读模式
英文:

Having some problems with binary search can someone look to it?

问题

以下是您要求的翻译好的内容:

import java.util.*;

public class Binarysearch2 {

    static void bubblesort(int array[]) {
        int i, j, temp;
        for (i = 0; i < array.length - 1; i++) {
            for (j = 0; j < array.length - 1 - i; j++) {
                if (array[j + 1] > array[j]) {
                    temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

    static int search(int array[], int target) {
        int upper = array.length - 1;
        int lower = 0;

        while (lower <= upper) {
            int middle = (lower + upper) / 2;
            if (array[middle] == target) {
                return middle;
            } else if (array[middle] < target) {
                lower = middle + 1;
            } else {
                upper = middle - 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入数组长度:");
        int length = scanner.nextInt();
        Random random = new Random();
        int i;
        int array[] = new int[length];
        for (i = 0; i < length; i++) {
            array[i] = (1 + random.nextInt(6));
            System.out.print(array[i] + " ");
        }
        System.out.println();
        System.out.println("输入目标数字:");
        int target, result;
        target = scanner.nextInt();
        bubblesort(array);
        result = Binarysearch2.search(array, target);

        if (result == -1) {
            System.out.println("目标不在数组中。");
        } else {
            System.out.println(target + " 在数组中,位于索引 " + result + " 处。");
        }
    }
}

如果您认为二分查找中有问题,请检查 if (orta < aranan) 这一行,可能应该是 if (dizi[orta] < aranan)

英文:

Binary search and bubblesort problem. Cant understand the problem with binary search pseudocode.What do i need to change in order to get the index of targeted number?

import java.util.*;
public class Binarysearch2 {
static void bubblesort(int dizi[]){  // bubblesort //
int o,p,temp;
for(o=0;o&lt;dizi.length-1;o++)
{
for(p=0;p&lt;dizi.length-1-o;p++)
{
if(dizi

&gt;dizi

){ temp=dizi

; dizi

=dizi

; dizi

=temp; } } } } static int search(int dizi[],int aranan) // binary search// { int ust=dizi.length-1; int alt=0; while(alt&lt;=ust) { int orta=(alt+ust)/2; if(dizi[orta]==aranan){ return orta; } else if (orta&lt;aranan) { alt=orta+1; } else ust=orta-1; } return -1; } public static void main(String[]args) //main// { Scanner tara=new Scanner(System.in); System.out.println(&quot;Enter array lenght&quot;); int x=tara.nextInt(); Random rnd= new Random(); int i; int a[]=new int[x]; for(i=0;i&lt;x;i++) { a[i]=(1+rnd.nextInt(6)); System.out.print(a[i]+&quot; &quot;); } System.out.println(); System.out.println(&quot;Enter targeted number :&quot;); int k,z; k=tara.nextInt(); bubblesort(a); z=Binarysearch2.search(a,k); if(z==-1) {System.out.println(&quot;Target is not in array.&quot;);} else System.out.println(k+&quot; in array &quot;+z +&quot; . at this index.&quot;); }}

i think something in binarysearch is wrong.

答案1

得分: 2

这里有一个问题:

if (orta<aranan)
{
    alt=orta+1;
}

与其将值(dizi[orta])与目标(aranan)进行比较,你实际上比较的是索引。只需将条件替换为 if (dizi[orta]<aranan)

此外,正如安纳托利在他的答案中正确指出的,你的排序方向是错误的。你的冒泡排序在较小的元素在较大的元素之前时交换元素(if(dizi

>dizi

)。这意味着你是在降序中进行排序。另一方面,你的搜索假设数组是按升序排序的。

英文:

There's a problem here:

if (orta&lt;aranan)
{
alt=orta+1;
}

Instead of comparing the value (dizi[orta]) to the target (aranan), you're comparing the index. Just replace the condition with if (dizi[orta]&lt;aranan)

Also, as Anatoly points out correctly in his answer, you're sorting in the wrong direction. Your bubble sort swaps elements when the lower one is smaller than the bigger one (if(dizi

&gt;dizi

) . That means that you're sorting in descending order. On the other hand, your search assumes that the array is sorted in ascending order.

答案2

得分: 1

Good day,
It would be really helpful if you'll be using English to name variables. Binary search makes an important assumption, that the array where you are seeking an element has been sorted. So, if you were to add sorting into the search method as follows, your code will work:

    static int search(int array[], int target) {
        int high = array.length - 1;
        int low = 0;

        Arrays.sort(array); // ADDED ELEMENT

        while (low <= high) {
            int mid = (low + high) / 2;
            if (array[mid] == target) {
                return mid;
            } else if (array[mid] < target) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return -1;
    }
英文:

Good day,
It would be really helpful if you'll be using english to name variables. Binary search making an important assumption, that array, where you are seeking for an element, has been sorted. So, if you would add sorting into search method as such, your code will work:

    static int search(int dizi[], int aranan) {
int ust = dizi.length - 1;
int alt = 0;
Arrays.sort(dizi); // ADDED ELEMENT
while (alt &lt;= ust) {
int orta = (alt + ust) / 2;
if (dizi[orta] == aranan) {
return orta;
} else if (orta &lt; aranan) {
alt = orta + 1;
} else
ust = orta - 1;
}
return -1;
}

答案3

得分: 1

  1. bubblesort 中,将 dizi

    > dizi

    更改为 dizi

    < dizi

  2. search 中,将 else if (orta < aranan) 更改为 else if (dizi[orta] < aranan)
英文:
  1. Array sorting in wrong direction. In bubblesort change dizi

    &gt; dizi

    on dizi

    &lt; dizi

  2. In search change else if (orta &lt; aranan) on else if (dizi[orta] &lt; aranan)

huangapple
  • 本文由 发表于 2020年8月27日 22:42:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63618490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定