728x90
반응형
#include <stdio.h>

// Linear Search (Sequential Search) Algorithm

int LSearch(int ar[], int len, int target)
{
	int i;
	for (i = 0; i < len; i++)
	{
		if (ar[i] == target)
			return i;
	}
	return -1;
}

int main(void)
{
	int arr[] = { 3, 5, 2, 4, 9 };
	int idx;

	idx = LSearch(arr, sizeof(arr) / sizeof(int), 4);
	if (idx == -1) printf("Search to failed");
	else printf("Target Stored Index : %d \n", idx);

	idx = LSearch(arr, sizeof(arr) / sizeof(int), 7);
	if (idx == -1) printf("Search to failed");
	else printf("Target Stored Index : %d \n", idx);

	return 0;
}
728x90
반응형

'Data Structure & Algorithm' 카테고리의 다른 글

Linked List  (0) 2022.09.10
Bubble Sort Algorithm  (0) 2022.09.09
Sequential Data Structure  (0) 2022.09.07
Recursion (Factorial, Fibonacci, Tower of Hanoi)  (0) 2022.09.06
Binary Search Algorithm  (0) 2022.09.05

+ Recent posts