[英]Fortran Bubble Sort Algorithm
I'm with problems to compile a Bubble sort algorithm, I dont know what I'm doing wrong. 我在编译冒泡排序算法时遇到问题,我不知道自己在做什么错。 I will appreciate so much if somebody helps me.
如果有人帮助我,我将非常感激。
This is the code: 这是代码:
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
Thanks! 谢谢!
You had various issues in your code: 您的代码中遇到各种问题:
vet
instead of vec
vet
代替vec
Here is a clean solution: 这是一个干净的解决方案:
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
Your coding can be further improved... See this example . 您的编码可以进一步改进...请参阅此示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.