이진찾기

#include <iostream>
#include <iomanip>
using namespace std;

bool bi_Iterative(int list[], int end, int target, int & locn);

bool bi_Recursive();

const int SIZE=100;

int main(void)
{
 int list[SIZE];

 int srchArgu;
 int foundLocn;

 //배열 채우기

 for(int i=0; i < SIZE; i++)
 {
  list[i]=2*i;
 }
 
 for(int i=0; i < SIZE; i++)
 {
  if(i%10==0)
  {
   cout<<endl;
  }
  else
  {
   cout<<setw(4)<<list[i];
  }
 }

 cout<<endl<<"원하는 숫자입력:";
 cin>>srchArgu;

 bool res=bi_Iterative(list, SIZE-1,srchArgu,foundLocn);
 
 if(res ==true)
 {
  cout<<"데이터 찾음"<<list[foundLocn]<<endl;
 }
 else
 {
  cout<<srchArgu<<"데이터를 못찾음"<< endl;
 }
}

bool bi_Iterative(int list[], int end, int target, int & locn)
{
 int first=0, mid, last=end;
 

 while(first<=last)
 {
  mid=(first+last)/2;
  if(target>list[mid])
  {
   first=mid+1;
  }
  else if(target<list[mid])
  {
   last=mid-1;
  }
  else
  {
   break;
  }
 }

 locn=mid;
 
 return (target== list[mid]);

}

 

 

 

'1. IT Story > Development' 카테고리의 다른 글

링크드리스트ADT를 이용한 학생성적관리 Pro  (0) 2012.03.29
insertionSort  (0) 2012.03.28
Heap Tree (보기좋게출력)  (0) 2012.03.28
하노이타워  (0) 2012.03.28
피보나치  (0) 2012.03.28
윈도우폰 게임개발 기초(WindowPhoneGame)  (0) 2012.03.07
C# 채팅 프로그램(Client 확장)  (0) 2012.03.07
C# 채팅 프로그램(Server 확장)  (4) 2012.03.07
블로그 이미지

운명을바꾸는자

IT와 함께 살아가는 삶

,