C++ Tutorial: Learn the Linear search algorithm for arrays and vectors

preview_player
Показать описание
Let's discuss the simple algorithm for performing a linear search on an array for a value. I give a sample implementation using C++.

Also known as a sequential search.

A perfect C++ tutorial for beginners.

// Check out my complete C++ videos playlist:

// Consider supporting this channel in multiple ways
Bitcoin: 177wfkQwzXiC8o2whQMVpSyuWUs95krKYB
Dogecoin: DRK2HDp3ZkkbFpvGZnVgMVosnBhPv8r3uP
Рекомендации по теме
Комментарии
Автор

a example to search algorithm. Thank for your explanations. Thanks to Hank..

#include <vector>
#include <iostream>
using namespace std;
vector<int> search(int arr[], int, int);
int main()
{
int arr[10]{ 1, 2, 3, 4, 5, 6, 4, 8, 9 };
int s, t;
search(arr, sizeof(arr), 4);
vector<int>v= search(arr, sizeof(arr), 4);
for (auto c : v) {
cout << c << endl;
}
return 0;
}

vector<int> search(int arr[], int size, int target) {
vector<int>v;
for (auto c = 0; c != size; ++c) {
if (arr[c] == target)
v.push_back(c);

}
return v;
}

kemalali
Автор

whenever in doubt, search for professor Hank

MmedKasem