Linear Search

preview_player
Показать описание
Linear search algorithm finds given element in a list of elements with O(n) time complexity where n is total number of elements in the list. This search process starts comparing of search element with the first element in the list. If both are matching, then results with element found otherwise search element is compared with next element in the list. If both are matched, then the result is "element found". Otherwise, repeat the same with the next element in the list until search element is compared with last element in the list, if that last element also doesn't match, then the result is "Element not found in the list". That means, the search element is compared with element by element in the list.

Step 1: Read the search element from the user
Step 2: Compare, the search element with the first element in the list.
Step 3: If both are matching, then display "Given element found!!!" and terminate the function
Step 4: If both are not matching, then compare search element with the next element in the list.
Step 5: Repeat steps 3 and 4 until the search element is compared with the last element in the list.
Step 6: If the last element in the list is also doesn't match, then display "Element not found!!!" and terminate the function.

• The linear search is simple - It is very easy to understand and implement;
• It does not require the data in the array to be stored in any particular order.
• Linear Search is better than Binary Search, Linear search takes linear time with a worst case of O(n) for n items, and an average of O(n/2). Binary search takes logarithmic time, with a worst and average case of O(n log n).
• Linear search is its simplicity: conceptually, it's extraordinarily easy to understand, and, implementation-wise, it's also very straight-forward.
• From an operational standpoint, linear search also is very resource efficient - it does not require copying/partitioning of the array being search, and thus is memory-efficient. It also operates equally well on both unsorted and sorted data.

- If the data is initially unsorted, linear search will definitely be faster than sorting followed by binary search, if you are only searching once

- The list is unsorted and is only to be searched once

- The data structure is not random access (like a linked-list)

- The list will need sorting following the search operation (due to say an insertion), since the resorting will dominate the time complexity of the overall task
Рекомендации по теме
welcome to shbcf.ru