-
Notifications
You must be signed in to change notification settings - Fork 0
/
reallinearsearch.cs
94 lines (91 loc) · 2.55 KB
/
reallinearsearch.cs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace linearsearching
{
class Program
{
static void bubble(int[]num)
{
int count;
count = 0;
int l;
int swap;
while(count<num.Length)
{
l = count + 1;
while(l<num.Length)
{
if(num[count]>num[l])
{
swap = num[count];
num[count] = num[l];
num[l] = swap;
}
l++;
}
Console.Write("{0} ",num[count]);
count++;
}
}
static int binary(int[]num,int select)
{
int max;
int min;
int mid;
max = num.Length;
min = 0;
while(max>=min)
{
mid = (max + min) / 2;
if(num[mid]==select)
{
return mid;
}
if(num[mid]>select)
{
max = mid - 1;
}
else
{
min = mid + 1;
}
}
return -1;
}
static void Main(string[] args)
{
int size;
Console.WriteLine("enter the size of array");
size = int.Parse(Console.ReadLine());
int[] num = new int[size];
int count;
count = 0;
while(count<num.Length)
{
Console.WriteLine("enter the number for "+(count+1)+" "+"position");
num[count] = int.Parse(Console.ReadLine());
count++;
}
int searchitem;
Console.WriteLine("enter the number you want to search");
searchitem = int.Parse(Console.ReadLine());
bubble(num);
int y;
y=binary(num, searchitem);
if(y>=0)
{
Console.WriteLine();
Console.WriteLine("The number lies in "+(y+1)+" "+"position");
}
else
{
Console.WriteLine();
Console.WriteLine("There is no search item in cointainer");
}
Console.ReadKey();
}
}
}