Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions SentinelSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <vector>
using namespace std;

// Function to search x in the given vector
int sentinelSearch(vector<int>& arr, int key) {

// Last element of the vector
int last = arr.back();

// Element to be searched is placed at the last index
arr.back() = key;
int i = 0;
while (arr[i] != key)
i++;

// Put the last element back
arr.back() = last;

// Return the index if found, otherwise return -1
if ((i < arr.size() - 1) || (arr.back() == key))
return i;
else
return -1;
}

int main() {
vector<int> arr = { 10, 20, 180, 30, 60, 50, 110, 100, 70 };
int key = 180;
int result = sentinelSearch(arr, key);
if (result != -1)
cout << key << " is present at index " << result;
else
cout << "Element not found";

return 0;
}