Console.WriteLine("Hi,Please enter the number of array elements:");
int n = Convert.ToInt32(Console.ReadLine());
int[] Array = new int[n];
Console.WriteLine("-----------------------");
for (int z = 0; z < Array.Length; z++)
{
Array[z] = Convert.ToInt32(Console.ReadLine());
}
int temp;
for (int p = 0; p <= Array.Length - 2; p++)
{
for (int i = 0; i <= Array.Length - 2; i++)
{
if (Array[i] > Array[i + 1])
{
temp = Array[i + 1];
Array[i + 1] = Array[i];
Array[i] = temp;
}
}
}
Console.WriteLine("The Sorted array :");
foreach (int aa in Array) Console.WriteLine(aa + " "); //writting array
Console.WriteLine("-----------------------");
//-----------------------Line Search-------------------------------------
int Key;
Console.WriteLine("Enter Key For Line Search:");
Key = Convert.ToInt32(Console.ReadLine());
int O = -1;
for (int k = 0; k < Array.Length; k++)
{
if (Array[k]==Key)
{
Console.WriteLine("Index is :"+k);
Console.WriteLine("-----------------------");
}
}
//-------------Binary Search------------------------{
int Key2;
Console.WriteLine("Enter Key For Binary Search : ");
Key2 = Convert.ToInt32(Console.ReadLine());
int O2=-1;
int low=0;
int mid;
int high;
high = Array.Length - 1;
while(low<=high)
{
mid=(low+high)/2;
if (Key2 < Array[mid])high=mid-1;
else if (Key2 > Array[mid]) low = mid + 1;
else
{
O2=mid;
break;
}
}
Console.WriteLine("Index is : " + O2);
Console.ReadKey();