1.BinarySearch - C++
1.BinarySearch - C++
#include <iostream>
using namespace std;
int main()
{
// 1~500까지의 배열 형성
int arr[501];
for(int i=1;i<501;i++)
arr[i]=i;
int target = 62; // 타겟 값
int left = 0, right = sizeof(arr)/sizeof(int) - 1 ; // left, right 초기화
cout << sizeof(int) ;
// 이분 탐색 수행
while(left<=right)
{
int mid = (left+right)/2 ; // mid 갱신
//cout << mid <<'\n';
if(mid==target)
{
cout<<"Searching Complete! = " << target <<'\n';
break;
}
else if(mid>target)
{
right = mid-1;
}
else
{
left = mid+1;
}
}
return 0;
}
Leave a comment