Skip to content

This is a simple program that implements the Linear Search algorithm. It searches for a given target value in a array of integers and returns its position

Notifications You must be signed in to change notification settings

pooja210603/linear_search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Linear search

This is a simple program that implements the Linear Search algorithm. It searches for a given target value in a array of integers and returns the position of the target value if found or -1 if not found.

Algorithm

  1. Start with the main function.
  2. Declare an integer array 'arr' and initialize it with values.
  3. Declare an integer variable 'n' and assign the number of elements in the array 'arr'. Alternatively, you can calculate 'n' as: sizeof(arr) / sizeof(arr[0]).
  4. Declare an integer variable 'target' and assign the value to be searched.
  5. Call the 'linear_search' function with arguments: 'arr', 'n', and 'target'.
  6. Inside the 'linear_search' function:
    a. Declare an integer variable 'i' to use as a loop counter.
    b. Loop through the array 'arr' from index 0 to 'n-1' using a 'for' loop.
    i. Check if the element at the current index 'i' is equal to the target value.
    ii. If yes, return the index 'i' as the result.
    c. If the loop completes without finding the target value, return -1 as the result.
  7. Back in the main function:
    a. Store the result of the 'linear_search' function in an integer variable 'result'.
    b. Check if 'result' is equal to -1.
    i. If yes, print "Target value not found in the array."
    ii. If no, print "Target value found at index: " followed by the value of 'result'.
  8. End the main function.

image

Output for the above image example would be :

image

About

This is a simple program that implements the Linear Search algorithm. It searches for a given target value in a array of integers and returns its position

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages