লিনিয়ার সার্চিং টেকনিক হল সবচেয়ে সহজ টেকনিক। এই টেকনিকতে, আইটেমগুলো একে একে সার্চ করা হয়। এই প্রসিডিউর আনসর্টেড ডেটা সেটের জন্যও প্রযোজ্য। লিনিয়ার সার্চকে সিকোয়েনশিয়াল সার্চও বলা হয়। এটিকে লিনিয়ার বলা হয় কারণ এর টাইম কমপ্লেক্সিটি n-এর ক্রমে O(n)।
লিনিয়ার সার্চ টেকনিকের কমপ্লেক্সিটি
- টাইম কমপ্লেক্সিটি: O(n)
- স্পেস কমপ্লেক্সিটি: O(1)
ইনপুট এবং আউটপুট
Input: A list of data: 20 4 89 75 10 23 45 69 the search key 10 Output: Item found at location: 4
অ্যালগরিদম
linearSearch(array, size, key)
Input - An sorted array, size of the array and the search key
Output - location of the key (if found), otherwise wrong location.
Begin
for i := 0 to size -1 do
if array[i] = key then
return i
done
return invalid location
End
উদাহরণ
#includeusing namespace std; int linSearch(int array[], int size, int key) { for(int i = 0; i > n; int arr[n]; //create an array of size n cout << "Enter items: " << endl; for(int i = 0; i > arr[i]; } cout << "Enter search key to search in the list: "; cin >> searchKey; if((loc = linSearch(arr, n, searchKey)) >= 0) cout << "Item found at location: " << loc << endl; else cout << "Item is not found in the list." << endl; }
আউটপুট
Enter number of items: 8 Enter items: 20 4 89 75 10 23 45 69 Enter search key to search in the list: 10 Item found at location: 4