Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Linear Search: A Guide To DSA (2)

If you want to learn about Big O : Click Here Linear Search:Linear Search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.Time Complexity: O(n) where n is the number of elements in the array, because the worst case scenario is that the element is not present in the array and we have to traverse the whole array. Image of Linear Search Searching an Array: Algorithm:Step 1: Set i to 1Step 2: if i > n then go to step 7Step 3: if A[i] = x then go to step 6Step 4: Set i to i + 1Step 5: Go to Step 2Step 6: Print Element x Found at index i and go to step 8Step 7: Print element not foundStep 8: Exit Example:A = [2, 4, 0, 1, 9]x = 1n = 5 Solving the above example:Step 1: i = 1Step 2: 1 // Search an element in an array using linear searchvoid search(int arr[], int n, int x){ int i; for(i=0; i



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Linear Search: A Guide To DSA (2)

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×