Home Arrays Unordered Linear Search

# Unordered Linear Search

0 comment

Let us assume that given an array whose elements order is not known. That means the elements of the array are not sorted. In this case if we want to search for an element then we have to scan the complete array and see if the element is there in the given list or not.

This requires to scan the array completely and check each element for the array that we need to search. Here is an implementation of the algorithm.

#include<stdio.h>

// a function to search "data" in an array "arr" of size "size"
// returns 1 if the element is present else 0
int unorderedLinearSearch(int arr[], int size, int data)
{
int found_flag = 0;

int i;
for(i=0;i<size;i++)
{
//loop through the entire array and search for the element
if(arr[i] == data)
{
// if the element is found, we change the flag and break the loop
found_flag = 1;
break;
}
}

return found_flag;
}

//driver program to test the function
int main(void)
{
int arr[10] = {2, 6, 4, 10, 8, 1, 9, 5, 3, 7};

int to_search = 5;

if(unorderedLinearSearch(arr,10,to_search))
printf("FOUND");
else

return 0;
}


Time Complexity:- O(n), in the worst case to scan the complete array.
Space Complexity:- O(1)

0 comment

#### You may also like

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More