|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Arrays and Data-Structures > ARS. - Array's > !ARS. - Array Sorting > ARS. - Array Operations |
MiniRobotLanguage (MRL)
ARS.BinSearch
Performs a binary search for a value in a sorted array and optionally stores the result in a variable.
Intention
The ARS.BinSearch command performs a binary search on a sorted array to find a specific value. If the value is found, its index is optionally stored in a variable. If the value is not found, the index where the value should be inserted to maintain the sorted order is returned.
🔍 Searching for value in array:
🔢 [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
🔍 Value to find: 11
🔍 Binary search steps:
1. Midpoint: 9 (not found)
2. Midpoint: 13 (not found)
3. Midpoint: 11 (found)
🔢 Index of 11: 5
Syntax
ARS.BinSearch|$$ARS|$$VAL[|$$IND]
Parameter Explanation
P1 - $$ARS - (Variable)
The handle of the sorted array.
P2 - $$VAL - (Variable)
The value to search for in the array.
P3 - $$IND (optional) - (Variable)
The variable to store the index of the found value or the insertion point.
Example
'***********************************
' ARS.BinSearch - Sample 1
'***********************************
ARS.New|$$ARS
ARS.Add|$$ARS|1
ARS.Add|$$ARS|3
ARS.Add|$$ARS|5
ARS.Add|$$ARS|7
ARS.BinSearch|$$ARS|5|$$IND
DBP.New Found at index: $$IND
'
'***********************************
' ARS.BinSearch - Sample 2
'***********************************
ARS.New|$$ARS
ARS.Add|$$ARS|2
ARS.Add|$$ARS|4
ARS.Add|$$ARS|6
ARS.Add|$$ARS|8
ARS.BinSearch|$$ARS|4|$$IND
DBP.New Found at index: $$IND
'
'***********************************
' ARS.BinSearch - Sample 3
'***********************************
ARS.New|$$ARS
ARS.Add|$$ARS|10
ARS.Add|$$ARS|20
ARS.Add|$$ARS|30
ARS.Add|$$ARS|40
ARS.BinSearch|$$ARS|25|$$IND
DBP.New Insertion point: $$IND
'
Remarks
-
Limitations:
-
See also:
•