在我的程序中,我使用二进制搜索来查找元素应该属于的位置,将其放在正确的位置,并将元素在一个空格中向右移动,以便仍然对数组进行排序。我可以找到它属于我的二进制文件的位置,但是我无法将它放在正确的位置并移动其他元素。元素一次一个地从文本文件中读取(按顺序插入),因此理想情况下它将表现得像:17来 - > [17,..],65来 - > [17,65,..],20来 - > [17,20,65,..]等我的输出是完全错误的。我的代码输出为:41 55 48 34 84 78 89 94 61 108 74 76 97 62 121 119 132 110 144 156 160 146 164 170 75这完全出现故障:(
这是我的代码:
static void insertInOrder( int[] arr, int cnt, int newVal )
{
//arr is assumed to be big enough for values + it's an empty array
int binaryIndex = bSearch(arr,cnt,newVal); //returns a negative value if not duplicate
int positiveIndex = (-(binaryIndex))-1; //transforms binaryIndex into a positive value of the correct index where number belongs
for (int i = arr.length-1;i>=positiveIndex;i--){
if (i<=0)break;
arr[i]=arr[i-1];
}
arr[positiveIndex]=newVal;
}
这是我的bSearch:
public static int bSearch(int[] a, int cnt, int key)
{
int high = cnt-1;
int low = 0;
int mid = (high+low)/2;
while (low <= high) {
if (key==a[mid]){
return mid;
}
else if (key < a[mid]){
high = mid-1;
mid = (high+low)/2;
}
else {
low = mid +1;
mid = (high+low)/2;
}
}
return -(mid+1);
}
答案 0 :(得分:0)
您的for
- 循环必须在i>binaryIndex
处中断,否则您将目标位置左侧一个位置的元素移动到之后放置新元素的目标位置。
答案 1 :(得分:0)
我修改了一些代码以获得正确的输出。请找到以下代码: -
public class StackOverflow {
public static void main(String args[]) {
int[] intArray = new int[10];
// Just to try the case I am passing the hard-coded value
insertInOrder(intArray,lengthArray(intArray),50);
insertInOrder(intArray,lengthArray(intArray),60);
insertInOrder(intArray,lengthArray(intArray),55);
insertInOrder(intArray,lengthArray(intArray),50);
//Displaying the sorted array output
for(int intA : intArray) {
System.out.println("Sorted intResultArray" + intA);
}
}
// To return the exact count of element in array. I hope you are not inserting 0 as a value because I have use default value. The reason to do it is that default length property of ARRAY will return the actual size of array
// to get the exact count of element
public static int lengthArray(int[] intArrLen) {
int count = 0;
for (int i = 0;i < intArrLen.length - 1;i++) {
if(intArrLen[i] != 0) {
count++;
}
}
return count;
}
public static void insertInOrder(int[] arr, int count, int newVal ) // Here 'count' refers to exact count of element in array
{
int binaryIndex = bSearch(arr,count,newVal);
if (arr[arr.length - 1] == 0) { // I have added to ensure that there is enough space to move element further down the array
for (int i = lengthArray(arr);i > binaryIndex;i--) {
arr[i]=arr[i-1];
}
} else {
System.out.println("There is no space to move element in array");
}
arr[binaryIndex]=newVal;
}
public static int bSearch(int[] a, int cnt, int key) {
int high = cnt-1;
int low = 0;
int mid = 0;
if(high == -1) {
return low;
} else {
mid = (high+low)/2;
}
while (low <= high) {
if (key==a[mid]){
return (mid + 1); // I am returning 'mid + 1' in case if number already exist
}
else if (key < a[mid]){
high = mid-1;
mid = (high+low)/2;
}
else {
low = mid +1;
mid = (high+low)/2;
}
}
return (mid+1);
}
}
输出: -
Sorted intResultArray50
Sorted intResultArray50
Sorted intResultArray55
Sorted intResultArray60
Sorted intResultArray0
Sorted intResultArray0
Sorted intResultArray0
Sorted intResultArray0
Sorted intResultArray0
Sorted intResultArray0
答案 2 :(得分:0)
而不是返回 - (mid + 1),我需要返回 - (低+ 1)