#! /bin/sh # # get_percentile ext_file percentile # # sorts the extension file (specified by -e on modelfree command line) # after increasing SSE (2nd column). # prints the SSE(percentile) to standard output. # # uses the heapsort algorithm, see: # Aho et al. (1988) "The awk Programming Language", Addison-Wesley, p. 165 ff. # nawk ' BEGIN { percentile = ARGV[2]; ARGC=ARGC-1 ; # print "percentile = " percentile } { line[NR] = $0; SSE[NR] = $2; } END { cutline = int(FNR * percentile/100 + 0.5) ; # print "cutline = ", cutline hsort(SSE, NR) for (i=1; i<=NR; i++) { if( i == cutline ) { print "SSE (" percentile ") = " SSE[i] } } } function hsort(A,n,i) { for (i = int(n/2); i >= 1; i--) { heapify(A,i,n) } for (i=n; i>1; i--) { swap(A,1,i) heapify(A,1,i-1) } } function heapify(A,left,right,p,c) { for (p=left; (c=2*p) <= right; p=c) { if( c A[c]) { c++ } if( A[p] < A[c] ) { swap(A,c,p) } } } function swap(A,i,j,t) { t=A[i]; A[i]=A[j]; A[j]=t } ' $1 $2