Skip to content

Commit b1d0f9e

Browse files
Added binarySearch algorithm in both recursive and iterative ways.
1 parent 750988d commit b1d0f9e

File tree

3 files changed

+101
-24
lines changed

3 files changed

+101
-24
lines changed

Algorithms.NET.Debug/Program.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
List<double> unsorted01 = new() { .8, .9, .3, .3, .1,0 };
88

99
//var sortedDesc = Algorithms.NET.Sorting.MergeSort.MergeSortAlgorithm.SortDescending(unsortedAsc);
10-
var sortedAsc = Algorithms.NET.Sorting.BucketSort.BucketSortAlgorithm.SortAscending(unsorted01);
11-
var sortedDesc = Algorithms.NET.Sorting.BucketSort.BucketSortAlgorithm.SortDescending(unsorted01);
10+
//var sortedAsc = Algorithms.NET.Sorting.BucketSort.BucketSortAlgorithm.SortAscending(unsorted01);
11+
//var sortedDesc = Algorithms.NET.Sorting.BucketSort.BucketSortAlgorithm.SortDescending(unsorted01);
1212
//var sortedDesc = Algorithms.NET.Sorting.CountingSort.CountingSortAlgorithm.SortDescending(unsortedRand.ToArray());
13+
//Console.WriteLine(Algorithms.NET.Searching.LinearSearch.LinearSearchAlgorithm.Search(4, unsortedAsc));
14+
Console.WriteLine(Algorithms.NET.Searching.BinarySearch.BinarySearchAlgorithm.SearchRecursive(0, unsortedAsc));
15+
Console.WriteLine(Algorithms.NET.Searching.BinarySearch.BinarySearchAlgorithm.SearchIterative(0, unsortedAsc));
1316

14-
15-
foreach (var item in sortedDesc)
16-
{
17-
Console.WriteLine(item);
18-
}
19-
Console.WriteLine();
20-
foreach (var item in sortedAsc)
21-
{
22-
Console.WriteLine(item);
23-
}
17+
//foreach (var item in sortedDesc)
18+
//{
19+
// Console.WriteLine(item);
20+
//}
21+
//Console.WriteLine();
22+
//foreach (var item in sortedAsc)
23+
//{
24+
// Console.WriteLine(item);
25+
//}
2426

2527
//List<double> _unsortedAsc = new();
2628
//var rand = new Random();
@@ -29,7 +31,8 @@
2931
// _unsortedAsc.Add(rand.Next(1000000));
3032
//}
3133

32-
var summary = BenchmarkRunner.Run<BenchmarkDemo>();
34+
//var sortingBenchmarkSummary = BenchmarkRunner.Run<SortingBenchmark>();
35+
var searchingBenchmarkSummary = BenchmarkRunner.Run<SearchingBenchmark>();
3336

3437

3538

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,44 @@
99
namespace Algorithms.NET.Debug
1010
{
1111
[MemoryDiagnoser]
12-
public class BenchmarkDemo
12+
public class SortingBenchmark
1313
{
14-
private List<double> _unsorted = new();
15-
private List<double> _unsorted2 = new();
14+
private readonly List<double> _unsorted = new();
15+
private readonly List<double> _unsorted2 = new();
1616

17-
public BenchmarkDemo()
17+
public SortingBenchmark()
1818
{
1919
var rand = new Random();
2020
for (int i = 0; i < 10000; i++)
2121
{
22-
_unsorted.Add(rand.NextDouble());
23-
_unsorted2.Add(rand.Next(1000000));
22+
double number = rand.NextDouble();
23+
_unsorted.Add(number);
24+
_unsorted2.Add(number);
2425
}
2526
}
2627

27-
//[Benchmark]
28+
[Benchmark]
2829
public void BuiltInSort() =>
2930
_unsorted2.Sort();
3031

3132

32-
//[Benchmark]
33+
[Benchmark]
3334
public void BubbleSort() =>
3435
BubbleSortAlgorithm.SortAscending(_unsorted);
3536

36-
//[Benchmark]
37+
[Benchmark]
3738
public void SelectionSort() =>
3839
SelectionSortAlgorithm.SortAscending(_unsorted);
3940

40-
//[Benchmark]
41+
[Benchmark]
4142
public void InsertionSort() =>
4243
InsertionSortAlgorithm.SortAscending(_unsorted);
4344

4445
[Benchmark]
4546
public void MergeSort() =>
4647
MergeSortAlgorithm.SortAscending(_unsorted);
4748

48-
//[Benchmark]
49+
[Benchmark]
4950
public void QuickSort() =>
5051
QuickSortAlgorithm.SortAscending(_unsorted);
5152

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Algorithms.NET.Searching.BinarySearch
6+
{
7+
public class BinarySearchAlgorithm
8+
{
9+
/// <summary>
10+
/// Search an item in a list using BinarySearch algorithm, Time complexity of O(log 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 SearchRecursive(double item, List<double> sortedList)
16+
{
17+
return SearchRecursive(item, sortedList, 0, sortedList.Count - 1);
18+
}
19+
20+
/// <summary>
21+
/// Search an item in a list using BinarySearch algorithm in recursive approach, Time complexity of O(log n), Space complexity O(log n).
22+
/// </summary>
23+
/// <param name="item">Item to search for</param>
24+
/// <param name="sortedList">Sorted list in which we want to search.</param>
25+
/// <param name="endIndex">End index of searching.</param>
26+
/// <param name="startIndex">Start index of searching.</param>
27+
/// <returns>Index of item if found, Otherwise returns -1.</returns>
28+
private static int SearchRecursive(double item, List<double> sortedList, int startIndex, int endIndex)
29+
{
30+
if (startIndex > endIndex)
31+
return -1;
32+
33+
int middleIndex = (startIndex + endIndex) / 2;
34+
35+
if (sortedList[middleIndex] == item)
36+
return middleIndex;
37+
38+
if (item > sortedList[middleIndex])
39+
return SearchRecursive(item, sortedList, middleIndex + 1, endIndex);
40+
41+
return SearchRecursive(item, sortedList, startIndex, middleIndex - 1);
42+
}
43+
44+
/// <summary>
45+
/// Search an item in a list using BinarySearch algorithm in iterative approach, Time complexity of O(log n), Space complexity O(1).
46+
/// </summary>
47+
/// <param name="item">Item to search for</param>
48+
/// <param name="sortedList">Sorted list in which we want to search.</param>
49+
/// <returns>Index of item if found, Otherwise returns -1.</returns>
50+
public static int SearchIterative(double item, List<double> sortedList)
51+
{
52+
int start = 0;
53+
int end = sortedList.Count - 1;
54+
55+
while (start <= end)
56+
{
57+
int middleIndex = (start + end) / 2;
58+
59+
if (sortedList[middleIndex] == item)
60+
return middleIndex;
61+
62+
63+
if(item > sortedList[middleIndex])
64+
start = middleIndex + 1;
65+
66+
if (item < sortedList[middleIndex])
67+
end = middleIndex - 1;
68+
}
69+
70+
return -1;
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)