-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinarySearchExample.cs
More file actions
45 lines (39 loc) · 1.41 KB
/
BinarySearchExample.cs
File metadata and controls
45 lines (39 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BinarySearch
{
class BinarySearchExample
{
static void Main(string[] args)
{
int searchInt; // Search key
int position; // Location of search
BinaryArray searchArray = new BinaryArray(15);
Console.WriteLine(searchArray);
// Prompt and input first int from user
Console.Write("Please enter an integer value (-1 to quit): ");
searchInt = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
while(searchInt != -1)
{
// Use binary search to try to find integer
position = searchArray.BinarySearch(searchInt);
// Return value of -1 indicates integer was not found
if(position == -1)
{
Console.WriteLine("The integer {0} was not found. \n", searchInt);
}
else
{
Console.WriteLine("The integer {0} was found in position {1}. \n", searchInt);
}
// Prompt and input next int from user
Console.WriteLine("Please enter an integer value (-1 to quit): ");
searchInt = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
}
}
}
}