|
JJW wrote:
> Hi *ALL
>
> Does anyone have small working examples of BSEARCH and
> QSORT they can send me?
Sure:
-------------------------------------------------------------------------
H dftactgrp(*no) bnddir('QC2LE')
D qsort pr extproc('qsort')
D base * value
D num 10i 0 value
D width 10i 0 value
D compare * procptr value
D bsearch pr * extproc('bsearch')
D key * value
D base * value
D num 10i 0 value
D size 10i 0 value
D compare * procptr value
D compare5a pr 10i 0
D element1 5a const
D element2 5a const
D array s 5a dim(10) ctdata perrcd(10)
D i s 10i 0
D elem s like(array) based(p)
D key s like(array)
/free
// sort the array and show results
qsort (%addr(array): %elem(array): %size(array):
%paddr(compare5a));
for i = 1 to %elem(array);
dsply array(i);
endfor;
// Search for something in the array
key = 'hhhhh';
p = bsearch (%addr(key): %addr(array): %elem(array):
%size(array): %paddr(compare5a));
dsply elem;
// Compute index of item in the array
i = %div(p - %addr(array):%size(array)) + 1;
dsply i;
*inlr = *on;
/end-free
P compare5a b
D compare5a pi 10i 0
D element1 5a const
D element2 5a const
/free
if element1 < element2;
return -1;
elseif element1 > element2;
return 1;
endif;
return 0;
/end-free
P compare5a e
**CTDATA array
dddddhhhhhooooouuuuutttttmmmmmwwwwwlllllqqqqqppppp
-------------------------------------------------------------------------
Oh yeah, be sure to check if bsearch() returns a NULL value, which
means the item was not found.
Cheers! Hans
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.