saddleback-search-algorithm using divide a conquer
class search
{
static Saddleback(arry:Array<any>,row:number,col:number,value:any)
{
let bottomRowIndex:number=(row-1);
let bottomLeftCol:number=0;
while(bottomRowIndex>=0 && bottomLeftCol<col)
{
if(arry[bottomRowIndex][bottomLeftCol]===value)
{
return {rowindex:bottomRowIndex,colIndex:bottomLeftCol};
}else
{
if(arry[bottomRowIndex][bottomLeftCol]>value)
{
bottomRowIndex-=1;
}else
{
bottomLeftCol+=1;
}
}
}
}
}
let values=[[10, 20, 30, 40],
[15, 25, 35, 45],
[27, 29, 37, 48],
[32, 33, 39, 50],
[50, 60, 70, 80],
];
let index=search.Saddleback(values,5,4,80);
console.log(index);
Comments
Post a Comment