ProAnswers.org

algorithm Search for a key in an array(Linear Search)

Search for a key in an array(Linear Search)

int search(int a[], int sz, int key){

	    int i;

	    for(i = 0;i < sz;i++){

	        if(a[i] == key)

	            return i; //key found at index i

	        }

	    return -1; //not found

	    }