Created
May 12, 2021 02:09
-
-
Save S-codes14/fb9663d9f1d7c137f47d3def439b6e93 to your computer and use it in GitHub Desktop.
sequentialSearch in c#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C# program for Indexed Sequential Search | |
using System; | |
class Program { | |
static void sequentialSearch(int []arr, int n, int k) | |
{ | |
int []elements = new int[20]; | |
int []indices = new int[20]; | |
int i; | |
int j = 0, ind = 0, start=0, end=0, set = 0; | |
for (i = 0; i < n; i += 3) { | |
// Storing element | |
elements[ind] = arr[i]; | |
// Storing the index | |
indices[ind] = i; | |
ind++; | |
} | |
if (k < elements[0]) { | |
Console.Write("Not found"); | |
return; | |
} | |
else { | |
for (i = 1; i <= ind; i++) | |
if (k <= elements[i]) { | |
start = indices[i - 1]; | |
set = 1; | |
end = indices[i]; | |
break; | |
} | |
} | |
if(set == 0) | |
{ | |
start = indices[i-1]; | |
end = n-1; | |
} | |
for (i = start; i <= end; i++) { | |
if (k == arr[i]) { | |
j = 1; | |
break; | |
} | |
} | |
if (j == 1) | |
Console.WriteLine("the target item was found at location: "+ i); | |
else | |
Console.WriteLine("the target item was not found in the list"); | |
} | |
// Driver code | |
public static void Main (string[] args) { | |
int []arr = { 6, 7, 8, 9, 10 }; | |
int n = arr.Length; | |
// Element to search | |
Console.WriteLine("Enter value to find on the next line:"); | |
int k = Convert.ToInt32(Console.ReadLine()); | |
Console.WriteLine("you entered: " + k); | |
// int k = 10; | |
sequentialSearch(arr, n, k); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment