用Fortran对20,18,16,29,32进行冒泡排序
时间: 2024-04-29 17:25:31 浏览: 80
以下是用Fortran对20,18,16,29,32进行冒泡排序的示例代码:
```
program bubble_sort
implicit none
integer :: i, j, n
integer :: arr(5) = [20, 18, 16, 29, 32]
n = size(arr)
do i = 1, n-1
do j = 1, n-i
if (arr(j) > arr(j+1)) then
call swap(arr(j), arr(j+1))
end if
end do
end do
write(*,*) "Sorted Array:"
do i = 1, n
write(*,*) arr(i)
end do
contains
subroutine swap(a, b)
implicit none
integer, intent(inout) :: a, b
integer :: temp
temp = a
a = b
b = temp
end subroutine swap
end program bubble_sort
```
输出结果为:
```
Sorted Array:
16
18
20
29
32
```
相关问题
fortran 冒泡排序
可以使用Fortran语言实现冒泡排序算法。下面是一个示例代码:
```fortran
program bubble_sort
implicit none
integer, parameter :: n = 10
integer :: arr(n)
integer :: i, j, temp
! 初始化数组
arr = [9, 4, 2, 7, 5, 1, 8, 3, 6, 0]
! 冒泡排序
do i = 1, n-1
do j = 1, n-i
if (arr(j) > arr(j+1)) then
temp = arr(j)
arr(j) = arr(j+1)
arr(j+1) = temp
end if
end do
end do
! 输出排序后的数组
write(*,*) "排序后的数组:"
do i = 1, n
write(*,*) arr(i)
end do
end program bubble_sort
```
输出结果为:
```
排序后的数组:
0
1
2
3
4
5
6
7
8
9
```
用Fortran写一个冒泡排序
冒泡排序的 Fortran 代码如下:
program bubble_sort
implicit none
integer :: n, i, j, temp
integer, dimension(100) :: arr
write(*, "('Enter the number of elements: ')", advance="no")
read(*, *) n
write(*, "('Enter the elements: ')")
do i = 1, n
read(*, *) arr(i)
end do
do i = 1, n-1
do j = 1, n-i
if (arr(j) > arr(j+1)) then
temp = arr(j)
arr(j) = arr(j+1)
arr(j+1) = temp
end if
end do
end do
write(*, "('Sorted array: ')")
do i = 1, n
write(*, *) arr(i)
end do
end program bubble_sort
阅读全文