Computer Science

Linear Search

Linear search is a simple search algorithm that checks each element in a list or array until the desired element is found. It is a brute-force approach that is easy to implement but can be inefficient for large datasets.

Written by Perlego with AI-assistance

10 Key excerpts on "Linear Search"

  • Data Structures and Program Design Using Python
    eBook - ePub
    CHAPTER 6
    SEARCHING AND SORTING
    6.1 INTRODUCTION TO SEARCHING
    Computer systems are often used to store large numbers. We require some search mechanism to retrieve a specific record from the large amounts of data stored in our computer system. Searching means to find whether a particular data item exists in an array/list or not. The process of finding a particular value in a list or an array is called searching. If that particular value is present in the array, then the search is said to be successful and the location of that particular value is returned by the searching process. However, if the value does not exist, then searching is said to be unsuccessful. There are many different search algorithms, but three of the popular searching techniques are as follows:
    Linear Search or Sequential Search
    Binary Search
    Interpolation Search
    Here, we will discuss all these methods in detail.
    6.2 Linear Search OR SEQUENTIAL SEARCH
    A Linear Search is also called a sequential search. This is a very simple technique used to search for a particular value in an array. A Linear Search works by comparing the value of the key being searched for with every element of the array in a linear sequence until a match is found. A search will be unsuccessful if all the data elements are read and the desired element is not found. The following are some important points:
    It is the simplest way to search an element in a list.
    It searches the data element sequentially, no matter whether the array is sorted or unsorted.
    For Example – let us take an array/list of ten elements, which is declared as follows:
    array = [87, 25, 14, 39, 74, 1, 99, 12, 30, 67]
    The value to be searched for in the array is VALUE = 74, and then we search to find whether 74 exists in the array. If the value is present, then its position is returned. Here, the position of VAL = 74 is POS = 4 (index starting from zero), which is shown in the following figures.
    Pass 1
  • Data Structures and Program Design Using Java
    eBook - ePub
    6

    SEARCHING AND SORTING

    6.1  Introduction to Searching

    As we all know, computer systems are often used to store large numbers. We require some search mechanism to retrieve a specific record from the large amounts of data stored in our computer system. Searching means to find whether a particular data item exists in an array/list or not. The process of finding a particular value in a list or an array is called searching.If that particular value is present in the array, then the search is said to be successful and the location of that particular value is returned by the searching process. However, if the value does not exist, then searching is said to be unsuccessful. There are many different searching algorithms, but three of the popular searching techniques are as follows:
    • Linear Search or Sequential Search
    • Binary Search
    • Interpolation Search
    Here, we will discuss all these methods in detail.

    6.2  Linear Search or Sequential Search

    A Linear Search is also called a sequential search. This is a very simple technique used to search for a particular value in an array. A Linear Search works by comparing the value of the key being searched for with every element of the array in a linear sequence until a match is found. A search will be unsuccessful if all the data elements are readand the desired element is not found. The following are some important points:
    • It is the simplest way to search an element in a list.
    • It searches the data element sequentially, no matter whether the array is sorted or unsorted.
    For example: Let us take an array of ten elements, which is declared as follows:
    int array[10] = {87, 25, 14, 39, 74, 1, 99, 12, 30, 67};
    and the value to be searched for in the array is VALUE = 74, and then search to find whether 74 exists in the array or not. If the value is present, then its position is returned. Here the position of VAL = 74 is POS = 4 (index starting from zero), which has been shown by the following figures:
  • Data Structures and Program Design Using C++
    6

    SEARCHING AND SORTING

    In This Chapter
    Introduction to searching
    Linear Search or sequential search
    Binary search
    Interpolation search
    Introduction to sorting
    External sorting
    Summary
    Exercises

    6.1Introduction to Searching

    As we all know, computer systems are often used to store large numbers. We require some search mechanism to retrieve a specific record from the large amounts of data stored in our computer system. Searching means to find whether a particular data item exists in an array/list or not. The process of finding a particular value in a list or an array is called searching . If that particular value is present in the array, then the search is said to be successful and the location of that particular value is returned by the searching process. However, if the value does not exist then searching is said to be unsuccessful. There are many different searching algorithms, but three of the popular searching techniques are as follows:
    Linear Search or Sequential Search •Binary Search •Interpolation Search Here, we will discuss all these methods in detail.

    6.2Linear Search or Sequential Search

    A Linear Search is also called a sequential search. This is a very simple technique used to search for a particular value in an array. A Linear Search works by comparing the value of the key being searched for with every element of the array in a linear sequence until a match is found . A search will be unsuccessful if all the data elements are read and the desired element is not found. The following are some important points:
    •It is the simplest way to search an element in a list. •It searches the data element sequentially, no matter whether the array is sorted or unsorted.
    For example – Let us take an array of ten elements, which is declared as follows:
    int array[10] = {87, 25, 14, 39, 74, 1, 99, 12, 30, 67};
    and the value to be searched for in the array is VALUE = 74, and then search to find whether 74 exists in the array or not. If the value is present, then its position is returned. Here the position of VAL = 74 is POS = 4 (index starting from zero), which has been shown by the following figures:
  • Hands-On Data Structures and Algorithms with Python
    75 is present in the list or not. It becomes important to have an efficient search algorithm when the list of data items becomes large.
    There are two different ways in which data can be organized, which can affect how a search algorithm works:
    • First, the search algorithm is applied to a list of items that is already sorted; that is, it is applied to an ordered set of items. For example, [1, 3, 5, 7, 9, 11, 13, 15, 17] .
    • The search algorithm is applied to an unordered set of items, which is not sorted. For example, [11, 3, 45, 76, 99, 11, 13, 35, 37] .
    We will first take a look at Linear Searching.

    Linear Search

    The search operation is used to find out the index position of a given data item in a list of data items. If the searched item is available in the given list of data items, then the search algorithm returns the index position where it is located; otherwise, it returns that the item is not found. Here, the index position is the location of the desired item in the given list.
    The simplest approach to search for an item in a list is to search linearly, in which we look for items one by one in the whole list. Let’s take an example of six list items {60, 1, 88, 10, 11, 100} to understand the Linear Search algorithm, as shown in Figure 10.1 :
    Figure 10.1: An example of Linear Search
    The preceding list has elements that can be accessed through the index. To find an element in the list, we can search for the given element linearly one by one. This technique traverses the list of elements by using the index to move from the beginning of the list to the end. Each element is checked, and if it does not match the search item, the next item is examined. By hopping from one item to the next, the list is traversed sequentially. We use list items with integer values in this chapter to help you understand the concept, since integers can be compared easily; however, a list item can hold any other data type as well.
  • Data Structure Using C
    eBook - ePub

    Data Structure Using C

    Theory and Program

    • Ahmad Talha Siddiqui, Shoeb Ahad Siddiqui(Authors)
    • 2023(Publication Date)
    • CRC Press
      (Publisher)
    8 Searching and Sorting

    8.1 Searching and Sorting

    The process of finding the location of a specific data item or record with a given key value or finding the locations of all records, which satisfy one or more conditions in a list, is called “Searching”. If the item exists in the given list then search is said to be successful otherwise if the element if not found in the given list then search is said to be unsuccessful.
    1. External searching
    2. Internal searching
    External searching means searching the records using keys where there are many records which reside in files stored on disks. Internal searching means searching the records using keys where there are less number of records residing entirely within the computer’s main memory.
    There are many different searching algorithms. The algorithm that one choosesgenerally depends on the way information in DATA is arranged. Following arethe three important searching techniques:
    • Linear or Sequential Searching
    • Binary Searching
    • Interpolation Search
    The time required for a search operation depends on the complexity of the searching algorithm. Basically, we have to consider three cases when we search for a particular element in the list.
    1. Best Case: The best case is that in which the element is found during the first comparison.
    2. Worst Case: The worst case is that in which the element is found only at the end i.e., in the last comparison.
    3. Average Case: The average case is that in which the element is found in comparisons more than best case but less than worst case.

    8.2 Linear or Sequential Searching

    Suppose DATA is a linear array with n elements. Given no other information about DATA. The most intuitive way to search for a given ITEM in DATA is to compare ITEM with each element of DATA one-by-one. That is, first we test whether DATA (1) = ITEM, and then we test whether DATA (2) = ITEM, and so on. This method which traverses DATA sequentially to locate ITEM, is called Linear Search or sequence search. Each element of an array is read one-by-one sequentially and it is compared with the desired element. A search will be unsuccessful if all the elements are read and the desired element is not found. Linear Search is the least efficient search technique among the quantity dependent search techniques. This technique should be chosen for searching the records when the records are stored without considering the order or when the storage medium lacks the direct access facility. Some important points are:
  • Data Structures using C
    eBook - ePub

    Data Structures using C

    A Practical Approach for Beginners

    Sorting is an important step to speed up the subsequent operations on a data structure, like comparing two lists, assigning processes to the processor based on priority, etc. It is easier and faster to search the position of elements in a sorted list than unsorted. Sorting algorithms can be used in a program to sort an array for later searching or writing out of an ordered file or report. Computers spend more time in sorting than any other operation, historically 25% on mainframes. Sorting is the best-studied problem in computer science, with a variety of different algorithms known. Sorting is important because once a set of elements is sorted, many more other problems become easy to solve.

    7.2 Sequential Search or Linear Search

    7.2.1 Linear Search or Sequential Search Basics

    In Linear Search, we access each element of an array one by one sequentially and see whether or not it is desired element. A search will be unsuccessful if all the elements are accessed, and the desired element is not found.

    7.2.2 Efficiency of Linear Search

    1. Best-case time complexity: Searched element is available at the first or almost first place that is at a [0].
      T(n) = Ω (1)
    2. Worst-case time complexity: Searched element is available at the end of array that is at a [n – 1] or it is not available in the array.
      T(n) = O(n)
    3. Average-case time complexity: Searched element is present nearly at the middle of the list is called as average case in Linear Search.
      Average case = (Best case + Worst case)/2 = (1+n) /2
      T(n) = Θ (n)
    Time complexity:
    Best-case time complexity T(n) = Ω (1)
    Average-case time complexity T(n) = Θ (n)
    Worst-case time complexity T(n) = O(n)

    7.2.3 Algorithm of Linear Search

    1. Start.
    2. Traverse the array using any other loop.
    3. In every iteration, compare the key element value with the current value of the array.
      1. If the value gets a match, then print the current position or location of the array element.
      2. If the value does not match, move on to the next array element by incrementing the array index by one.
      3. If the last element in the array is checked with key elements and no match is found, then print the key element that is not present in your array or list.
  • Data Structure and Algorithms Using C++
    eBook - ePub

    Data Structure and Algorithms Using C++

    A Practical Implementation

    • Sachi Nandan Mohanty, Pabitra Kumar Tripathy(Authors)
    • 2021(Publication Date)
    • Wiley-Scrivener
      (Publisher)
    10 Searching and Sorting
    Searching is the process of finding out the position of an element in an list. If the element is found inside the list then the searching process is successful otherwise the searching process is failure.
    Searching is of two types such as
    Linear Search Binary Search

    10.1 Linear Search

    This is the simplest method of searching .In this method the element to be found is sequentially searched in the list.
    This method can be applied to a sorted or an un-sorted list.Searching is case of sorted list starts from 0th element and continues until the element is found or an element whose value is greater (Assuming the list is sorted in ascending order) than the value being searched is reached.
    As against this,searching in case of unsorted list starts from 0th element and continues until the element is found or the end of list is reached.
    Algorithm
    N → Boundary of the list Item → Searching number Data → Linear array Step-1 I=0 Step-2 Repeat while I<=n    If (item = data[i])        Print “Searching is successful”        Exit Else    Print “Searching is Unsuccessful” Step-3 exit
    Analysis of the Sequential Search
    The number of comparisons for a successful search is depends upon the position where the key value is present. If the searched value is present at the 1st place then m 1 comparison is required. So in general mth comparisons are required to search the mth element.
  • From Algorithms to Thinking Machines
    eBook - ePub
    • Domenico Talia(Author)
    • 2023(Publication Date)
    • ACM Books
      (Publisher)
    In this positive case, the algorithm breaks the search, exiting from the For loop and answering the location of the found value. If x is not present in the list, the answer will be 0. The best-case scenario is when the element is in the first position of the list, whereas in the worst-case it is at the end. We can then deduce that the time required to search an element using this Linear Search algorithm depends on the size of the list. A Linear Search is practical when the list contains a limited number of elements; however, it must be used whenever there is a need to search an unordered list. When a very long list of elements has to be searched, it is better to sort the list by using a sorting algorithm and then to run a binary search algorithm on the sorted list. A binary search looks for the position of a value by exploiting the sorted list. In particular, a binary search equates the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is obviously eliminated and the search continues on the remaining half. This strategy is iterated until the target value is found or the remaining half is empty. In this last case, the target is not in the array. A binary search is faster than a Linear Search as it works on a sorted array; thus, it runs in logarithmic time whereas linear time is needed for a Linear Search. Sorting algorithms play a significant role in computation methods, and they are very often used in software applications. As an example of a sorting algorithm, here we show a procedure for sorting a set of numeric values that is used in many applications when there is a need to order arbitrarily long sequences of numbers. The algorithm we discuss here is called insertion sort (sort by insertion), and although it is simple, it is not the most efficient sorting algorithm
  • Introduction to C++
    • George S. Tselikis(Author)
    • 2022(Publication Date)
    • CRC Press
      (Publisher)
    12 Searching and Sorting Arrays
    DOI: 10.1201/9781003230076-12
    This chapter describes some of the most common algorithms used for searching a value in an array and for sorting the elements of an array in ascending or descending order. For more algorithms, performance analysis, metrics, and best implementations you should read a book that focuses on this subject. The purpose of this chapter is to introduce you in this area and get a feeling of how algorithms can be implemented.

    SEARCHING ARRAYS

    To check whether a value is stored in an array, we are going to describe the linear and binary search algorithms.

    Linear Search Algorithm

    The Linear Search algorithm (also called sequential search) is the simplest algorithm to search for a value in a non-sorted array. The searched value is compared with the value of each element until a match is found. In an array of n elements, the maximum number of searches is n , which occurs when the searched value is either the last element or not in the array. In the following program, the linear_search() function implements the Linear Search algorithm.

    EXERCISES

    C.12.1 Write a function that searches for a number in an array of doubles. If the number is stored, the function should return the number of its occurrences and the position of its first occurrence, otherwise -1 . Write a program that reads up to 100 doubles and stores them in an array. If the user enters -1
  • Data Structures Through C++
    eBook - ePub

    Data Structures Through C++

    Experience Data Structures C++ through animations

    Chapter 09

    Searching and Sorting

    Seek Me Out, Sort Me Out

    Why This Chapter Matters?

    It would be an interesting statistic to find out how much time pre-computer-age generations spent in searching things and arranging them in an order. What a colossal waste it must have been to do these things manually! When history of computing is written ‘searching’ and ‘sorting’ would be right there at the top, as entities responsible for changing the way people do work.
     
    W e often spend time in searching some thing or the other. If the data is kept properly in some sorted order then searching becomes very easy. Think of searching a word's meaning from an unordered list of words and then you will appreciate the way a dictionary is designed. In this chapter we are going to discuss different types of searching and sorting methods. Let us start with searching methods.

    Searching

    Searching is an operation that finds the location of a given element in a list. The search is said to be successful or unsuccessful depending on whether the element that is to be searched is found or not. Here, we will discuss two standard searching methods—Linear Search and Binary search.

    Linear Search

    This is the simplest method of searching. In this method, an element is searched in the list sequentially. This method can be applied to a sorted or an unsorted list. Searching in unsorted list starts from the 0th element and continues until the element is found or the end of list is reached. As against this, searching in an ascending order sorted list starts from 0th element and continues until the element is found or an element whose value is greater than the value being searched is reached.
    Following program implements Linear Search method for an unsorted as well as a sorted array.

    Honest Solid Code

    Program 9-1. Implementation of Linear Search algorithm
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.