Closest-pair problem/Fortran: Difference between revisions

m
Fixed syntax highlighting.
(moved from Closest pair problem)
 
m (Fixed syntax highlighting.)
 
(One intermediate revision by one other user not shown)
Line 5:
This module defines the point type and implements only two operations on "vectors" ("difference" and "length")
 
<langsyntaxhighlight lang="fortran">module Points_Module
implicit none
 
Line 39:
end function pt_len
 
end module Points_Module</langsyntaxhighlight>
 
Then we need a modified version of the [[Quicksort#Fortran|fortran quicksort]] able to handle "points" and that can use a custom comparator.
 
<langsyntaxhighlight lang="fortran">module qsort_module
use Points_Module
implicit none
Line 109:
end subroutine partition
 
end module qsort_module</langsyntaxhighlight>
 
The module containing the custom comparators.
 
<langsyntaxhighlight lang="fortran">module Order_By_XY
use Points_Module
implicit none
Line 140:
end if
end function order_by_y
end module Order_By_XY</langsyntaxhighlight>
 
The '''closest pair''' functions' module.
 
<langsyntaxhighlight lang="fortran">module ClosestPair
use Points_Module
use Order_By_XY
Line 278:
end function closest_pair_real
 
end module ClosestPair</langsyntaxhighlight>
 
Testing:
 
<langsyntaxhighlight lang="fortran">program TestClosestPair
use ClosestPair
implicit none
Line 307:
dr = closest_pair(points, p)
print *, "rec ", dr
end program TestClosestPair</langsyntaxhighlight>
 
<tt>Time</tt> gave 2.92user 0.00system 0:02.94elapsed for brute force, and 0.02user 0.00system 0:00.03elapsed for the other one.
9,476

edits