What I know is Linear Search and Sequential Search are same, if my knowledge is correct then kindly guide me wether the below algorithm work for linear search or not as it is a Sequential Search algorithm.
Step 1. Initialize searcharray, searchno, length.
Step 2. Initialize pos=0.
Step 3. Repeat step 4 till pos<=length.
Step 4. if searcharray[pos]=searchno
return pos
else
increment pos by 1.
Secondly I need help on how can I move the found value at the first element of an array, I have this code which can find value which user enter in an array using linear search.
#include <iostream.h>
#include<conio.h>
int linearSearch(const int a[], int size, int key);
int main() {
const int SIZE = 8;
int a1[SIZE] = {8, 4, 5, 3, 2, 9, 4, 1};
cout << linearSearch(a1, SIZE, 8) << endl; // 0
cout << linearSearch(a1, SIZE, 4) << endl; // 1
cout << linearSearch(a1, SIZE, 99) << endl; // 8 (not found)
}
// Search the array for the given key
// If found, return array index [0, size-1]; otherwise, return size
int linearSearch(const int a[], int size, int key) {
for (int i = 0; i < size; ++i) {
if (a[i] == key) return i;
}
return size;
}