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

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

Fortran Merge Sort Implementation

Fortran

Goal -- WPM

Ready
Exercise Algorithm Area
1PROGRAM merge_sort_example
2IMPLICIT NONE
3
4INTEGER, PARAMETER :: ARRAY_SIZE = 10
5INTEGER :: arr(ARRAY_SIZE)
6INTEGER :: i
7
8! Initialize array with some values
9arr = [5, 2, 8, 1, 9, 4, 7, 3, 6, 0]
10
11PRINT *, "Original array:"
12CALL print_array(arr, ARRAY_SIZE)
13
14CALL merge_sort(arr, 1, ARRAY_SIZE)
15
16PRINT *, "Sorted array:"
17CALL print_array(arr, ARRAY_SIZE)
18
19CONTAINS
20
21SUBROUTINE merge_sort(a, low, high)
22IMPLICIT NONE
23INTEGER, INTENT(INOUT) :: a(:)
24INTEGER, INTENT(IN) :: low, high
25INTEGER :: mid
26
27IF (low < high) THEN
28mid = low + (high - low) / 2
29CALL merge_sort(a, low, mid)
30CALL merge_sort(a, mid + 1, high)
31CALL merge(a, low, mid, high)
32END IF
33END SUBROUTINE merge_sort
34
35SUBROUTINE merge(a, low, mid, high)
36IMPLICIT NONE
37INTEGER, INTENT(INOUT) :: a(:)
38INTEGER, INTENT(IN) :: low, mid, high
39INTEGER, ALLOCATABLE :: temp_left(:), temp_right(:)
40INTEGER :: n1, n2, i, j, k
41
42n1 = mid - low + 1
43n2 = high - mid
44
45ALLOCATE(temp_left(n1), temp_right(n2))
46
47! Copy data to temporary arrays
48DO i = 1, n1
49temp_left(i) = a(low + i - 1)
50END DO
51DO j = 1, n2
52temp_right(j) = a(mid + 1 + j - 1)
53END DO
54
55! Merge the temporary arrays back into a[low..high]
56i = 1
57j = 1
58k = low
59DO WHILE (i <= n1 .AND. j <= n2)
60IF (temp_left(i) <= temp_right(j)) THEN
61a(k) = temp_left(i)
62i = i + 1
63ELSE
64a(k) = temp_right(j)
65j = j + 1
66END IF
67k = k + 1
68END DO
69
70! Copy the remaining elements of temp_left[], if any
71DO WHILE (i <= n1)
72a(k) = temp_left(i)
73i = i + 1
74k = k + 1
75END DO
76
77! Copy the remaining elements of temp_right[], if any
78DO WHILE (j <= n2)
79a(k) = temp_right(j)
80j = j + 1
81k = k + 1
82END DO
83
84DEALLOCATE(temp_left, temp_right)
85END SUBROUTINE merge
86
87SUBROUTINE print_array(a, size)
88IMPLICIT NONE
89INTEGER, INTENT(IN) :: a(:), size
90INTEGER :: i
91DO i = 1, size
92PRINT *, a(i), " "
93END DO
94PRINT *
95END SUBROUTINE print_array
96
97END PROGRAM merge_sort_example
Algorithm description viewbox

Fortran Merge Sort Implementation

Algorithm description:

This Fortran program implements the merge sort algorithm, a classic divide-and-conquer sorting technique. It recursively divides the input array into smaller halves until each subarray contains only one element, then merges these sorted subarrays back together to produce a fully sorted array. This is commonly used in scenarios where stable sorting is required or when dealing with large datasets that might not fit entirely into memory.

Algorithm explanation:

Merge sort has a time complexity of O(n log n) in all cases (best, average, and worst) because it consistently divides the problem into halves and performs a linear-time merge operation. The space complexity is O(n) due to the need for temporary arrays during the merge step. Edge cases include empty arrays (handled by the base case of recursion where low >= high), arrays with one element, and arrays with duplicate elements. The correctness relies on the invariant that at each step of the merge, the subarrays being merged are already sorted, and the merge procedure correctly combines them into a larger sorted subarray. The recursive structure ensures that the entire array is eventually sorted.

Pseudocode:

FUNCTION merge_sort(array, low, high):
  IF low < high:
    mid = low + (high - low) / 2
    merge_sort(array, low, mid)
    merge_sort(array, mid + 1, high)
    merge(array, low, mid, high)

FUNCTION merge(array, low, mid, high):
  Create temp_left array of size (mid - low + 1)
  Create temp_right array of size (high - mid)
  Copy elements from array[low..mid] to temp_left
  Copy elements from array[mid+1..high] to temp_right
  Initialize i = 0, j = 0, k = low
  WHILE i < size(temp_left) AND j < size(temp_right):
    IF temp_left[i] <= temp_right[j]:
      array[k] = temp_left[i]
      i = i + 1
    ELSE:
      array[k] = temp_right[j]
      j = j + 1
    k = k + 1
  WHILE i < size(temp_left):
    array[k] = temp_left[i]
    i = i + 1
    k = k + 1
  WHILE j < size(temp_right):
    array[k] = temp_right[j]
    j = j + 1
    k = k + 1