Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int linearSearch(int arr[], int arrSize, int key)
- {
- int resIdx{ -1 };
- for (int i{ 0 }; i < arrSize; ++i)
- {
- if (arr[i] == key)
- {
- resIdx = i;
- break;
- }
- }
- return resIdx;
- }
- int main()
- {
- const int arrSize{ 10 };
- int arr[arrSize]{ 6,1,4,2,8,9,11,3,2,1 };
- //int arr[arrSize]{ 1,1,1,2,2,9,11,1,2,1 };
- //int arr[arrSize]{ 1,2,3,4,5,6,7,8,9,10 };
- //int arr[arrSize]{ 1,2,3,4,5,6,7,9,8,7 };
- //int arr[arrSize]{ 10,9,8,7,6,5,4,3,2,1 };
- //int arr[arrSize]{0};
- int key{ 1 };
- int idx{};
- idx = linearSearch(arr, arrSize, key);
- if (idx >= 0)
- {
- std::cout << "Search key was " << key <<
- " result index is " << idx <<
- " arr[resIdx] = " << arr[idx] << '\n';
- }
- else
- {
- std::cout << "Search key was " << key << ". Search key NOT FOUND!\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement