Skip to content

Add binary search algoritm #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions BinarySearch/BinarySearch.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

</Project>
79 changes: 79 additions & 0 deletions BinarySearch/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;

namespace BinarySearch
{
class Program
{
//метод для рекурсивного бинарного поиска
static int BinarySearch(int[] array, int searchedValue, int first, int last)
{
//границы сошлись
if (first > last)
{
//элемент не найден
return -1;
}

//средний индекс подмассива
var middle = (first + last) / 2;
//значение в средине подмассива
var middleValue = array[middle];

if (middleValue == searchedValue)
{
return middle;
}
else
{
if (middleValue > searchedValue)
{
//рекурсивный вызов поиска для левого подмассива
return BinarySearch(array, searchedValue, first, middle - 1);
}
else
{
//рекурсивный вызов поиска для правого подмассива
return BinarySearch(array, searchedValue, middle + 1, last);
}
}
}

//программа для бинарного поиска элемента в упорядоченном массиве
static void Main(string[] args)
{
Console.WriteLine("Binary Search");
Console.Write("Enter elements: ");
var s = Console.ReadLine().Split(new[] { " ", ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
var array = new int[s.Length];
for (int i = 0; i < s.Length; i++)
{
array[i] = Convert.ToInt32(s[i]);
}

Array.Sort(array);
Console.WriteLine("Упорядоченный массив: {0}", string.Join(", ", array));

while (true)
{
Console.Write("Enter search element or -777 for exit: ");
var k = Convert.ToInt32(Console.ReadLine());
if (k == -777)
{
break;
}

var searchResult = BinarySearch(array, k, 0, array.Length - 1);
if (searchResult < 0)
{
Console.WriteLine("Element {0} wasn't founded", k);
}
else
{
Console.WriteLine("Element was founded {0} index :{1}", k, searchResult);
}
}

Console.ReadLine();
}
}
}
21 changes: 21 additions & 0 deletions Tests/search/BinarySearchTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tests.search
{
[TestClass]
public class BinarySearchTest
{

[TestMethod]
public void search()
{

}

}
}