1 subroutine da_trace_int_sort(&
6 !----------------------------------------------------------------------
7 ! Purpose: sort integers for tracing
8 !----------------------------------------------------------------------
12 integer, intent(in) :: n ! The number of items to be sorted.
13 integer, 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
27 ! Do heapsort: Create the heap...
28 makeheap : do i=n/2,1,-1
31 ! find the largest out of the head and its two children...
33 if (child>n) exit sift1
35 if (key(index(child+1))>key(index(child))) child=child+1
37 ! if the head is the largest, then sift is done...
38 if (key(index(head))>=key(index(child))) exit sift1
39 ! otherwise swap to put the largest child at the head,
40 ! and prepare to repeat the procedure for the head in its new
41 ! subordinate position.
43 index(child)=index(head)
49 ! Retire heads of the heap, which are the largest, and
50 ! 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_int_sort