Skip to content

Commit b74bdc8

Browse files
authored
Merge pull request #390 from golasa97/main
Replacing bubblesort in PURR with quicksort
2 parents ee9c4d0 + 329a8c1 commit b74bdc8

1 file changed

Lines changed: 40 additions & 15 deletions

File tree

src/purr.f90

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,7 @@ subroutine unrest(bkg,sig0,sigf,tabl,tval,er,gnr,gfr,ggr,gxr,gt,&
19101910
els(itemp,ie)=spot
19111911
enddo
19121912
enddo
1913-
call fsort(es,xs,ne,1)
1913+
call fsort(es,ne)
19141914

19151915
!--loop over sequences
19161916
do 170 k=1,nseq0
@@ -2277,7 +2277,7 @@ subroutine unrest(bkg,sig0,sigf,tabl,tval,er,gnr,gfr,ggr,gxr,gt,&
22772277
do ie=1,ne
22782278
es(ie)=els(itemp,ie)+fis(itemp,ie)+cap(itemp,ie)+bkg(1)
22792279
enddo
2280-
call fsort(es,xs,ne,1)
2280+
call fsort(es,ne)
22812281
tmin(itemp)=es(1)
22822282
tmax(itemp)=es(ne)
22832283
nebin=int(nsamp/(nbin-10+1.76))
@@ -2780,7 +2780,7 @@ subroutine uw2(rez,aim1,rew,aimw)
27802780
return
27812781
end subroutine uw2
27822782

2783-
subroutine fsort(x,y,n,i)
2783+
subroutine fsort(x,n)
27842784
!-------------------------------------------------------------------
27852785
! Floating-point sort routine.
27862786
! Sort x and y into increasing x order.
@@ -2792,21 +2792,46 @@ subroutine fsort(x,y,n,i)
27922792
integer::k,j
27932793
real(kr)::xt,yt
27942794

2795-
do k=1,n-1
2796-
do j=k+1,n
2797-
if (x(k).gt.x(j)) then
2798-
xt=x(k)
2799-
yt=y(k)
2800-
x(k)=x(j)
2801-
y(k)=y(j)
2802-
x(j)=xt
2803-
y(j)=yt
2804-
endif
2805-
enddo
2806-
enddo
2795+
if (n <= 1) return
2796+
call quicksort(x,1,n)
2797+
28072798
return
28082799
end subroutine fsort
28092800

2801+
recursive subroutine quicksort(a,l,r)
2802+
real(kr), intent(inout) :: a(:)
2803+
integer, intent(in) :: l, r
2804+
integer :: i, j
2805+
real(kr) :: pivot, ta
2806+
2807+
i = l
2808+
j = r
2809+
pivot = a((l+r)/2)
2810+
2811+
do
2812+
do while (a(i) < pivot)
2813+
i = i + 1
2814+
end do
2815+
do while (a(j) > pivot)
2816+
j = j - 1
2817+
end do
2818+
2819+
if (i <= j) then
2820+
ta = a(i)
2821+
a(i) = a(j)
2822+
a(j) = ta
2823+
2824+
i = i + 1
2825+
j = j - 1
2826+
end if
2827+
2828+
if (i > j) exit
2829+
end do
2830+
2831+
if (l < j) call quicksort(a,l,j)
2832+
if (i < r) call quicksort(a,i,r)
2833+
end subroutine quicksort
2834+
28102835
subroutine fsrch(x,xarray,n,i,k)
28112836
!-------------------------------------------------------------------
28122837
! Floating-point search routine.

0 commit comments

Comments
 (0)