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

22
LINES

< > BotCompany Repo | #1029041 // intArrayBinarySearch

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

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

// Like Arrays.binarySearch, but without range checks.
static int intArrayBinarySearch(int[] a, int key) {
  ret intArrayBinarySearch(a, 0, a.length, key);
}

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

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

    if (midVal < key)
        low = mid + 1;
    else if (midVal > key)
        high = mid - 1;
    else
        return mid; // key found
  }
  return -(low + 1);  // key not found.
}

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: #1029041
Snippet name: intArrayBinarySearch
Eternal ID of this version: #1029041/4
Text MD5: bd35c1c2f1ae3a04cb1163fe4239ae82
Transpilation MD5: b1cb705896a664ef5be4475388b27df6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-17 14:13:39
Source code size: 579 bytes / 22 lines
Pitched / IR pitched: No / No
Views / Downloads: 135 / 216
Version history: 3 change(s)
Referenced in: [show references]