An extremely simple algorithm for searching for a particular value in a list. It consists of comparing each element of the list with the key, until the key is found, or the search reaches the end of the list. Nor surprisingly, it runs in linear time. Sample code in C:1

int linear_search(some_type list[], int n, some_type key)
	int i = 0;
	while(i < n) {
		if(list[i] == key)
			return i;

	return NOT_FOUND;
1 I contemplated whether it would be better to give sample code in Python, since the syntax is generally thought to be so simple and clear. On the other hand, everyone knows C. If you have thoughts on this, please /msg me.

Log in or register to write something here or to contact authors.