Skip to content

Commit 46a3b94

Browse files
maaz-0thuva4
authored andcommitted
Binary Search and GCD Implementation in C# (thuva4#394)
* Add binary search algorithm implementation in C# * Update README.md * Update CONTRIBUTING.md * Fix bug * Add program to find GCD using euclidean algorithm implemented in C# * Remove dotnet project files * Update README.md
1 parent 188bb05 commit 46a3b94

6 files changed

Lines changed: 152 additions & 3 deletions

File tree

BinarySearch/C#/binSearchAlgo.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text.RegularExpressions;
5+
6+
namespace BinSearchAlgo
7+
{
8+
public class Program
9+
{
10+
// Returns index of searchValue in sorted array x, or -1 if not found
11+
public static int BinSearch(int[] x, int searchValue)
12+
{
13+
var low = 0;
14+
var high = x.Length - 1;
15+
return binarySearch(x, searchValue, low, high);
16+
}
17+
18+
public static int binarySearch(int[] x, int searchValue, int low, int high)
19+
{
20+
if (high < low)
21+
{
22+
return -1;
23+
}
24+
var mid = (low + high) / 2;
25+
if (searchValue > x[mid])
26+
{
27+
return binarySearch(x, searchValue, mid + 1, high);
28+
}
29+
else if (searchValue < x[mid])
30+
{
31+
return binarySearch(x, searchValue, low, mid - 1);
32+
}
33+
else
34+
{
35+
return mid;
36+
}
37+
}
38+
39+
//Setting up a random array and search value
40+
public static void Main(string[] args)
41+
{
42+
var rnd = new Random();
43+
44+
var rndList = new List<int>();
45+
46+
for(var i = 0; i < 10; i++)
47+
{
48+
var num = rnd.Next(0, 999);
49+
while (rndList.Contains(num))
50+
{
51+
num = rnd.Next(0, 999);
52+
}
53+
rndList.Add(num);
54+
}
55+
56+
rndList.Sort();
57+
58+
Console.WriteLine(String.Join(",", rndList));
59+
60+
var arr = rndList.ToArray();
61+
62+
var searchItem = arr[rnd.Next(0, 10)];
63+
Console.WriteLine("Search Item: " + searchItem);
64+
65+
Console.WriteLine("Index:" + BinSearch(arr, searchItem));
66+
Console.WriteLine("Index:" + BinSearch(arr, 99999));
67+
}
68+
}
69+
}

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ Unfortunately, sometimes the bug can be only reproduced in your project or in yo
154154
- [Santhosh Kumar](https://github.com/santhoshsamy29)
155155
- [Judar Lima](https://github.com/judarlima)
156156
- [Jhalaa](https://github.com/jhalaa)
157+
- [Maaz Qureshi](https://github.com/maazsq)
157158
- [Utkarsh](https://github.com/utkarshmani1997)
158159
- [langlk](https://github.com/langlk)
159160
- [Anat Portnoy](https://github.com/Anat-Port)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
// Use IntelliSense to find out which attributes exist for C# debugging
3+
// Use hover for the description of the existing attributes
4+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceRoot}/bin/Debug/netcoreapp2.0/C#.dll",
14+
"args": [],
15+
"cwd": "${workspaceRoot}",
16+
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
17+
"console": "internalConsole",
18+
"stopAtEntry": false,
19+
"internalConsoleOptions": "openOnSessionStart"
20+
},
21+
{
22+
"name": ".NET Core Attach",
23+
"type": "coreclr",
24+
"request": "attach",
25+
"processId": "${command:pickProcess}"
26+
}
27+
]
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"version": "0.1.0",
3+
"command": "dotnet",
4+
"isShellCommand": true,
5+
"args": [],
6+
"tasks": [
7+
{
8+
"taskName": "build",
9+
"args": [
10+
"${workspaceRoot}/C#.csproj"
11+
],
12+
"isBuildCommand": true,
13+
"problemMatcher": "$msCompile"
14+
}
15+
]
16+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
3+
namespace C_
4+
{
5+
class Program
6+
{
7+
static int EuclideanGCD(int a, int b)
8+
{
9+
while (b != 0)
10+
{
11+
var temp = b;
12+
b = a % b;
13+
a = temp;
14+
}
15+
return a;
16+
}
17+
18+
static void Main(string[] args)
19+
{
20+
var rnd = new Random();
21+
22+
Console.WriteLine($"a: 10, b: 5, gcd: {EuclideanGCD(10,5)}");
23+
Console.WriteLine($"a: 5, b: 10, gcd: {EuclideanGCD(5,10)}");
24+
Console.WriteLine($"a: 7, b: 11, gcd: {EuclideanGCD(7,11)}");
25+
Console.WriteLine($"a: 5000, b: 1200, gcd: {EuclideanGCD(5000,1200)}");
26+
27+
for (var i = 0; i < 3; i++)
28+
{
29+
var a = rnd.Next(1, 9999);
30+
var b = rnd.Next(1, 9999);
31+
Console.WriteLine($"a: {a}, b: {b}, gcd: {EuclideanGCD(a,b)}");
32+
}
33+
}
34+
}
35+
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ A*Search | | :+1: | | | :+1: | | | |
1111
BellmanFord | :+1: | | | | :+1: | | | |
1212
BestFirstSearch | :+1: | :+1: | | | | | | | :+1: |
1313
BinaryGCD | :+1: | | | | | | | | |
14-
BinarySearch | :+1: | :+1: | | :+1: | :+1: | :+1: | :+1: | | :+1: | :+1: | | | | :+1:
14+
BinarySearch | :+1: | :+1: | | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | | | | :+1:
1515
Binary Search Modified | | | | :+1: | | | | |
1616
Bitap Algorithm | | :+1: | | | :+1: | | | |
1717
BreadthFirstSearch | :+1: | :+1: | | :+1: | | | | |
@@ -32,8 +32,8 @@ Fibonacci | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1:
3232
FisherYatesShuffle | :+1: | | | | :+1: | :+1: | | :+1: | :+1: |
3333
FloodFill Algorithm | :+1: | :+1: | | | | | | |
3434
Floyd'sAlgorithm | :+1: | :+1: | | | :+1: | | | |
35-
Greatest Common Divisor | :+1: |:+1:| :+1: | :+1: | :+1: | | | |
36-
Hamming Distance | :+1: | :+1: | | :+1: | | :+1: | :+1: | | :+1:
35+
Greatest Common Divisor | :+1: |:+1:| :+1: | :+1: | :+1: | | |:+1: |
36+
Hamming Distance | :+1: | :+1: | | :+1: | | :+1: | :+1: | | :+1:
3737
HeapSort | :+1: | :+1: | | | :+1: | :+1: | :+1: | | :+1: | | | | :+1:
3838
Histogram equalization | :+1: | | | | | | | |
3939
InsertionSort | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1:

0 commit comments

Comments
 (0)