How to seach for an Item In an Array?? and insert a new element in sorted array
Suppose I have one Array, it have multiple items in it. I want to search one number in it. To do this task I am using binary search algorithm to search the Item in an array.
We can insert a new value in an sorted array.
Binary Search algorithm
I am listing out the simple steps to do this task easily understandable.
1) Define the array int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
2) Enter any number you want to search between 1 to 10.
3) Define the variables int start = 0, endItem = array.Length - 1, midleItem = (int)((start + endItem) / 2);
4) Looping it with while loop array[midleItem] != searchItem && start <= endItem.
Performing the if condition with searchItem > array[midleItem], if yes, start = midleItem + 1 else endItem = midleItem - 1;
5) Now define midleItem = (int)((start + endItem) / 2)
6) If array[midleItem] == searchItem then print the array index.
1) Else display the not fount message.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Item Value to search between 1 to 9:");
int searchItem = Int32.Parse(Console.ReadLine());
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int start = 0;
int endItem = array.Length - 1;
int midleItem = (int)((start + endItem) / 2);
while (array[midleItem] != searchItem && start <= endItem)
{
if (searchItem > array[midleItem])
{
start = midleItem + 1;
}
else
{
endItem = midleItem - 1;
}
midleItem = (int)((start + endItem) / 2);
}
if (array[midleItem] == searchItem)
{
Console.WriteLine("I found your Search Item, Index is:");
Console.Write(midleItem.ToString());
Console.Read();
}
else
{
Console.Write("Item Not found");
Console.Read();
}
}
}
}
I am listing out the simple steps to do this task easily understandable.
1) Define the array int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
2) Enter any number you want to search between 1 to 10.
3) Define the variables int start = 0, endItem = array.Length - 1, midleItem = (int)((start + endItem) / 2);
4) Looping it with while loop array[midleItem] != searchItem && start <= endItem.
Performing the if condition with searchItem > array[midleItem], if yes, start = midleItem + 1 else endItem = midleItem - 1;
5) Now define midleItem = (int)((start + endItem) / 2)
6) If array[midleItem] == searchItem then print the array index.
1) Else display the not fount message.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Item Value to search between 1 to 9:");
int searchItem = Int32.Parse(Console.ReadLine());
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int start = 0;
int endItem = array.Length - 1;
int midleItem = (int)((start + endItem) / 2);
while (array[midleItem] != searchItem && start <= endItem)
{
if (searchItem > array[midleItem])
{
start = midleItem + 1;
}
else
{
endItem = midleItem - 1;
}
midleItem = (int)((start + endItem) / 2);
}
if (array[midleItem] == searchItem)
{
Console.WriteLine("I found your Search Item, Index is:");
Console.Write(midleItem.ToString());
Console.Read();
}
else
{
Console.Write("Item Not found");
Console.Read();
}
}
}
}
insert a new element in sorted array
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter lengh of array in list");
int arrayLength = Int32.Parse(Console.ReadLine());
ArrayList array = new ArrayList();
for (int loop = 0; loop < arrayLength; loop++)
{
array.Add(Convert.ToInt32(Console.ReadLine()));
}
Console.WriteLine("Enter Item to insert in list");
int item = Int32.Parse(Console.ReadLine());
int begin = 0;
int end = arrayLength - 1;
int mid = (begin + end) / 2;
bool flag1 = false, flag2 = false;
if (item < Convert.ToInt32(array[0]))
{
array.Insert(0, item);
}
else if (item > Convert.ToInt32(arrayLength - 1))
{
array.Insert(arrayLength, item);
}
else
{
while (!(flag1 && flag2))
{
if (item >= Convert.ToInt32(array[mid]))
{
flag1 = true;
begin = mid + 1;
}
else if (Convert.ToInt32(array[arrayLength - 1]) < item)
{
flag2 = true;
}
else
{
flag2 = true;
end = mid - 1;
}
mid = (begin + end) / 2;
}
array.Insert(mid + 1, item);
}
}
}
}