-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchIn2DList.cs
More file actions
46 lines (43 loc) · 1.3 KB
/
SearchIn2DList.cs
File metadata and controls
46 lines (43 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
namespace DA.Algorithms.Problems
{
public static class SearchIn2DList
{
/// <summary>
/// Find value in 2 dimensional list.
/// Each row and column are sorted in ascending order.
/// <para>Time Complexity - O(n)</para>
/// </summary>
///
/// <param name="array">Two dimensional array</param>
/// <param name="row">Row count</param>
/// <param name="column">Column count</param>
/// <param name="value">Value to find</param>
///
/// <returns>
/// true - if value is exists, otherwise return false.
/// </returns>
public static bool Search (int[,] array, int row, int column, int value)
{
int rowIndex = 0;
int columnIndex = column - 1;
while (rowIndex < row && columnIndex >= 0)
{
if (array[rowIndex, columnIndex] == value)
{
return true;
}
else if (array[rowIndex, columnIndex] > value)
{
--columnIndex;
}
else
{
++rowIndex;
}
}
return false;
}
}
}