The bubble sort algorithm is implemented using two nested loops. The outer loop (i) iterates from 1 to n-1, and the inner loop (j) iterates from 1 to n-i.
Within the inner loop, adjacent elements are compared, and if they are out of order, they are swapped.
Finally, the sorted array is printed after the sorting process is complete.
Bubble Sort algorithm in Fortran (user input the numbers):
program BubbleSort
implicit none
integer, parameter :: n = 10 ! Number of elements in the array
integer :: array(n)
integer :: i, j, temp
write(*,*) "Enter", n, "numbers:"
! Read input numbers from the user
do i = 1, n
read(*,*) array(i)
end do
write(*,*) "Array before sorting:"
do i = 1, n
write(*,*) array(i)
end do
! Bubble sort algorithm
do i = 1, n-1
do j = 1, n-i
if (array(j) > array(j+1)) then
! Swap the elements
temp = array(j)
array(j) = array(j+1)
array(j+1) = temp
end if
end do
end do
write(*,*) "Array after sorting:"
do i = 1, n
write(*,*) array(i)
end do
end program BubbleSort