Arama Algoritmaları Kod Örnekleri

Arama algoritmaları
-Sıralyla arama
-İkili Arama

 

1-Sırayla Arama Selection Search

[csharp]
boolean <b>linearSearch</b>(int key){
for(int i=0; i < size; i++){
if(key == arr[i]) return true;
}
return false;
} [/csharp]

2-İkili Arama Binary Search

[csharp]boolean <b>binarySearch</b>(int key){
int first,last,mid = 0;
boolean found = false;
first=0; last=size-1;
while((!found) && (first <= last)){
mid=first+(last-first)/2;
if(arr[mid]==key) found=true;
else if(key < arr[mid]) last=mid-1;
else if(key > arr[mid]) first=mid+1;
}
return found;
}[/csharp]

Yorum yapın