Sorting
Quick Sort (Lomuto)
Choose the last item as a pivot, partition smaller values to its left, then recurse on the two sides.
Algorithm
The checked-in replay follows the same small input and final output across all 21 DSA books, so this Fortran DSA implementation can be compared directly with the other languages.
pivot
The final element is moved to the boundary between smaller and larger values.
partition
One scan rearranges the current range before the recursive calls.
Visual walkthrough
Basic Implementation
basic.f90
program sort_quick_lomuto
implicit none
integer :: arr(5) = [4, 1, 5, 2, 3]
call quick_sort(arr, 1, 5)
print '(*(I0,1X))', arr
contains
integer function partition(arr, low, high)
integer, intent(inout) :: arr(:)
integer, intent(in) :: low, high
integer :: pivot, i, j, tmp
pivot = arr(high)
i = low - 1
do j = low, high - 1
if (arr(j) <= pivot) then
i = i + 1
tmp = arr(i); arr(i) = arr(j); arr(j) = tmp
end if
end do
tmp = arr(i + 1); arr(i + 1) = arr(high); arr(high) = tmp
partition = i + 1
end function partition
recursive subroutine quick_sort(arr, low, high)
integer, intent(inout) :: arr(:)
integer, intent(in) :: low, high
integer :: pivot_index
if (low < high) then
pivot_index = partition(arr, low, high)
call quick_sort(arr, low, pivot_index - 1)
call quick_sort(arr, pivot_index + 1, high)
end if
end subroutine quick_sort
end program sort_quick_lomuto
Complexity
- Time: O(n^2) worst, O(n log n) average
- Space: O(log n) average call stack
- Stable: no
Implementation notes
- Keep the explicit algorithmic steps instead of calling a standard-library sort. This checked-in replay shows each comparison and swap in the first partition, then summarizes the recursive calls on already-sorted sides.
- The implementation is intentionally compact for learning and replay, not a production sorting utility.