Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions CSharpLinearSearch/LinearSearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<int> myList = new List<int>() { 3, 9, 1, 2, 5, 100, 2123, 54, 34, 123, 534 };
Console.Write("List = ");
PrintList(myList);
int index = LinearSearch(myList, 1);
Console.WriteLine("1 found at " + index);
index = LinearSearch(myList, 4);
Console.WriteLine("4 found at " + index);
Console.WriteLine("Press enter when done");
Console.ReadLine();
}
public static int LinearSearch(List<int> list, int target)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i] == target)
{
return i;
}
}
return -1;
}
public static void PrintList(List<int> list)
{
foreach (int item in list)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
}
}