Linear search or sequential search is a method for finding a particular value in a list, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found. Linear search is the simplest search algorithm. Its worst case cost is proportional to the number of elements in the list.
Questions tagged [linear-search]
280 questions
3
votes
1 answer
Unexpected Output of linear search
Please find my code below for linear search. Binary search function is giving the correct output. But after doing the stress testing I am not getting the correct output of linear search.
When implementing the same code for linear search with the…

Jarv Potter
- 33
- 3
3
votes
1 answer
complexity analysis of linear search in sorted array
Can anyone tell me that what will be the average time complexity of linear search when it is applied on a sorted array?
I know worst case will be O(n) and best case will be O(1) but I dont know about average case in sorted array.

Nitesh kumar
- 31
- 3
3
votes
1 answer
Time elapsed between linear search and binary search using Python
I have made two Python functions below, one for sequential (linear) search and other for binary search.
I want to do these 3 things for each size value in the given list :
generate a list of random integer values (between 1 to 1000,0000) for a…

Vishal Srivastav
- 563
- 6
- 13
3
votes
5 answers
Why is the average number of steps for finding an item in an array N/2?
Could somebody explain why the average number of steps for finding an item in an unsorted array data-structure is N/2?

Quixotic
- 2,424
- 7
- 36
- 58
3
votes
7 answers
What's the point of using linear search with sentinel?
My goal is to understand why adopting linear search with sentinel is preferred than using a standard linear search.
#include
int linearSearch(int array[], int length) {
int elementToSearch;
printf("Insert the element to be…
anon
2
votes
4 answers
Binary search giving me error saying forcing value to bool 'true' or 'false'
My program is complete the only errors I'm getting are from my binary search and my linear search. They are the same error so I'm only going to post one.
bool searchByDescriptionBinary(char desc[][DESC_SIZE],
const int…

Chad Loos
- 31
- 2
2
votes
1 answer
Dynamic array of Linear search funcion implementation
Need to implement a function
int* linearSearch(int* array, int num);
That gets a fixed size array of integers with a number and return an array with indices to the occurrences of the searched number.
For example array={3,4,5,3,6,8,7,8,3,5} & num=5…

Roey
- 171
- 12
2
votes
5 answers
How to generate random numbers from 0 to 100 without duplicates when user enters the size of the array in Java?
Write a program to generate any number of random integers in 0 to 100
range. Your program should get the size as a parameter and return the
numbers as an array.
This the question I got and below is the code I tried, but I get duplicates with this…

RaVeN
- 33
- 1
- 7
2
votes
2 answers
Get id from json data without linear scan
I have the following json data:
[
{
"date_created": "2019-12-25 12:57:58",
"date_updated": "2020-12-18 16:18:29",
"description": "Scala is a multi-paradigm, general-purpose programming language.",
"discount_price": 2,
"id":…

SAI SANTOSH CHIRAG
- 2,046
- 1
- 10
- 26
2
votes
3 answers
My Approach is Brute-Force or Linear Search?
My Problem is the following:
Given a sequence of integer values, determines if there is a distinct pair of numbers in the sequence whose product is odd. Please provide two Python functions, oddpair_bf() and oddpair_linear() for this problem. The…

Ingo Banghard
- 51
- 3
2
votes
1 answer
Why is my Binary Search slower than Linear Search?
I was trying to code a Binary Search and Linear Search and I was shocked by seeing that binary search is slower than Linear Search by sometimes even by 2 times. Please help me. Here is my code.
Binary Search Code:
def binary_search(array, target,…

Shubham Gupta - TCH
- 137
- 1
- 5
2
votes
2 answers
Why would I prefer binary search over linear search in an unsorted array?
I have been taking the DSA course on Coursera and this week have been introduced to searching algorithms. While the complexity of binary search(O(logn)) is better than linear search (O(n)). But why would I ever use it in an unsorted array given the…

Sachin Rathi
- 29
- 4
2
votes
1 answer
Why the for loop isn't getting executed?(linear search)
In this code for linear search, after line 8 is executed it jumps to line 13 without executing the for loop.
Output:
Enter the size of array
5
Enter the elements
Enter the element to be searched
Here is the code:
#include
int main()
{
…

Asfiya Shaikh
- 107
- 2
- 7
2
votes
3 answers
Haskell Linear Search (returning index)
So, I want my function to return the index (the first if it appears multiple times in the list) of a given value. It appears to work only for an empty list or if the given element has the index 0. Could you let me know what I do wrong?
linsearch _…

Andrei Bercea
- 61
- 5
2
votes
0 answers
Is my multi-threaded linear search flawed?
In the pursuit of learning I have written a multi-threaded linear search, designed to operate on an int[] array. I believe the search works as intended, however after completing it I tested it against a standard 'for loop' and was surprised to see…

Levenal
- 3,796
- 3
- 24
- 29