1 subroutine da_trace_real_sort(&
6 !-----------------------------------------------------------------------
7 ! Purpose: Sort reals for tracing
8 !-----------------------------------------------------------------------
12 integer, intent(in) :: n ! The number of items to be sorted.
13 real, intent(in) :: key(:)
14 integer, intent(out) :: index(:)
16 integer :: head ! heaps are tree structures: head and child refer
17 integer :: child ! to related items within the tree
19 integer :: dum ! used to swap index items
26 ! Do heapsort: Create the heap...
27 makeheap : do i=n/2,1,-1
30 ! find the largest out of the head and its two children...
32 if (child>n) exit sift1
34 if (key(index(child+1))>key(index(child))) child=child+1
36 ! if the head is the largest, then sift is done...
37 if (key(index(head))>=key(index(child))) exit sift1
38 ! otherwise swap to put the largest child at the head,
39 ! and prepare to repeat the procedure for the head in its new
40 ! subordinate position.
42 index(child)=index(head)
48 ! Retire heads of the heap, which are the largest, and
49 ! stack them at the end of the array.
56 ! second sift is similar to first...
59 if (child>(i-1)) exit sift2
61 if (key(index(child+1))>key(index(child))) child=child+1
63 if (key(index(head))>=key(index(child))) exit sift2
65 index(child)=index(head)
71 end subroutine da_trace_real_sort