View My Profile

DSA - Linear Search with C#

Linear Search

Linear search is a straightforward and easy to implement method. It is a sequential search and the most basic search technique that search an element or items in a list or array sequentially. The search is for the desire item go through the entire list and try to fetch a match for a single element. If the elements list is long and the position of desire item is the last index then the search path will be will be long and the time complexity will be the worst O(n).

Linear Search Algorithm Process

Suppose we have an array arr[] = [1, 2, 7, 9] of 4 integers, and an integer element 9, find whether element 9 is present in the array and return the index of the element 9 in the array, or -1 if it doesn’t exist.
Input: arr[] = [1, 2, 7, 9], x = 9
Output: 3

Linear Search Algorithm Implement with C#

Suppose we have an array arr[] = [1, 2, 7, 9] of 4 integers, and an integer element 9, find whether element 9 is present in the array and return the index of the element 9 in the array, or -1 if it doesn’t exist.

Time Complexity

Worst case: O (n), when the desire item found at the last index.
Best case: O (1), when the desire item found at the first index.

Application of Linear Search Algorithm

Unsorted Lists: when the list or array items are not sorted
Small data set: It is very easy to implement in a small set of items.
Searching Linked Lists: Searching items in an e-commerce category or pick up the nearest phonebook and open it to the first page of names are some examples of real life project implementation of Linear Search Algorithm.


Author, About me

I have 7 years of experience in developing Software using C#, Java, Asp.Net Core REST API, AngularJs, Angular 9 , SQL & Oracle Server, CQRS(Clean) and N-tier pattern. I am a Full-Stack Software Developer & Seeking a full time job in the USA