英文:
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<dizi.length-1;o++)
{
for(p=0;p<dizi.length-1-o;p++)
{
if(dizi>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<=ust)
{
int orta=(alt+ust)/2;
if(dizi[orta]==aranan){
return orta;
}
else if (orta<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("Enter array lenght");
int x=tara.nextInt();
Random rnd= new Random();
int i;
int a[]=new int[x];
for(i=0;i<x;i++)
{
a[i]=(1+rnd.nextInt(6));
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println("Enter targeted number :");
int k,z;
k=tara.nextInt();
bubblesort(a);
z=Binarysearch2.search(a,k);
if(z==-1)
{System.out.println("Target is not in array.");}
else
System.out.println(k+" in array "+z +" . at this index.");
}}
i think something in binarysearch is wrong.
答案1
得分: 2
这里有一个问题:
if (orta<aranan)
{
alt=orta+1;
}
与其将值(dizi[orta]
)与目标(aranan
)进行比较,你实际上比较的是索引。只需将条件替换为 if (dizi[orta]<aranan)
此外,正如安纳托利在他的答案中正确指出的,你的排序方向是错误的。你的冒泡排序在较小的元素在较大的元素之前时交换元素( >diziif(dizi
)。这意味着你是在降序中进行排序。另一方面,你的搜索假设数组是按升序排序的。
英文:
There's a problem here:
if (orta<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]<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 ( >diziif(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 <= ust) {
int orta = (alt + ust) / 2;
if (dizi[orta] == aranan) {
return orta;
} else if (orta < aranan) {
alt = orta + 1;
} else
ust = orta - 1;
}
return -1;
}
答案3
得分: 1
- 在
bubblesort
中,将dizi
更改为> dizi
dizi
。< dizi
- 在
search
中,将else if (orta < aranan)
更改为else if (dizi[orta] < aranan)
。
英文:
- Array sorting in wrong direction. In
bubblesort
changedizi
on> dizi
dizi
< dizi
- In
search
changeelse if (orta < aranan)
onelse if (dizi[orta] < aranan)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论