Java 练习使用二进制搜索将数据插入数组,没有什么问题
我正在尝试创建一个方法,该方法插入元素,然后以二进制形式对元素进行排序 我遇到的问题是,我的代码没有正确插入数据,这意味着输出看起来根本不正常 列表没有组织,数据是按插入顺序添加的 现在,两个问题,我做错了什么?如何解决这个问题Java 练习使用二进制搜索将数据插入数组,没有什么问题,java,arrays,insert,binary-search,Java,Arrays,Insert,Binary Search,我正在尝试创建一个方法,该方法插入元素,然后以二进制形式对元素进行排序 我遇到的问题是,我的代码没有正确插入数据,这意味着输出看起来根本不正常 列表没有组织,数据是按插入顺序添加的 现在,两个问题,我做错了什么?如何解决这个问题 public void insertBinarySearch(long value) // put element into array { int j = 0; int lower = 0; int upper = elems-1
public void insertBinarySearch(long value) // put element into array
{
int j = 0;
int lower = 0;
int upper = elems-1;
int cur = 0;
while (cur < elems)
{
cur = (lower + upper ) / 2;
if(a[cur] < value)
{
j = cur + 1;
break;
}
else if(a[cur] > value)
{
j = cur;
break;
}
else
{
if(a[cur] < value)
lower = cur + 1;
else
upper = cur - 1;
}
}
for(int k = elems; k > j; k--)
a[k] = a[k-1];
a[j] = value;
elems++;
}
public void insertBinarySearch(长值)//将元素放入数组
{
int j=0;
整数下限=0;
int上限=elems-1;
int cur=0;
while(cur值)
{
j=电流;
打破
}
其他的
{
if(a[cur]<值)
下限=电流+1;
其他的
上限=cur-1;
}
}
对于(int k=elems;k>j;k--)
a[k]=a[k-1];
a[j]=值;
elems++;
}
while(较低值)
上限=cur-1;
else if(a[curIn]==值)
打破
}
if(a[curIn]while(较低值)
上限=cur-1;
else if(a[curIn]==值)
打破
}
如果(a[curIn]如果您试图将一个项目插入未排序的列表中,如果之后不直接对其排序,则无法期望该列表被排序
二进制搜索只能在已排序的列表上使用。您的结果在其他方面没有任何意义:毕竟,如果您正在搜索一个元素(例如,让我们使用8),您如何知道一个项目属于8之前还是之后
[ 1 2 3 4 5 6 7 8 9 10 11 ]
注意,如果我找到8,我知道它之后的所有值都大于8,之前的所有值都小于8。但是这个列表呢
[ 1 4 11 9 8 6 7 2 5 3 10 ]
Yikes!现在,看看8,你可以看到8的位置与小于或大于8的项目没有任何关系。尝试在该列表中使用二进制搜索查找元素会给你错误的信息,无论它是否找到你要查找的项目
要使其正常工作,请确保列表始终已排序(如果始终将项目插入列表中的正确位置,则应如此),或在插入新元素之前对其进行排序。此时,二进制搜索将很好地为您服务。如果您试图将某个项目插入未排序的列表中,如果之后不直接对其进行排序,则无法对列表进行排序
二进制搜索只能在已排序的列表上使用。您的结果在其他方面没有任何意义:毕竟,如果您正在搜索一个元素(例如,让我们使用8),您如何知道一个项目属于8之前还是之后
[ 1 2 3 4 5 6 7 8 9 10 11 ]
注意,如果我找到8,我知道它之后的所有值都大于8,之前的所有值都小于8。但是这个列表呢
[ 1 4 11 9 8 6 7 2 5 3 10 ]
Yikes!现在,看看8,你可以看到8的位置与小于或大于8的项目没有任何关系。尝试在该列表中使用二进制搜索查找元素会给你错误的信息,无论它是否找到你要查找的项目
要使其正常工作,请确保列表始终已排序(如果您总是将项目插入列表中的正确位置,则应如此),或者在插入新元素之前对其进行排序。二进制搜索将很好地满足您的需要。Dude,您几乎得到了它,只需调试代码并找到一些缺少的语句(如果有).如果你找到那些缺失的语句,你的代码看起来比我的好。别看我的,你就在那里,非常接近
让我知道任何错误和差异等快乐编码
public class Insertion {
private int[] a;
int n;
int c;
public Insertion()
{
a = new int[10];
n=0;
}
int find(int key)
{
int lowerbound = 0;
int upperbound = n-1;
while(true)
{
c = (lowerbound + upperbound)/2;
if(n==0)
return 0;
if(lowerbound>=upperbound)
{
if(a[c]<key)
return c++;
else
return c;
}
if(a[c]>key && a[c-1]<key)
return c;
else if (a[c]<key && a[c+1]>key)
return c++;
else
{
if(a[c]>key)
upperbound = c-1;
else
lowerbound = c+1;
}
}
}
void insert(int key)
{
find(key);
for(int k=n;k>c;k--)
{
a[k]=a[k-1];
}
a[c]=key;
n++;
}
void display()
{
for(int i=0;i<10;i++)
{
System.out.println(a[i]);
}
}
public static void main(String[] args)
{
Insertion i=new Insertion();
i.insert(56);
i.insert(1);
i.insert(78);
i.insert(3);
i.insert(4);
i.insert(200);
i.insert(6);
i.insert(7);
i.insert(1000);
i.insert(9);
i.display();
}
}
公共类插入{
私人机构【】a;
int n;
INTC;
公共插入()
{
a=新整数[10];
n=0;
}
整数查找(整数键)
{
int lowerbound=0;
int上限=n-1;
while(true)
{
c=(下限+上限)/2;
如果(n==0)
返回0;
如果(下限>=上限)
{
if(一个[c]键和一个[c-1]键)
上限=c-1;
其他的
lowerbound=c+1;
}
}
}
无效插入(整数键)
{
查找(键);
对于(int k=n;k>c;k--)
{
a[k]=a[k-1];
}
a[c]=键;
n++;
}
无效显示()
{
对于(inti=0;iDude,你几乎成功了,只需调试代码并找到一些缺少的语句(if)。如果你找到那些缺少的语句,你的代码看起来比我的好。不要看我的,你就在那里,非常接近了
让我知道任何错误和差异等快乐编码
public class Insertion {
private int[] a;
int n;
int c;
public Insertion()
{
a = new int[10];
n=0;
}
int find(int key)
{
int lowerbound = 0;
int upperbound = n-1;
while(true)
{
c = (lowerbound + upperbound)/2;
if(n==0)
return 0;
if(lowerbound>=upperbound)
{
if(a[c]<key)
return c++;
else
return c;
}
if(a[c]>key && a[c-1]<key)
return c;
else if (a[c]<key && a[c+1]>key)
return c++;
else
{
if(a[c]>key)
upperbound = c-1;
else
lowerbound = c+1;
}
}
}
void insert(int key)
{
find(key);
for(int k=n;k>c;k--)
{
a[k]=a[k-1];
}
a[c]=key;
n++;
}
void display()
{
for(int i=0;i<10;i++)
{
System.out.println(a[i]);
}
}
public static void main(String[] args)
{
Insertion i=new Insertion();
i.insert(56);
i.insert(1);
i.insert(78);
i.insert(3);
i.insert(4);
i.insert(200);
i.insert(6);
i.insert(7);
i.insert(1000);
i.insert(9);
i.display();
}
}
公共类插入{
私人机构【】a;
int n;
INTC;
公共插入()
{
a=新整数[10];
n=0;
}
整数查找(整数键)
{
int lowerbound=0;
int上限=n-1;
while(true)
{
c=(下限+上限)/2;
如果(n==0)
返回0;
如果(下限>=上限)
{
if(一个[c]键和一个[c-1]键)
上限=c-1;
其他的
lowerbound=c+1;
}
}
}
无效插入(整数键)
{
查找(键);
对于(int k=n;k>c;k--)
{
a[k]=a[k-1];
}
a[c]=键;
n++;
}
无效显示()
{
对于(int i=0;ii)如果要添加大量元素,然后遍历,最好只添加项,然后根据需要进行排序。这似乎涉及大量的移位。我不同意这一点。因为我们可以先组织元素,因此不需要创建额外的循环,这将增加此方法的计算时间(如果该循环有数百万个元素呢