ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Fortran Linear Search Implementation

Fortran

Goal -- WPM

Ready
Exercise Algorithm Area
1PROGRAM linear_search_example
2IMPLICIT NONE
3
4INTEGER, PARAMETER :: ARRAY_SIZE = 10
5INTEGER :: arr(ARRAY_SIZE)
6INTEGER :: i
7INTEGER :: search_value
8INTEGER :: result_index
9
10! Initialize array with some values
11arr = [12, 45, 67, 23, 89, 54, 90, 34, 78, 11]
12
13PRINT *, "Array elements:"
14DO i = 1, ARRAY_SIZE
15PRINT *, arr(i), " "
16END DO
17PRINT *
18
19! Test case 1: Element found
20search_value = 23
21PRINT *, "Searching for: ", search_value
22result_index = linear_search(arr, ARRAY_SIZE, search_value)
23IF (result_index /= -1) THEN
24PRINT *, "Element ", search_value, " found at index: ", result_index
25ELSE
26PRINT *, "Element ", search_value, " not found."
27END IF
28PRINT *
29
30! Test case 2: Element not found
31search_value = 100
32PRINT *, "Searching for: ", search_value
33result_index = linear_search(arr, ARRAY_SIZE, search_value)
34IF (result_index /= -1) THEN
35PRINT *, "Element ", search_value, " found at index: ", result_index
36ELSE
37PRINT *, "Element ", search_value, " not found."
38END IF
39PRINT *
40
41CONTAINS
42
43FUNCTION linear_search(a, size, value)
44IMPLICIT NONE
45INTEGER, INTENT(IN) :: a(:), size, value
46INTEGER :: linear_search
47INTEGER :: i
48
49! Iterate through the array
50DO i = 1, size
51IF (a(i) == value) THEN
52linear_search = i ! Return the index if found
53RETURN
54END IF
55END DO
56
57! If the loop finishes without finding the element
58linear_search = -1
59END FUNCTION linear_search
60
61END PROGRAM linear_search_example
Algorithm description viewbox

Fortran Linear Search Implementation

Algorithm description:

This Fortran program implements a basic linear search algorithm. It iterates through an array sequentially, comparing each element with the target value. If a match is found, it returns the index of that element; otherwise, it returns a special value (like -1) to indicate that the element was not found. Linear search is simple to implement and is suitable for small datasets or when the data is not sorted.

Algorithm explanation:

Linear search has a time complexity of O(n) in the worst and average cases, as it may need to examine every element in the array. In the best case (element is the first one), it's O(1). The space complexity is O(1) as it only uses a few variables for iteration and comparison. Edge cases include searching in an empty array (the loop condition `i = 1, size` would handle this if `size` is 0 or negative, though Fortran arrays are typically 1-indexed and `size` would be positive), searching for the first or last element, and searching for an element that is not present in the array. The algorithm is correct because it systematically checks every possible position for the target value.

Pseudocode:

FUNCTION linear_search(array, size, value):
  FOR i FROM 1 TO size:
    IF array[i] == value:
      RETURN i
  RETURN -1