home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
norge.freeshell.org (192.94.73.8)
/
192.94.73.8.tar
/
192.94.73.8
/
pub
/
computers
/
cpm
/
alphatronic
/
BBUG13.ZIP
/
SORT.DOC
< prev
next >
Wrap
Text File
|
1998-07-30
|
7KB
|
165 lines
.pn 38
.fo Appendix C page #
SORT
SORTé i≤ ß genera∞ purposσ sor⌠ utility« I⌠ caε handlσ ver∙ ì
largσ file≤ (dependinτ oε diskettσ storagσ space⌐ anΣ i≤ ablσ ì
t∩á sor⌠ oε ß recorΣ basi≤ a≤ wel∞ a≤ ß linσ basis«á I⌠á i≤ ì
no⌠á intendeΣá t∩ bσ ßá gloriou≤á all-purpose-whizz-bang-ì
everything-but-the-kitchen-sinδ sor⌠ program« I⌠ ha≤ beeε ì
pu⌠á oεá thσá deliver∙ disδ a≤ aε afterthought«á S∩á iµá i⌠ ì
doesn'⌠á fi⌠ al∞ you≥ sortinτ needs¼á kee≡ iε minΣ tha⌠á yo⌡ ì
go⌠ i⌠ fo≥ free¼ anΣ don'⌠ expec⌠ ß lo⌠ oµ new-and-improved-ì
better-than-eve≥ updates.
Thσá sortinτ algorithφ employeΣ i≤ thσ onσ developeΣ b∙á C« ì
A«á R«á Hoarσá calleΣá thσ "quicksort.ó Fo≥ mos⌠á kind≤á oµ ì
sortinτá thi≤ i≤ thσ bes⌠ choicσ a≤ i⌠ usuall∙ ha≤ thσ bes⌠ ì
averagσ runninτ timσ (ε loτ ε compares)« Therσ arσ certaiε ì
case≤ wherσ thσ quicksor⌠ i≤ a⌠ ß disadvantage« Thi≤ occur≤ ì
wheε thσ datß t∩ bσ sorteΣ i≤ alread∙ partiall∙ sorted« ì
Iµ thσ datß i≤ alread∙ sorted¼ thσ quicksor⌠ wil∞ exhibi⌠ it≤ ì
worst-casσ runninτ timσ (ε squareΣ compares)« Kee≡ thi≤ iε ì
minΣ iµ i⌠ seem≤ t∩ bσ takinτ aε inordinatσ amoun⌠ oµ timσ ì
oε ß nearl∙ sorteΣ file.
Thσ maximuφ amoun⌠ oµ datß tha⌠ caε bσ sorteΣ depend≤ oε thσ ì
capacit∙ oµ you≥ disk«á T∩ determinσ ho≈ biτ ß filσ yo⌡á caε ì
sort¼á dividσá thσá capacit∙ oµ you≥ drivσ b∙á two«á Fo≥ ì
example¼ iµ yo⌡ havσ ß standarΣ CP/═ 2.2 systeφ anΣ ß drivσ ì
witΦ 241δ capacit∙ (remember¼ CP/═ use≤ par⌠ oµ thσ disδ fo≥ ì
itselµ anΣ thσ directory⌐ theε yo⌡ caε sor⌠ ß filσ u≡ t∩ ì
241k/▓ o≥ 120δ bytes« Thi≤ assume≤ tha⌠ yo⌡ havσ tw∩ disδ ì
drives¼ onσ witΦ SORT¼ thσ othe≥ empt∙ bu⌠ fo≥ thσ filσ t∩ bσ ì
sorted« Thσ reasoε fo≥ thi≤ limitatioε i≤ becausσ SOR╘ put≤ ì
it≤ temporar∙ file≤ oε thσ samσ drivσ a≤ thσ filσ beinτ ì
sorted« FO╥ THOS┼ O╞ YO╒ WIT╚ HAR─ DISKS║ Yes¼ therσ isé ß ì
limit«á Yo⌡á canno⌠ sor⌠ ß filσ teε megabyte≤ lonτ jus⌠á be-ì
causσ yo⌡ havσ ß twent∙ megabytσ drive«á Sincσ SOR╘á mus⌠ ì
maintaiεá ß filσ contro∞ blocδ fo≥ eacΦ temporar∙ filσá i⌠ ì
generates¼á thσ large≥ thσ sor⌠ file¼ thσ morσ temporarie≤ ì
yo⌡ neeΣ and¼ consequently¼ thσ morσ memor∙ i≤ used« I⌠ i≤ ì
possiblσá tha⌠ yo⌡ coulΣ takσ u≡ al∞ oµ memor∙ jus⌠ witΦ filσ ì
contro∞ blocks«ì
SOR╘á dynamicall∙ allocate≤ spacσ accordinτ t∩ thσ sizσá oµ ì
you≥á CP/═ system«á Wha⌠ doe≤ thi≤ mean┐á Well¼á fo≥ ì
starters¼ i⌠ mean≤ thσ morσ memor∙ yo⌡ have¼ thσ quicke≥ SOR╘ ì
i≤á goinτá t∩á ge⌠á thσ joΓ done«á SOR╘ use≤á mos⌠á oµá thσ ì
availablσ memor∙ fo≥ thσ tex⌠ buffer¼á anΣ thσ res⌠ fo≥á FCB≤ ì
anΣá recorΣá pointers«á Thσ large≥ thσ sor⌠ buffe≥á is¼á thσ ì
fewe≥ temporar∙ file≤ havσ t∩ bσ made¼ anΣ therefore¼ thσ ì
fewe≥á file≤á tha⌠ havσ t∩ bσ mergeΣ (merginτá take≤á ßá lonτ ì
time).
SOR╘ ha≤ thσ abilit∙ t∩ sor⌠ no⌠ onl∙ lines¼á bu⌠ record≤á a≤ ì
well«á Thi≤á i≤á donσ b∙ specifyinτ aεá end-of-recorΣ ìècharacte≥ (EOR)« Thi≤ i≤ an∙ characte≥ tha⌠ signifie≤ thσ ì
begin/enΣ oµ eacΦ record« Fo≥ instance¼ iε ß norma∞ linσ ì
sort¼ thi≤ characte≥ i≤ ß linσ feeΣ (assuminτ tha⌠ eacΦ linσ ì
i≤ terminateΣ b∙ ß carriagσ returε linσ feed)« Thi≤ featurσ ì
enable≤ yo⌡ t∩ sor⌠ addresses¼ financia∞ records¼ etc« ┴ ì
WOR─ O╞ WARNING« Iµ yo⌡ havσ ß filσ iε thσ followinτ form:
ì
<rec 1> Bugs Bunnyì
4242 Warner Bros. Laneì
Hollyweed, CA. 935146728ì
<EOR><CR><LF>ì
<rec 2> George Burnsì
1 Heavenly Placeì
Eternity, Timbuktu 0001ì
<EOR><CR><LF>ì
<rec 3> Afterthought Engineeringì
7266 Courtney Dr.ì
San Diego, CA. 92111ì
<EOR><CR><LF>ì
ì
SOR╘ wil∞ no⌠ sor⌠ i⌠ a≤ <reπ 3╛ <reπ 1╛ <reπ 2>¼ bu⌠ a≤ ì
<rec3╛ <reπ 2╛ <reπ 1>« Thi≤ i≤ becausσ record≤ ▓ anΣ │ ì
reall∙ begiε witΦ CR/LF¼ whilσ recorΣ ▒ begin≤ witΦ "Bugs"« ì
T∩ avoiΣ this¼á yo⌡ caε d∩ onσ oµ thσ followinτá things║á (1⌐ ì
begiεá thσ filσ witΦ aε enΣ oµ recorΣ characte≥ oε ß linσ al∞ ì
b∙ itself¼ (2⌐ begiε thσ filσ witΦ ß blanδ line¼ o≥ (3⌐ begiε ì
eacΦá recorΣá witΦá aεá enΣá oµá recorΣá characte≥áá followeΣ ì
immediatel∙ b∙ thσ firs⌠ characte≥ oµ thσ nex⌠ record« Usinτ ì
thσ thirΣ methoΣ (probabl∙ thσ wors⌠ oµ thσ bunch)¼ thσ abovσ ì
examplσ woulΣ looδ likσ this:
<reπ 1╛á Bug≤ Bunn∙
424▓ Warne≥ Bros« Lanσ
Hollyweed¼áCA« 93514672╕
<reπ 2╛ <EOR>Georgσ Burn≤
á ▒ Heavenl∙ Placσ
Eternity¼ Timbukt⌡ 00000
<reπ 3╛ <EOR>Afterthough⌠ Engineering
7266 Courtney Dr.
San Diego, CA. 92111
<EOR>
Sor⌠á i≤á no⌠ ß completel∙ barσ bone≤ sor⌠ program«á I⌠á ha≤ ì
five (well¼ four anΣ ß half⌐ options« Thσ firs⌠ option¼ -b¼ ì
allow≤á yo⌡á ski≡á leadinτ blank≤ anΣ tab≤á iεá line≤á beforσ ì
comparinτ them«á ╔ havσ n∩ ideß a⌠ thσ momen⌠ wha⌠ gooΣ thi≤ ì
optioε is¼á b∙ I'φ surσ i⌠ caε bσ useΣ iε somσ way« Thσ nex⌠ ì
optioεá i≤á thσ -dé option«á Thi≤ put≤ thσ prograφá int∩á thσ ì
debuτá modσ anΣ result≤ iε ß buncΦ oµ meaningles≤ informatioε ì
oεá thσ screen«á Thσ thirΣ optioε i≤ thσá -eéá option«á Thi≤ ì
optioεá wil∞á makσ thσ prograφ promp⌠ thσ use≥ fo≥ aε enΣá oµ ì
recorΣ character«á Thσ fourtΦ optioε i≤ thσ -né option« Thi≤ ì
cause≤ thσ prograφ t∩ regarΣ thσ firs⌠ columε oµ eacΦ linσ a≤ ì
ßá number«á Thi≤á caε bσ usefu∞ iµ yo⌡ wan⌠ t∩ sor⌠á ßá filσ ì
wherσá thσá firs⌠á columεá ha≤ number≤á tha⌠á arσá no⌠á righ⌠ ìèjustified«á Iµ tw∩ number≤ arσ equal¼á thσ remainde≥ oµá thσ ì
linσá i≤ sorteΣ oε aε alphabetiπ basis«á Thσ las⌠ optioεá i≤ ì
thσá -réá optioε whicΦ cause≤ thσ prograφ t∩ sor⌠á iεá reversσ ì
(descending⌐ order.
All options default to off.
Restrictions
1«á Neithe≥ linσ feeΣ no≥ carriagσ returε caε bσ specifieΣ a≤ ì
enΣá oµ recorΣ characters«á Thi≤ isn'⌠ reall∙ ßá restrictioε ì
sincσá usinτ carriagσ returε a≤ aε EO╥ make≤ thσ sor⌠ seeφ t∩ ì
comσ ou⌠ wronτ (seσ thσ warninτ above⌐ anΣ sincσ linσ feeΣ i≤ ì
the default EOR.
2«á Thσ maximuφ recorΣ lengtΦ i≤ 51▓ bytes«á Iµ yo⌡ wan⌠á t∩ ì
sort a record longer than 512 bytes, too bad.
3«á Wheε usinτ thσ -né option¼á thσ onl∙ lega∞ number≤ arσ thσ ì
integers between -32768 and 32767.
Using Sort
T∩á sor⌠á ßá tex⌠ filσ iε ascendinτ orde≥ oε ß linσá b∙á linσ ì
basis, type
sort infile outfile
wherσ infilσ i≤ thσ namσ oµ thσ inpu⌠ filσ anΣ outfilσ i≤ thσ ì
namσá oµá thσá outpu⌠ file«á Iµ yo⌡ wan⌠ t∩á ignorσá leadinτ ì
blanks when sorting a file, typeì
sort -b infile outfile
Iµ yo⌡ wan⌠ t∩ sor⌠ ß filσ wherσ yo⌡ wan⌠ thσ firs⌠ columε t∩ ì
bσá regardeΣ a≤ ß columε oµ number≤ anΣ yo⌡ wan⌠ thσá number≤ ì
sorted in descending (reverse) order, type
sort -n -r infile outfile
or
sort -nr infile outfile
Iµ yo⌡ reall∙ wan⌠ t∩ wrinτ sor⌠ ou⌠ anΣ usσ al∞ thσ options¼ ì
type
sort -bdenr infile outfile
and watch the world go by.
Thi≤á sor⌠á prograφ wa≤ translateΣ froφá thσá Ratfo≥á versioε ì
describeΣá iε "Softwarσ Toolsó b∙ Kernighaε anΣ Plauge≥ t∩á ├ ì
and modified to handle dynamic buffer allocation.