2014-02-13 61 views
0

在我的程序中,我使用二進制搜索來查找元素應該屬於的位置,將它放在正確的位置並將元素向右移動一個空格,以便數組仍然是有序的。我可以在我的二進制文件中找到它所屬的位置,但是我很難將它放在正確的位置並且改變其他元素。元素是從一個文本文件中一次讀取(按順序插入),所以理想情況下它的行爲如下: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

很難如果沒有實施bSearch,告訴發生了什麼問題。其他代碼似乎相當合理。 – gregkow

+3

什麼是'bSearch'?特別是,如果價值不存在,它會返回什麼? –

+0

bSearch是一個返回元素應該放在數組中的位置的方法。它基本上是一個修改的二進制搜索。 – ComicStix

回答

0

,而不是返回的 - (MID + 1),我需要回到 - (低+ 1)

0

for -loop具有i>binaryIndex打破,否則您將它留在目標位置到目標位置的一個地方,你的新元素被放置在元素之後,

+0

在改變break語句之後,我得到了這個輸出:94 121 144 0 146 78 119 160 164 170 0 0 0 74 84 0 0 0 0 48 55 76 97 0 0:/ – ComicStix

+0

我假設'positiveIndex'應該由'binaryIndex'這是一個錯字?另外請檢查'bSearch'算法。 – Smutje

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 
+0

感謝您的代碼!但我解決了我的問題。我本來應該使用low而不是mid作爲我的bSearch。 – ComicStix