Staircase Search

Can someone please tell me where I can find the code for the staircase search algorithm ?

Hey Mayank, there is a video in the course on this topic by that you can understand the algorithm and code it yourself. If you are facing any issue with the code of this algorithm you can share the link to your code here, we will help you in debugging it.

#include
using namespace std;
int main() {
int m,n;
int i,j;
cout<<“Enter the no. of rows and columns”<<endl;
cin>>m>>n;
int a[10][10];
for ( i=0;i<m;i++){
for (j=0;j<n;j++){
cin>>a[i][j];

    }
}
cout<<"Enter the no. to be searched"<<endl;
int x;
cin>>x;
i = 0;
j = n-1;
int pass = 0;
while(i<m&&j<n){
    if(a[i][j]==x){
        pass = 1;
        break;
    }
    else if(x<a[i][j]){
        j--;

    }
    else{
        i++;
    }
}
if (pass==1)
cout<<"No. found at row = "<<i+1<<"column = "<<j+1<<endl;
return 0;

}

The condition inside the loop j<n is invalid, it should be j>=0

#include<bits/stdc++.h>

using namespace std;

void stairCaseSearch(vector<vector<int>> arr, int n, int el) {
    int i=0, j=n-1;
    while(i<n && j>=0) {
        if(el==arr[i][j]) {
            cout<<"Found at row = "<<i+1<<" and column = "<<j+1;
            return ;
        } else if(el<arr[i][j])
            j--;
        else if(el>arr[i][j])
            i++;
    }
    cout<<"Not found";
    return;
}
int main() {
    int n, el;
    cin>>n>>el;
	vector<vector<int>> arr(n, vector<int> (n));
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            cin>>arr[i][j];
        }
    }
    stairCaseSearch(arr,n,el);
	return 0;
}
1 Like