Last active
August 23, 2021 05:51
-
-
Save SajidK25/ee1a1d15fe0932487f794bc0496e4632 to your computer and use it in GitHub Desktop.
Binary Search Implementation using Dart programming language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int binarySearch(L,left,right,x){ | |
if(left>right){ | |
return -1; | |
} | |
int mid=(left+right)~/2; | |
if(L[mid]==x){ | |
return mid; | |
} | |
if(L[mid]<x){ | |
return binarySearch(L,mid+1,right,x); | |
} | |
else{ | |
return binarySearch(L,left,mid-1,x); | |
} | |
} | |
void main() { | |
var L=[1,2,5,6,9,10,34,43,46,50,77,81,87,90,95,101,340,430,460,500]; | |
var left=0; | |
var right=L.length-1; | |
var x=46; | |
var result=binarySearch(L,left,right,x); | |
print('${x} found in index of ${result}'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment