use*_*219 2 sorting algorithm fortran bubble-sort
我有问题编译冒泡排序算法,我不知道我做错了什么.如果有人帮助我,我会非常感激.
这是代码:
program bubble
integer, dimension(6) :: vec
integer :: temp, bubble, lsup, j
read *, vec !the user needs to put 6 values on the array
lsup = 6 !lsup is the size of the array to be used
do while (lsup > 1)
bubble = 0 !bubble in the greatest element out of order
do j = 1, (lsup-1)
if vet(j) > vet(j+1) then
temp = vet(j)
vet(j) = vet(j+1)
vet(j+1) = temp
bubble = j
endif
enddo
lsup = bubble
enddo
print *, vet
end program
Run Code Online (Sandbox Code Playgroud)
谢谢!
您的代码中存在各种问题:
vet而不是vec这是一个干净的解决方案:
program bubble_test
implicit none
integer, dimension(6) :: vec
integer :: temp, bubble, lsup, j
read *, vec !the user needs to put 6 values on the array
lsup = 6 !lsup is the size of the array to be used
do while (lsup > 1)
bubble = 0 !bubble in the greatest element out of order
do j = 1, (lsup-1)
if (vec(j) > vec(j+1)) then
temp = vec(j)
vec(j) = vec(j+1)
vec(j+1) = temp
bubble = j
endif
enddo
lsup = bubble
enddo
print *, vec
end program
Run Code Online (Sandbox Code Playgroud)
您的编码可以进一步改进......请参阅此示例.