-
[Algorithm] - 선형 탐색(Linear Search)Data Structure, Algorithm 2016. 3. 5. 12:50
#include <iostream> using std::cout; using std::endl; int LinearSearch(int arr[], int len, int target) { for (int i = 0; i < len; i++) { if (target == arr[i]) { return i; } } return -1; } int main(void) { int arr[] = { 3, 5, 2, 4, 9 }; int index = LinearSearch(arr, sizeof(arr) ⁄ sizeof(arr[0]), 4); if (index == -1) { cout << "Search Failed" << endl; } else { cout << "Target Save Index : " << index << endl; } index = LinearSearch(arr, sizeof(arr) ⁄ sizeof(arr[0]), 7); if (index == -1) { cout << "Search Failed" << endl; } else { cout << "Target Save Index : " << index << endl; } }
'Data Structure, Algorithm' 카테고리의 다른 글
[Algorithm] - 사각형 가로 세로 비율 구하기(Aspect Rectangle Ratio) (0) 2017.02.20 [Algorithm] - 오름차순(Ascending), 내림차순(Descending) 정렬 (0) 2016.03.08 [Algorithm] - 이진 탐색(Binary Search) (0) 2016.03.05