Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
def linearSearch(arr, targetVal): for i in range(len(arr)): if arr[i] == targetVal: return i return -1 arr = [3, 7, 2, 9, 5] targetVal = 9 result = linearSearch(arr, targetVal) if result != -1: print("Value",targetVal,"found at index",result) else: print("Value",targetVal,"not found") #Python
#include
int linearSearch(int arr[], int size, int targetVal); int main() { int arr[] = {3, 7, 2, 9, 5}; int targetVal = 9; int size = sizeof(arr) / sizeof(arr[0]); int result = linearSearch(arr, size, targetVal); if (result != -1) { printf("Value %d found at index %d\n", targetVal, result); } else { printf("Value %d not found\n", targetVal); } return 0; } int linearSearch(int arr[], int size, int targetVal) { for (int i = 0; i < size; i++) { if (arr[i] == targetVal) { return i; } } return -1; } //C
public class Main { public static void main(String[] args) { int[] arr = {3, 7, 2, 9, 5}; int targetVal = 9; int result = linearSearch(arr, targetVal); if (result != -1) { System.out.println("Value " + targetVal + " found at index " + result); } else { System.out.println("Value " + targetVal + " not found"); } } public static int linearSearch(int[] arr, int targetVal) { for (int i = 0; i < arr.length; i++) { if (arr[i] == targetVal) { return i; } } return -1; } } //Java
Python result:
C result:
Java result:
Value 9 found at index 3
Value 9 found at index 3
Value 9 found at index 3