Fortran Debugging Merge Sort Program with Fortran 90

AI Thread Summary
The discussion revolves around writing a Merge Sort program in Fortran 90. The initial code attempts to read a list of numbers and sort them using a Merge subroutine, but it encounters errors. Key issues identified include the failure to initialize the array "b" and incorrectly printing the unsorted array "a" instead of the sorted output "c". The user then presents an improved version of the code, which still does not function correctly. The thread also references additional resources for sorting algorithms, including links to source code for efficient sorting techniques. The conversation highlights the importance of correctly managing array initialization and output in sorting algorithms.
Soff
Messages
36
Reaction score
0
I'm trying to write a Merge Sort program with fortran 90. However, this is what I already could do:

Code:
program sorting

integer :: i,ialloc,error,n,d
integer, pointer :: a(:)

Print *,'How many numbers would you like to type in?'
Read(*,*)d

Print*,' Enter the values, separated by commas'

Read(*,*)(a(t),t=1, d)

!Result
  call Merge(a,b,c,m,n)
    print *,'Sorted list:'
    write(*,*) (a(i),i=0,n-1)
  print *,' '

end program sorting

!Merge Sorting of a() of dimension m and b() of dimension n
!into c() of size m+n=d
Subroutine Merge(a,b,c,m,n)

integer :: m,n,a(0:m-1),b(0:n-1),c(0:m+n-1)
integer :: i,j,k,d
  i=0; j=0; k=0 !Start
  do while (i<m.and.j<n)
    if (a(i)<b(j)) then
      c(k)=a(i)
      i=i+1
    else
      c(k)=b(j)
      j=j+1
    end if
    k=k+1
  end do
  do while (i<m)  !pick up any remainder
    c(k)=a(i)
    k=k+1; i=i+1
  end do
  do while (j<n)
    c(k)=b(j)
    k=k+1; j=j+1
  end do
  return
End

It starts as it was planed, but after typing in the numbers, I get an error message. So, can anyone help me? Where is the bug?
 
Technology news on Phys.org
Okay, I started again. This is my improved version (but still not working!):

Code:
program sorting

integer :: d,length,center,t,i,n,m
integer :: a(128), b(128)

!List of numbers
Print *,'How many numbers would you like to type in?'
Read(*,*)d

Print*,' Enter the values, separated by commas'
Read(*,*)(a(t),t=1, d)


length=d

i=1
do while (length>=2)
	
	length=length/2
	i=i+1
end do

write(*,*)i,length
n=1


do while (n<=i)
	if (a(2*n)>a(2*n-1)) then
		b(2*n)=a(2*n)
		b(2*n-1)=a(2*n-1)
	else 	
		b(2*n)=a(2*n-1)
		b(2*n-1)=a(2*n)	
	end if
	n=n+1
end do

write(*,*)(b(t),t=1,d)

a(i)=b(i)

n=1
m=1
length=length*2

do while (n<=i/2)
	do while (m<=length-1.and.a(length*n)>=a(length*n-m))
		m=m+1
	end do
	b(n*length-length-1-m)=a(length*n)
	do while (m<=length-1.and.a(length*n-1)>=a(length*n-m))
		m=m+1
	end do
	b(n*length-length-1-m)=a(length*n-1)
	do while (m<=length-1.and.a(length*n-2)>=a(length*n-m))
		m=m+1
	end do
	b(n*length-length-1-m)=a(length*n-1)
	do while (m<=length-1.and.a(length*n-3)>=a(length*n-m))
		m=m+1
	end do
n=n+1
end do

write(*,*)(b(t),t=1,d)
end program
 
In the first example, you're not sorting but merging "a" and "b" to create "c". "b" is never initialized, and you're printing "a", instead of the output, "c".

Sort algorithms (plus links to zips of source code) were discussed in this thread:

efficient sorting

Here is a direct link to the sorts that sort an array (vector) of 64 bit integers, msort.cpp is a merge sort.

sortv.zip

My merge sort is pointer oriented, if I get the time, I'll make a duplicate that uses indexes instead.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
12
Views
2K
Replies
4
Views
2K
Replies
3
Views
2K
Replies
20
Views
4K
Back
Top