File tree Expand file tree Collapse file tree 3 files changed +43
-1
lines changed
Algorithms.NET/Searching/JumpSearch Expand file tree Collapse file tree 3 files changed +43
-1
lines changed Original file line number Diff line number Diff line change 1515//Console.WriteLine(Algorithms.NET.Searching.BinarySearch.BinarySearchAlgorithm.SearchIterative(0, unsortedAsc));
1616
1717Console . WriteLine ( Algorithms . NET . Searching . TernarySearch . TernarySearchAlgorithm . Search ( 8 , unsortedAsc ) ) ;
18+ Console . WriteLine ( Algorithms . NET . Searching . JumpSearch . JumpSearchAlgorithm . Search ( 0 , unsortedAsc ) ) ;
1819
1920//foreach (var item in sortedDesc)
2021//{
3435//}
3536
3637//var sortingBenchmarkSummary = BenchmarkRunner.Run<SortingBenchmark>();
37- var searchingBenchmarkSummary = BenchmarkRunner . Run < SearchingBenchmark > ( ) ;
38+ // var searchingBenchmarkSummary = BenchmarkRunner.Run<SearchingBenchmark>();
3839
3940
4041
Original file line number Diff line number Diff line change @@ -35,5 +35,8 @@ public SearchingBenchmark()
3535
3636 [ Benchmark ]
3737 public void TernarySearchRecursive ( ) => Algorithms . NET . Searching . TernarySearch . TernarySearchAlgorithm . Search ( 0 , _sortedList ) ;
38+
39+ [ Benchmark ]
40+ public void JumpSearch ( ) => Algorithms . NET . Searching . JumpSearch . JumpSearchAlgorithm . Search ( 0 , _sortedList ) ;
3841 }
3942}
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Text ;
4+
5+ namespace Algorithms . NET . Searching . JumpSearch
6+ {
7+ public class JumpSearchAlgorithm
8+ {
9+ /// <summary>
10+ /// Search an item in a list using JumpSearch algorithm, Time complexity of O(sqrt n).
11+ /// </summary>
12+ /// <param name="item">Item to search for</param>
13+ /// <param name="sortedList">Sorted list in which we want to search.</param>
14+ /// <returns>Index of item if found, Otherwise returns -1.</returns>
15+ public static int Search ( double item , List < double > sortedList )
16+ {
17+ int blockSize = ( int ) Math . Sqrt ( sortedList . Count ) ;
18+
19+ int start = 0 ;
20+ int next = blockSize ;
21+
22+ while ( start < sortedList . Count && sortedList [ next - 1 ] < item )
23+ {
24+ start = next ;
25+ next += blockSize ;
26+
27+ if ( next > sortedList . Count )
28+ next = sortedList . Count ;
29+ }
30+
31+ for ( int i = start ; i < next ; i ++ )
32+ if ( sortedList [ i ] == item )
33+ return i ;
34+
35+ return - 1 ;
36+ }
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments