Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

22
LINES

< > BotCompany Repo | #1029419 // intArrayBinarySearchWithComparator

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (52L/1K).

1  
static int intArrayBinarySearchWithComparator(int[] a, IntComparator comparator, int key) {
2  
  ret intArrayBinarySearchWithComparator(a, 0, a.length, comparator, key);
3  
}
4  
5  
static int intArrayBinarySearchWithComparator(int[] a, int fromIndex, int toIndex, IntComparator comparator, int key) {
6  
  int low = fromIndex;
7  
  int high = toIndex - 1;
8  
9  
  while (low <= high) {
10  
    int mid = (low + high) >>> 1;
11  
    int midVal = a[mid];
12  
13  
    int cmp = comparator.compare(midVal, key);
14  
    if (cmp < 0)
15  
        low = mid + 1;
16  
    else if (cmp > 0)
17  
        high = mid - 1;
18  
    else
19  
        ret mid; // key found
20  
  }
21  
  ret -(low + 1);  // key not found.
22  
}

Author comment

Began life as a copy of #1029041

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1029419
Snippet name: intArrayBinarySearchWithComparator
Eternal ID of this version: #1029419/2
Text MD5: 5c245b093ad5f5425ebf45d8d0dce5ff
Transpilation MD5: 8e1d49b31bdd3b864242fee4ff2b237e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-05 19:00:24
Source code size: 661 bytes / 22 lines
Pitched / IR pitched: No / No
Views / Downloads: 204 / 298
Version history: 1 change(s)
Referenced in: [show references]