0% found this document useful (0 votes)
12 views4 pages

Linear Search - Dessert Example

Linear search is a straightforward method for finding a specific item in a list by checking each item sequentially until the target is found or the end of the list is reached. It is simple to implement but can be inefficient for long lists. The document provides examples and pseudo code for performing a linear search on an array of dessert names.

Uploaded by

poojeet177
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Linear Search - Dessert Example

Linear search is a straightforward method for finding a specific item in a list by checking each item sequentially until the target is found or the end of the list is reached. It is simple to implement but can be inefficient for long lists. The document provides examples and pseudo code for performing a linear search on an array of dessert names.

Uploaded by

poojeet177
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

What is Linear Search?

Linear search is a simple way to find a specific item in a list. It checks each item one by one from the
start until it finds the target or reaches the end.

- Linear search checks each item one by one.

- It continues until it finds the target or reaches the end.

- Simple but can be slow if the list is very long.

Linear search using a simple example with a 1-dimensional static array containing some
delicious dessert names.

Imagine this array:

Index: 1 2 3 4 5

Values: "chocolate brownie" "apple crumble" "red velvet" "ice-cream" "tiramisu"

How does it work?

Suppose you want to find `"ice-cream"` in our array.

Step-by-step process:

1. **Start at the first element (index 1):**

Is `"chocolate brownie"` equal to `"ice-cream"`? No.

2. **Move to the next element (index 2):**

Is `"apple crumble"` equal to `"ice-cream"`? No.

3. **Next (index 3):**

Is `"red velvet"` equal to `"ice-cream"`? No.

4. **Next (index 4):**

Is `"ice-cream"` equal to `"ice-cream"`? Yes!

We found our item.

5. **Stop searching.**

The search is complete, and we've found the item at index 4.

What if the item isn't in the list?

If you look for `"cheesecake"` and it's not in the array, you will check all items and reach the end without
finding it. In that case, the search concludes that the item is **not present**.
A simple pseudo code for performing a linear search on a 1D static array containing the given
dessert names – which will include steps from declaration, initialization, inputting values, and
then performing the linear search.

Method A: Values entered during Initialisation

Declare desserts: Array[1:5] of String // Step 1: Declare an array to hold dessert names

// Step 2: Initialize the array with dessert names

desserts[0] = "chocolate brownie" // For simplicity, assume values are already assigned as shown

desserts[1] = "apple crumble"

desserts[2] = "red velvet"

desserts[3] = "ice-cream"

desserts[4] = "tiramisu" // (Optional) Step 3: Input values into the array if not hardcoded

Input "Enter the dessert to search for: ", searchvalue // Step 4: Ask the user to input the dessert to search for

// Step 5: Initialize a variable to keep track of search result

found_index  -1 // -1 means not found

// Step 6: Loop through each element in the array to find searchvalue

for i from 1 to 5 do

if desserts[i] equals searchvalue then

found_index = i

Output “Search Successfull !”

end if

end for

// Step 7: Check if found and output the result

if found_index <> -1 then

Output "Dessert found at position " + (found_index)

else

Output "Dessert not found in the list."

end if
Method A: Values input within a loop into the array

Declare desserts: Array[1:5] of String // Step 1: Declare an array to hold dessert names

// Step 2: Initialize the array with dessert names

For i = 1 to 5 do

desserts[0] = " "

endfor

// Step 3: Input values into the array if not assigned during initialisation

For I = 1 to 5 do

Input "Enter the dessert name “, desserts[i]

endfor

Input "Enter the dessert to search for: ", searchvalue // Step 4: Ask the user to input the dessert to search for

// Step 5: Initialize a variable to keep track of search result

found_index  -1 // -1 means not found

// Step 6: Loop through each element in the array to find searchvalue

for i from 1 to 5 do

if desserts[i] equals searchvalue then

found_index = i

Output “Search Successfull !”

end if

end for

// Step 7: Check if found and output the result

if found_index <> -1 then

Output "Dessert found at position " + (found_index)

else

Output "Dessert not found in the list."

end if

Note: index, found_index have not be declared


Module Module1

Sub Main()

Dim desserts(1 To 5) As String ' Step 1: Declare an array to hold dessert names

Dim i As Integer

Dim searchValue As String

Dim foundIndex As Integer = -1 ' Initialize a variable to keep track of search result

For i = 1 To 5 ' Step 2: Initialize the array with blank strings

desserts(i) = " "

Next

For i = 1 To 5 ' Step 3: Input values into the array

Console.Write("Enter the dessert name for position " & i & ": ")

desserts(i) = Console.ReadLine()

Next

Console.Write("Enter the dessert to search for: ") ' Step 4: Ask the user to input the dessert to search for

searchValue = Console.ReadLine()

For i = 1 To 5 ' Step 5: Loop through each element in the array to find searchValue

If desserts(i) = searchValue Then

foundIndex = i

Console.WriteLine("Search Successful!")

Exit For ' Exit loop once found

End If

Next

If foundIndex <> -1 Then ' Step 7: Check if found and output the result

Console.WriteLine("Dessert found at position " & foundIndex)

Else

Console.WriteLine("Dessert not found in the list.")

End If

Console.WriteLine("Press any key to exit.") ' Wait for user input before closing

Console.ReadKey()

End Sub

End Module

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy