Questions tagged [linear-search]

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.

280 questions
2
votes
2 answers

Variable not declared within this scope Array Linear Search

I am trying to make a program in C++ that will search for a desired value in an array of size 10 using a separate search function. Below is the code: main.cpp #include #include using namespace std; int main() { cout…
2
votes
2 answers

Find string pairs

I was trying to practice a few string and algorithmic based questions and i stumbled across one which has the below wordings: You have a string x of length N, which consists of small English letters. You have to find the number of sub-strings S in x…
Subhayan Bhattacharya
  • 5,407
  • 7
  • 42
  • 60
2
votes
2 answers

Linear Search using functions and dynamic memory allocation in C

#include #include #include #define LIMIT 30000 void CreateArray(int *p, int N) { int i; p = (int *)malloc(N * sizeof(int)); srand((long)210); for (i = 0; i < N; i++) *(p + i) = rand() %…
user3120283
  • 85
  • 2
  • 9
2
votes
1 answer

SSE comparisons not working as intended while doing a linear search over an array of integers in c++

I have the following code intended to execute a linear search through an array using streaming SIMD extensions in c++: #include #include using namespace std; bool sse2_search_array(int* arr, int size, int key) { int…
Valence
  • 201
  • 1
  • 3
  • 8
2
votes
1 answer

Can linear search be executed in a O(log n) time by incrementing the loop variable by 2 instead of 1?

The crucial change in the code could be something like: // while loop from 0 to n - 2; i initially = 0 if( arr[i + 1] != element && arr[i] != element) i += 2; else if(arr[i] == element){ cout << "Element present at: " << i; } else{ cout <<…
Aashir shukla
  • 93
  • 1
  • 9
2
votes
2 answers

creating more threads in JAVA to parallelize linear search consumes more time

I have written a java program to paralleise Linear search algorithm,where input:100000 random nos and N=no of threads,where each thread takes 100000/N no of elements and perform linear search I expected time taken for execution to decrease with…
avaj
  • 135
  • 1
  • 1
  • 8
2
votes
1 answer

Linear and Binary search using iterator position

I am writing two functions, the first one does a linear search and is supposed to return the position of the first found value if it is in the vector. The second method is a binary search that is done using equal_range and it is supposed to return…
GrapeSoda3
  • 583
  • 1
  • 7
  • 17
2
votes
3 answers

is this algorithm called linear search?

Say, I am trying to find the largest element in an array, and I write some code as follows. public class LargestElement { public static void main(String[] args) { int[] a = {1,2,6,4,5,4,3,1}; int max = a[0]; for(int…
saltandwater
  • 791
  • 2
  • 9
  • 25
2
votes
5 answers

Function of return -1

Basically what does this return -1 do ? for an example : int linear[] = {4, 21, 36, 14, 66, 91, 8, 22, 7, 81, 77, 10}; int key = 77; for (int i = 0; i < linear.length; i++){ if (linear[i] > key) return -1; //here else if (linear[i] == key) …
Athirah Hazira
  • 473
  • 2
  • 15
  • 37
2
votes
1 answer

Remove function not working properly

My remove function is acting up. Lets say if I add "Apple", "Boy", "Cat" into my array. It sorts it alphabetically. When I remove something, lets say "Boy", it removes it fine. But if I enter in "Brown" it removes "Cat" from my list. It will always…
GiBiT
  • 311
  • 1
  • 8
  • 20
1
vote
2 answers

Binary search + Sorting vs. Linear search (Big O)

i have a question on a problem i am working on. I have to play Videos randomly in order without repeating a video. Each video is only allowed to be played once per playlist. Each video has an unique id from 0 up to max_video_count which is…
markus_p
  • 574
  • 8
  • 25
1
vote
1 answer

Linear search algorithm partially correct

def linsearch(list, target): for i in range(0, len(list)): if (list[i] == target): return (i) else: return ("not in list") list1 = [1,2,3,4,5] print(linsearch(list1,1)) This is the…
1
vote
0 answers

How to calculate the FLOPS of a Python program?

I have the following python program for Linear Search algorithm: import numpy as np a, item = [2.6778716682529704, 8.224004328108661, 8.819020166860604, 25.04500044837642, 114.6788167136755, 147.21744952331062, 109.1213882924877,…
1
vote
1 answer

Make a method that contains a linear search?

I'm new to programmning and I'm taking a course to learn the basics in c#. Right now I'm doing a console application that are supposed to work as a blog. In the application the user should be able to write a new post, show written posts and search…
1
vote
1 answer
1 2
3
18 19