Debugging Merge Sort Program with Fortran 90

In summary, the conversation discusses the process of writing a Merge Sort program in Fortran 90. The code provided includes a subroutine for merging two arrays and a program for sorting a list of numbers. However, the program is not working properly and the user is seeking help to identify the bug. The conversation also mentions a discussion about efficient sorting and provides a link to a zip file containing sort algorithms for an array of 64-bit integers.
  • #1
Soff
36
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
  • #2
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
 
  • #3
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.
 

FAQ: Debugging Merge Sort Program with Fortran 90

What is debugging?

Debugging is the process of identifying and fixing errors or bugs in a program. It involves the use of various tools and techniques to locate and correct errors in the code.

Why is it important to debug a merge sort program in Fortran 90?

Debugging a merge sort program in Fortran 90 is important because it ensures that the program runs correctly and efficiently. Even a small error in the code can lead to incorrect results, and debugging helps to identify and fix these errors.

What are some common errors that can occur in a merge sort program in Fortran 90?

Some common errors that can occur in a merge sort program in Fortran 90 include syntax errors, logic errors, and runtime errors. Syntax errors occur when there is a mistake in the syntax of the code, while logic errors occur when the code does not produce the expected results. Runtime errors occur during program execution and can be caused by a variety of factors, such as division by zero or accessing an invalid memory location.

How can I debug a merge sort program in Fortran 90?

There are several ways to debug a merge sort program in Fortran 90. One approach is to use a debugger tool, which allows you to step through the code line by line and observe the values of variables at each step. Another approach is to use print statements to display the values of variables at different points in the code. Additionally, using an IDE (integrated development environment) with built-in debugging features can also be helpful.

How can I prevent errors in a merge sort program in Fortran 90?

While it is not possible to completely prevent errors in a merge sort program, there are some steps that can be taken to minimize the occurrence of errors. These include writing clear and concise code, testing the program with different inputs, and using good programming practices such as commenting and modularization. It is also important to thoroughly test and debug the program before using it in a production environment.

Similar threads

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