C# exercise solutions-C# Arrays: search


C# Arrays exercise: search (con)

Exercise 1: By using the sequential search algorithm, write C# code to search for an element of an integer array of 10 elements.

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
  int[] arr = new int[10] { 23, 2, 3, 34, 6,1,24,45,78,8}; //data set
  int pos,target;
  Console.Write("Enter value to find:");
  target = int.Parse(Console.ReadLine());
  pos = seqsearch(arr, target, 10);
  if (pos != -1)
    Console.WriteLine("The target item was found at location:{0}", pos);
  else
    Console.WriteLine("The target item was not found in the list.\n");
  Console.ReadLine();

}
///sequential search
static int seqsearch(int[] dataset, int target, int n)
{
  int found = 0;
  int i;
  int pos = -1;
  for (i = 0; i < n && found != 1; i++)
    if (target == dataset[i]) { pos = i; found = 1; }

   return pos;
  }
}
}

Exercise 2: Modify the C# code in exercise 1 in order to search for an element of  the array using binary search algorithm.

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{

int[] arr = new int[10] { 23, 2, 3, 34, 6, 1, 24, 45, 78, 8 }; //unsorted data set
int pos, target;
Console.Write("Enter value to find:");
target = int.Parse(Console.ReadLine());
pos = binsearch(arr, 23, 10);
if (pos != -1)
Console.WriteLine("The target item was found at location:{0}", pos);
else
Console.WriteLine("The target item was not found in the list.\n");
Console.ReadLine();

}

///binary search
static int binsearch(int[] dataset,int target, int l,int u){
  insertsort(dataset,dataset.Length);//make sure the list sorted
  while(u>=l){  

    int mid=(l+u)/2;  

    if(target==dataset[mid]) return mid;  

    else if(target>dataset[mid]) l=mid+1;  

    else if(target<dataset[mid]) u=mid-1;  

}
return -1;
}


static void insertsort(int[] dataset, int n)
{

int i, j;
for (i = 1; i < n; i++)
{
int pick_item = dataset[i];
int inserted = 0;
for (j = i - 1; j >= 0 && inserted != 1; )
{
if (pick_item < dataset[j])
{
dataset[j + 1] = dataset[j];
j--;
dataset[j + 1] = pick_item;
}
else inserted = 1;
}
}

}
}
}


Comments

Talal comment

 Talal

I couldn't understand the function of "Variable found" in the first exercise


2018-03-22
Shkar H Sardar comment

 Shkar H Sardar

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int[] shkar = new int[4] { 1, 4, 5, 1 };
int find;
find = Convert.ToInt32(Console.ReadLine());

var output= Array.IndexOf(shkar, find);
Console.WriteLine("The Array Element Postion in {0}",output);



}
}
}


2018-02-14
RD comment

 RD

TEst


2017-05-23
shrill comment

 shrill

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BinarySearch
{
public class Program
{

public void Main(string[] args)
{

int number;
int[] array = new int[] { 1, 3, 4, 5, 6, 7, 8, 9 };
int length = array.Length;
Console.WriteLine("enter the number to be searched from these number ");
for(int i = 0; i <= length - 1; i++)
{
Console.Write( array[i]+" ");

}
Console.WriteLine();
number = Int32.Parse(Console.ReadLine());
BinarySearch(array, length, number);
}
static void BinarySearch(int[] array, int length ,int number)
{
int first = 0, last = length - 1,found = -1;
for (int i = 0; i <= length-1; i++)
{

if (first > last)
{
found = -1;
}
else {
int mid = (first + last)/2;
if (number == array[mid])
found = mid+1;
else if (number > array[mid])
first = mid + 1;
else
last = mid - 1;
}

}
if (found == -1)
Console.WriteLine("Number not found");
else
Console.WriteLine("Number found at " + found+"th Position");
}
}
}


2017-02-07
Jimmy comment

 Jimmy

Hi Guys!
I need help with a program:
write an application that creates and returns a one-dimensional array containing all the elements in the two-dimensional array.store the values in a row major format.For testing purposes,you may do a compile-time initialization of a 12*5 two dimensional array.Display both the two-dimensional and the one-dimensional array.Be sure the values in the array are number aligned.
Thank you!


2015-11-20
iM comment

 iM

array must be sorted in order for binary search to work.


2015-05-19
davyka comment

 davyka

#2
1 - won't compile (BinSearch is not properly called)
2 - int mid=(l+u)/2; //if l+u = 99, invalid Cast
3 - obviously, binary search is not suitable for a chaotic range...


2014-05-11



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.