Sunday 15 June 2014

java - Binary search and insert -


I stumbled over a problem with my coding and I can not see where it is wrong.

I am working on a program where I need to use binary search, after which an inserted item should be sorted after execution, but this is not the case at this time.

My code is as follows:

  // array 103, 102, 101, 105, 106 public entry entry (int ITID) {int [] newItems = new Int [ Items.length + 1]; ItemPos = Arrays.binarySearch (item, 0, item number, item id); If (item POS & lt; 0) {itemPos = -imemos- 1; } System. Arrayopy (item, 0, new item, 0, item length); Item = new item; Item [item grid + 1] = eyecide; ItemsLength ++; System.out.println ("Item Status:" + ItemPoice); System.out.println ("Items:" + Arrays.toString (item)); System.out.println ("item length:" + objects lang); Return items POS; }  

Now when I run my code the output is:

  Item status: 0 items: [103, 102] Item Lelentity: 1 item Item: [103, 101, 0, 105, 106] Item: [103, 101, 0] Item Length: 2 Item Condition: 2 Items: [103, 101, 0, 105] Item Length: 3 items Status: 3 items:  

The only problem I have is that the array is not resolved (and it returns zero when it is 102, but maybe this is the same sorting problem is). I appreciate it very much if someone tells me in the right direction.

Greetings,

Alfred

You may want Find out how binary search works You will find that work for it, items are being searched should be sorted This is the place that misleads the inserting position.


No comments:

Post a Comment