Algorithm for Searching in a Linear List:
#Initialize counter by assigning the lower bound value of the linear list
Step 1: Start
Step 2: Set ctr = L #L (Lower bound) is 0 (zero)
#Now search for the ITEM
Step 3: Repeat steps 3 through 4 until ctr > U. #U (Upper bound) is size-1
Step 4: IF AR[ctr] = = ITEM then
{
print("Search Successful")
print(ctr, "is the location of", ITEM)
break # go out of the loop
}
Step 5: ctr = ctr + 1
#end of repeat
Step 6: IF ctr > U then
print("Search Unsuccessful !!")
Step 7: Stop / End
The technique above searches for ITEM in a linear list AR with lower and upper bounds. When the search is successful, it jumps out of the loop (break statement); otherwise, it continues until the last element.
Linear Searching in an Array (Linear List)
function Lsearch(AR, ITEM) {
let i = 0;
while (i < AR.length && AR[i] !== ITEM) {
i += 1;
}
if (i < AR.length) {
return i;
} else {
return -1;
}
}
// ----- main -----
const N = parseInt(prompt("Enter desired linear-list size (max. 50)..."));
console.log("\nEnter elements for Linear List\n");
const AR = new Array(N).fill(0); // Initialize list of size N with zeros
for (let i = 0; i < N; i++) {
AR[i] = parseInt(prompt("Element " + i + ":"));
}
const ITEM = parseInt(prompt("Enter element to search for:"));
const index = Lsearch(AR, ITEM);
if (index !== -1) {
console.log("\nElement found at index: " + index + ", Position: " + (index + 1));
} else {
console.log("\nSorry!! Given element could not be found.\n");
}
The provided code performs a linear search on an array to find a specific item and is written in JavaScript. Here’s an explanation of the code step-by-step:
Lsearch Function
This function performs the linear search:
function Lsearch(AR, ITEM) {
let i = 0;
while (i < AR.length && AR[i] !== ITEM) {
i += 1;
}
if (i < AR.length) {
return i;
} else {
return -1;
}
}
Parameters:
- AR: The array in which to search for the item.
- ITEM: The item to search for in the array.
Algorithm:
– Initialize i to 0.
– Use a while loop to iterate through the array:
- The loop continues as long as i is less than the length of the array and the current array element (AR[i]) is not equal to ITEM.
- i is incremented in each iteration.
– After the loop, check if i is less than the length of the array:
- If true, ITEM was found at index i, so return i.
- If false, return -1 indicating ITEM was not found.
Main Program
The main part of the code interacts with the user to create the array and search for an item:
// ----- main -----
const N = parseInt(prompt("Enter desired linear-list size (max. 50)..."));
console.log("\nEnter elements for Linear List\n");
const AR = new Array(N).fill(0); // Initialize list of size N with zeros
for (let i = 0; i < N; i++) {
AR[i] = parseInt(prompt("Element " + i + ":"));
}
const ITEM = parseInt(prompt("Enter element to search for:"));
const index = Lsearch(AR, ITEM);
if (index !== -1) {
console.log("\nElement found at index: " + index + ", Position: " + (index + 1));
} else {
console.log("\nSorry!! Given element could not be found.\n");
}
Step – by – Step
– Prompt for Array Size:
- ‘const N = parseInt(prompt(“Enter desired linear-list size (max. 50)…”));’
- Prompts the user to enter the desired size of the array (N) and converts the input to an integer.
– Initialize Array:
- ‘const AR = new Array(N).fill(0);’
- Creates a new array AR of size N and fills it with zeros.
– Populate Array:
- A for loop is used to prompt the user to enter each element of the array:
for (let i = 0; i < N; i++) {
AR[i] = parseInt(prompt("Element " + i + ":"));
}
- Each element entered by the user is parsed as an integer and stored in AR.
– Prompt for Item to Search:
- ‘const ITEM = parseInt(prompt(“Enter element to search for:”));’
- Prompts the user to enter the item to search for (ITEM) and converts the input to an integer.
– Call Search Function:
- ‘const index = Lsearch(AR, ITEM);’
- Calls the Lsearch function with AR and ITEM and stores the result in index.
– Output Result:
- Check the value of index:
if (index !== -1) {
console.log("\nElement found at index: " + index + ", Position: " + (index + 1));
} else {
console.log("\nSorry!! Given element could not be found.\n");
}
- If index is not -1, it means the item was found, and it prints the index and position (index + 1 for 1-based position).
- If index is -1, it means the item was not found, and it prints a message indicating this.
Output Console:
Enter desired linear-list size (max. 50)...
5
Enter elements for Linear List
Element 0:
10
Element 1:
20
Element 2:
30
Element 3:
40
Element 4:
50
Enter element to search for:
60
Sorry!! Given element could not be found.
Delve deeper into DSA with Lounge Coder, explore them below:
For Python: Searching in a linear List
Discover more from lounge coder
Subscribe to get the latest posts sent to your email.