@@ -7,27 +7,24 @@ namespace Algorithms.Search
7
7
{
8
8
public class BinarySearcher < T > : IEnumerator < T >
9
9
{
10
-
11
10
private readonly IList < T > _collection ;
12
11
private readonly Comparer < T > _comparer ;
13
-
12
+ private T _item ;
14
13
private int _currentItemIndex ;
15
14
private int _leftIndex ;
16
15
private int _rightIndex ;
17
16
18
17
/// <summary>
19
18
/// The value of the current item
20
19
/// </summary>
21
- public T Current {
22
-
20
+ public T Current
21
+ {
23
22
get
24
23
{
25
24
return _collection [ _currentItemIndex ] ;
26
25
}
27
-
28
26
}
29
27
30
-
31
28
object IEnumerator . Current => Current ;
32
29
33
30
/// <summary>
@@ -41,59 +38,39 @@ public BinarySearcher(IList<T> collection, Comparer<T> comparer)
41
38
{
42
39
throw new NullReferenceException ( "List is null" ) ;
43
40
}
41
+ _collection = collection ;
42
+ _comparer = comparer ;
43
+ HeapSorter . HeapSort ( _collection ) ;
44
+ }
44
45
45
- this . _collection = collection ;
46
- this . _comparer = comparer ;
47
-
46
+ public void SearchFor ( T item )
47
+ {
48
+ if ( item == null )
49
+ {
50
+ throw new NullReferenceException ( "Item to search for is not set" ) ;
51
+ }
52
+ Reset ( ) ;
53
+ _item = item ;
48
54
}
49
55
50
-
51
56
/// <summary>
52
57
/// Apply Binary Search in a list.
53
58
/// </summary>
54
- /// <param name="item">The item we search for</param>
55
59
/// <returns>If item found, its' index, -1 otherwise</returns>
56
- public int BinarySearch ( T item )
60
+ public int BinarySearch ( )
57
61
{
58
- _currentItemIndex = - 1 ;
59
- _leftIndex = 0 ;
60
- _rightIndex = _collection . Count - 1 ;
61
- HeapSorter . HeapSort ( _collection ) ;
62
- InternalBinarySearch ( item ) ;
63
- return _currentItemIndex ;
64
- }
62
+ bool notFound = true ;
65
63
66
-
67
- /// <summary>
68
- /// An implementation of binary search algorithm.
69
- /// </summary>
70
- /// <param name="item">The item we search for</param>
71
- /// <returns></returns>
72
- private void InternalBinarySearch ( T item )
73
- {
74
- bool found = false ;
75
-
76
- while ( MoveNext ( ) && ! found )
64
+ while ( ( _leftIndex <= _rightIndex ) && notFound )
77
65
{
78
- if ( _comparer . Compare ( item , Current ) < 0 )
79
- {
80
- _rightIndex = _currentItemIndex - 1 ;
81
- }
82
- else if ( _comparer . Compare ( item , Current ) > 0 )
83
- {
84
- _leftIndex = _currentItemIndex + 1 ;
85
- }
86
- else
87
- {
88
- found = true ;
89
- }
66
+ notFound = MoveNext ( ) ;
90
67
}
91
-
92
- if ( ! found )
68
+
69
+ if ( notFound )
93
70
{
94
- this . Reset ( ) ;
71
+ Reset ( ) ;
95
72
}
96
-
73
+ return _currentItemIndex ;
97
74
}
98
75
99
76
/// <summary>
@@ -104,23 +81,31 @@ public bool MoveNext()
104
81
{
105
82
_currentItemIndex = this . _leftIndex + ( this . _rightIndex - this . _leftIndex ) / 2 ;
106
83
107
- if ( _leftIndex <= _rightIndex )
84
+ if ( _comparer . Compare ( _item , Current ) < 0 )
85
+ {
86
+ _rightIndex = _currentItemIndex - 1 ;
87
+ }
88
+ else if ( _comparer . Compare ( _item , Current ) > 0 )
108
89
{
109
- return true ;
90
+ _leftIndex = _currentItemIndex + 1 ;
110
91
}
111
92
else
112
93
{
113
94
return false ;
114
95
}
115
-
96
+ return true ;
116
97
}
117
98
118
-
119
- public void Reset ( ) { this . _currentItemIndex = - 1 ; }
120
-
121
-
122
- public void Dispose ( ) { }
99
+ public void Reset ( )
100
+ {
101
+ this . _currentItemIndex = - 1 ;
102
+ _leftIndex = 0 ;
103
+ _rightIndex = _collection . Count - 1 ;
104
+ }
123
105
106
+ public void Dispose ( )
107
+ {
108
+ //not implementing this, since there are no managed resources to release
109
+ }
124
110
}
125
-
126
111
}
0 commit comments