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

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

Fortran Heap Sort Implementation

Fortran

Goal -- WPM

Ready
Exercise Algorithm Area
1PROGRAM heap_sort_example
2IMPLICIT NONE
3
4INTEGER, PARAMETER :: ARRAY_SIZE = 10
5INTEGER :: arr(ARRAY_SIZE)
6INTEGER :: i
7
8! Initialize array with some values
9arr = [4, 1, 3, 9, 7, 5, 8, 2, 6, 0]
10
11PRINT *, "Original array:"
12CALL print_array(arr, ARRAY_SIZE)
13
14CALL heap_sort(arr, ARRAY_SIZE)
15
16PRINT *, "Sorted array:"
17CALL print_array(arr, ARRAY_SIZE)
18
19CONTAINS
20
21SUBROUTINE heap_sort(a, n)
22IMPLICIT NONE
23INTEGER, INTENT(INOUT) :: a(:)
24INTEGER, INTENT(IN) :: n
25INTEGER :: i
26
27! Build a maxheap.
28! Since last parent will be at (n/2) - 1, we can start from there.
29DO i = n / 2, 1, -1
30CALL heapify(a, n, i)
31END DO
32
33! One by one extract an element from heap
34DO i = n, 2, -1
35! Move current root to end
36CALL swap(a(1), a(i))
37
38! call max heapify on the reduced heap
39CALL heapify(a, i - 1, 1)
40END DO
41END SUBROUTINE heap_sort
42
43SUBROUTINE heapify(a, n, i)
44IMPLICIT NONE
45INTEGER, INTENT(INOUT) :: a(:)
46INTEGER, INTENT(IN) :: n, i
47INTEGER :: largest
48INTEGER :: left, right
49
50largest = i ! Initialize largest as root
51left = 2 * i ! left child index
52right = 2 * i + 1 ! right child index
53
54! If left child is larger than root
55IF (left <= n .AND. a(left) > a(largest)) THEN
56largest = left
57END IF
58
59! If right child is larger than largest so far
60IF (right <= n .AND. a(right) > a(largest)) THEN
61largest = right
62END IF
63
64! If largest is not root
65IF (largest /= i) THEN
66CALL swap(a(i), a(largest))
67
68! Recursively heapify the affected sub-tree
69CALL heapify(a, n, largest)
70END IF
71END SUBROUTINE heapify
72
73SUBROUTINE swap(x, y)
74IMPLICIT NONE
75INTEGER, INTENT(INOUT) :: x, y
76INTEGER :: temp
77temp = x
78x = y
79y = temp
80END SUBROUTINE swap
81
82SUBROUTINE print_array(a, size)
83IMPLICIT NONE
84INTEGER, INTENT(IN) :: a(:), size
85INTEGER :: i
86DO i = 1, size
87PRINT *, a(i), " "
88END DO
89PRINT *
90END SUBROUTINE print_array
91
92END PROGRAM heap_sort_example
Algorithm description viewbox

Fortran Heap Sort Implementation

Algorithm description:

This Fortran program implements Heap Sort, a comparison-based sorting algorithm that uses a binary heap data structure. It first builds a max-heap from the input array, then repeatedly extracts the maximum element from the heap (which is the root) and places it at the end of the sorted portion of the array. Heap sort is efficient and can be performed in-place. It's useful in scenarios where memory is a concern and a guaranteed O(n log n) performance is required.

Algorithm explanation:

Heap sort has a time complexity of O(n log n) for all cases (best, average, and worst). Building the initial heap takes O(n) time, and each of the n extraction operations takes O(log n) time. The space complexity is O(1) because it sorts the array in-place. Edge cases include empty arrays or arrays with a single element (handled by loop conditions), and arrays with duplicate elements. The correctness relies on the `heapify` procedure maintaining the max-heap property: the value of a parent node is always greater than or equal to the values of its children. This ensures that the largest element is always at the root.

Pseudocode:

FUNCTION heap_sort(array, n):
  FOR i FROM n/2 DOWN TO 1:
    heapify(array, n, i)

  FOR i FROM n DOWN TO 2:
    SWAP array[1], array[i]
    heapify(array, i-1, 1)

FUNCTION heapify(array, n, i):
  largest = i
  left = 2*i
  right = 2*i + 1

  IF left <= n AND array[left] > array[largest]:
    largest = left
  IF right <= n AND array[right] > array[largest]:
    largest = right

  IF largest != i:
    SWAP array[i], array[largest]
    heapify(array, n, largest)