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).

static int intArrayBinarySearchWithComparator(int[] a, IntComparator comparator, int key) {
  ret intArrayBinarySearchWithComparator(a, 0, a.length, comparator, key);
}

static int intArrayBinarySearchWithComparator(int[] a, int fromIndex, int toIndex, IntComparator comparator, int key) {
  int low = fromIndex;
  int high = toIndex - 1;

  while (low <= high) {
    int mid = (low + high) >>> 1;
    int midVal = a[mid];

    int cmp = comparator.compare(midVal, key);
    if (cmp < 0)
        low = mid + 1;
    else if (cmp > 0)
        high = mid - 1;
    else
        ret mid; // key found
  }
  ret -(low + 1);  // key not found.
}

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: 130 / 203
Version history: 1 change(s)
Referenced in: [show references]