<#33096#><#33096#>
<#66172#>Figure: A tabular illustration of <#33097#>quick-sort<#33097#><#66172#>
<#33103#>(list<#33103#> <#33104#>11<#33104#> <#33105#>8<#33105#> <#33106#>14<#33106#> <#33107#>7)<#33107#>The pivot item is <#66173#><#33111#>11<#33111#><#66173#>. Partioning the list into items larger and smaller than <#66174#><#33112#>11<#33112#><#66174#> produces two lists:
<#33117#>(list<#33117#> <#33118#>8<#33118#> <#33119#>7)<#33119#>and
<#33127#>(list<#33127#> <#33128#>14)<#33128#>The second one is already sorted in ascending order; sorting the first one produces <#66175#><#33132#>(list<#33132#>\ <#33133#>7<#33133#>\ <#33134#>8)<#33134#><#66175#>. This leaves us with three pieces from the original list:
<#71458#>;; <#66192#><#33189#>quick-sort<#33189#> <#33190#>:<#33190#> <#33191#>(listof<#33191#> <#33192#>number)<#33192#> <#33193#><#33193#><#33194#>-;SPMgt;<#33194#><#33195#><#33195#> <#33196#>(listof<#33196#> <#33197#>number)<#33197#><#66192#><#71458#>
<#33198#>;; to create a list of numbers with the same numbers as<#33198#>
<#71459#>;; <#66193#><#33199#>alon<#33199#><#66193#> sorted in ascending order<#71459#>
<#33200#>(d<#33200#><#33201#>efine<#33201#> <#33202#>(quick-sort<#33202#> <#33203#>alon)<#33203#>
<#33204#>(c<#33204#><#33205#>ond<#33205#>
<#33206#>[<#33206#><#33207#>(empty?<#33207#> <#33208#>alon)<#33208#> <#33209#>empty]<#33209#>
<#33210#>[<#33210#><#33211#>else<#33211#> <#33212#>...]<#33212#><#33213#>))<#33213#>
The answer for the first case is given. For the second case, when
<#66194#><#33217#>quicksort<#33217#><#66194#>'s input is non-<#66195#><#33218#>empty<#33218#><#66195#>, the algorithm uses the
first item to partition the rest of the list into two sublists: a list with
all items smaller than the pivot item and another one with those larger
than the pivot item.
Since the rest of the list is of unknown size, we leave the task of
partitioning the list to two auxiliary functions: <#66196#><#33219#>smaller-items<#33219#><#66196#>
and <#66197#><#33220#>larger-items<#33220#><#66197#>. They process the list and filter out those items
that are smaller and larger, respectively, than the first one. Hence, each
auxiliary function accepts two arguments, namely, a list of numbers and a
number. Developing these functions is, of course, an exercise in structural
recursion; their definitions are shown in figure~#figqsort#33221><#71460#>;; <#66198#><#33226#>quick-sort<#33226#> <#33227#>:<#33227#> <#33228#>(listof<#33228#> <#33229#>number)<#33229#> <#33230#><#33230#><#33231#>-;SPMgt;<#33231#><#33232#><#33232#> <#33233#>(listof<#33233#> <#33234#>number)<#33234#><#66198#><#71460#>
<#33235#>;; to create a list of numbers with the same numbers as<#33235#>
<#71461#>;; <#66199#><#33236#>alon<#33236#><#66199#> sorted in ascending order<#71461#>
<#33237#>(d<#33237#><#33238#>efine<#33238#> <#33239#>(quick-sort<#33239#> <#33240#>alon)<#33240#>
<#33241#>(c<#33241#><#33242#>ond<#33242#>
<#33243#>[<#33243#><#33244#>(empty?<#33244#> <#33245#>alon)<#33245#> <#33246#>empty]<#33246#>
<#33247#>[<#33247#><#33248#>else<#33248#> <#33249#>(a<#33249#><#33250#>ppend<#33250#>
<#33251#>(quick-sort<#33251#> <#33252#>(smaller-items<#33252#> <#33253#>alon<#33253#> <#33254#>(first<#33254#> <#33255#>alon)))<#33255#>
<#33256#>(list<#33256#> <#33257#>(first<#33257#> <#33258#>alon))<#33258#>
<#33259#>(quick-sort<#33259#> <#33260#>(larger-items<#33260#> <#33261#>alon<#33261#> <#33262#>(first<#33262#> <#33263#>alon))))]<#33263#><#33264#>))<#33264#>
<#71462#>;; <#66200#><#33265#>larger-items<#33265#> <#33266#>:<#33266#> <#33267#>(listof<#33267#> <#33268#>number)<#33268#> <#33269#>number<#33269#> <#33270#><#33270#><#33271#>-;SPMgt;<#33271#><#33272#><#33272#> <#33273#>(listof<#33273#> <#33274#>number)<#33274#><#66200#><#71462#>
<#71463#>;; to create a list with all those numbers on <#66201#><#33275#>alon<#33275#><#66201#> <#71463#>
<#71464#>;; that are larger than <#66202#><#33276#>threshold<#33276#><#66202#><#71464#>
<#33277#>(d<#33277#><#33278#>efine<#33278#> <#33279#>(larger-items<#33279#> <#33280#>alon<#33280#> <#33281#>threshold)<#33281#>
<#33282#>(c<#33282#><#33283#>ond<#33283#>
<#33284#>[<#33284#><#33285#>(empty?<#33285#> <#33286#>alon)<#33286#> <#33287#>empty]<#33287#>
<#33288#>[<#33288#><#33289#>else<#33289#> <#33290#>(if<#33290#> <#33291#>(;SPMgt;<#33291#> <#33292#>(first<#33292#> <#33293#>alon)<#33293#> <#33294#>threshold)<#33294#>
<#33295#>(cons<#33295#> <#33296#>(first<#33296#> <#33297#>alon)<#33297#> <#33298#>(larger-items<#33298#> <#33299#>(rest<#33299#> <#33300#>alon)<#33300#> <#33301#>threshold))<#33301#>
<#33302#>(larger-items<#33302#> <#33303#>(rest<#33303#> <#33304#>alon)<#33304#> <#33305#>threshold))]<#33305#><#33306#>))<#33306#>
<#71465#>;; smaller-items : <#66203#><#33307#>(listof<#33307#> <#33308#>number)<#33308#> <#33309#>number<#33309#> <#33310#><#33310#><#33311#>-;SPMgt;<#33311#><#33312#><#33312#> <#33313#>(listof<#33313#> <#33314#>number)<#33314#><#66203#><#71465#>
<#71466#>;; to create a list with all those numbers on <#66204#><#33315#>alon<#33315#><#66204#> <#71466#>
<#71467#>;; that are smaller than <#66205#><#33316#>threshold<#33316#><#66205#><#71467#>
<#33317#>(d<#33317#><#33318#>efine<#33318#> <#33319#>(smaller-items<#33319#> <#33320#>alon<#33320#> <#33321#>threshold)<#33321#>
<#33322#>(c<#33322#><#33323#>ond<#33323#>
<#33324#>[<#33324#><#33325#>(empty?<#33325#> <#33326#>alon)<#33326#> <#33327#>empty]<#33327#>
<#33328#>[<#33328#><#33329#>else<#33329#> <#33330#>(if<#33330#> <#33331#>(;SPMlt;<#33331#> <#33332#>(first<#33332#> <#33333#>alon)<#33333#> <#33334#>threshold)<#33334#>
<#33335#>(cons<#33335#> <#33336#>(first<#33336#> <#33337#>alon)<#33337#> <#33338#>(smaller-items<#33338#> <#33339#>(rest<#33339#> <#33340#>alon)<#33340#> <#33341#>threshold))<#33341#>
<#33342#>(smaller-items<#33342#> <#33343#>(rest<#33343#> <#33344#>alon)<#33344#> <#33345#>threshold))]<#33345#><#33346#>))<#33346#>
<#33350#>Figure: The quick-sort algorithm<#33350#><#33370#>(append<#33370#> <#33371#>(quick-sort<#33371#> <#33372#>(smaller-items<#33372#> <#33373#>alon<#33373#> <#33374#>(first<#33374#> <#33375#>alon)))<#33375#>
<#33376#>(list<#33376#> <#33377#>(first<#33377#> <#33378#>alon))<#33378#>
<#33379#>(quick-sort<#33379#> <#33380#>(larger-items<#33380#> <#33381#>alon<#33381#> <#33382#>(first<#33382#> <#33383#>alon))))<#33383#>
Clearly, all items in list~1 are smaller than the pivot and the pivot is
smaller than all items in list~2, so the result is a sorted list.
Figure~#figqsort#33387> <#33395#>(quick-sort<#33395#> <#33396#>(list<#33396#> <#33397#>11<#33397#> <#33398#>8<#33398#> <#33399#>14<#33399#> <#33400#>7))<#33400#>
<#33401#>=<#33401#> <#33402#>(append<#33402#> <#33403#>(quick-sort<#33403#> <#33404#>(list<#33404#> <#33405#>8<#33405#> <#33406#>7))<#33406#>
<#33407#>(list<#33407#> <#33408#>11)<#33408#>
<#33409#>(quick-sort<#33409#> <#33410#>(list<#33410#> <#33411#>14)))<#33411#>
<#33419#>=<#33419#> <#33420#>(append<#33420#> <#33421#>(append<#33421#> <#33422#>(quick-sort<#33422#> <#33423#>(list<#33423#> <#33424#>7))<#33424#>
<#33425#>(list<#33425#> <#33426#>8)<#33426#>
<#33427#>(quick-sort<#33427#> <#33428#>empty))<#33428#>
<#33429#>(list<#33429#> <#33430#>11)<#33430#>
<#33431#>(quick-sort<#33431#> <#33432#>(list<#33432#> <#33433#>14)))<#33433#>
<#33441#>=<#33441#> <#33442#>(append<#33442#> <#33443#>(append<#33443#> <#33444#>(append<#33444#> <#33445#>(quick-sort<#33445#> <#33446#>empty)<#33446#>
<#33447#>(list<#33447#> <#33448#>7)<#33448#>
<#33449#>(quick-sort<#33449#> <#33450#>empty))<#33450#>
<#33451#>(list<#33451#> <#33452#>8)<#33452#>
<#33453#>(quick-sort<#33453#> <#33454#>empty))<#33454#>
<#33455#>(list<#33455#> <#33456#>11)<#33456#>
<#33457#>(quick-sort<#33457#> <#33458#>(list<#33458#> <#33459#>14)))<#33459#>
<#33467#>=<#33467#> <#33468#>(append<#33468#> <#33469#>(append<#33469#> <#33470#>(append<#33470#> <#33471#>empty<#33471#>
<#33472#>(list<#33472#> <#33473#>7)<#33473#>
<#33474#>empty)<#33474#>
<#33475#>(list<#33475#> <#33476#>8)<#33476#>
<#33477#>empty)<#33477#>
<#33478#>(list<#33478#> <#33479#>11)<#33479#>
<#33480#>(quick-sort<#33480#> <#33481#>(list<#33481#> <#33482#>14)))<#33482#>
<#33490#>=<#33490#> <#33491#>(append<#33491#> <#33492#>(append<#33492#> <#33493#>(list<#33493#> <#33494#>7)<#33494#>
<#33495#>(list<#33495#> <#33496#>8)<#33496#>
<#33497#>empty)<#33497#>
<#33498#>(list<#33498#> <#33499#>11)<#33499#>
<#33500#>(quick-sort<#33500#> <#33501#>(list<#33501#> <#33502#>14)))<#33502#>
<#33503#>=<#33503#> <#33504#>...<#33504#>
The calculation shows the essential steps of the sorting process, that is,
the partitioning steps, the recursive sorting steps, and the concatenation
of the three parts. From this calculation, we can see that
<#66213#><#33508#>quick-sort<#33508#><#66213#> implements the process illustrated in
figure~#figsortill#33509><#71468#>;; <#66236#><#33571#>general-quick-sort<#33571#> <#33572#>:<#33572#> <#33573#>(X<#33573#> <#33574#>X<#33574#> <#33575#><#33575#><#33576#>-;SPMgt;<#33576#><#33577#><#33577#> <#33578#>bool)<#33578#> <#33579#>(list<#33579#> <#33580#>X)<#33580#> <#33581#><#33581#><#33582#>-;SPMgt;<#33582#><#33583#><#33583#> <#33584#>(list<#33584#> <#33585#>X)<#33585#><#66236#><#71468#> <#33586#>(define<#33586#> <#33587#>(general-quick-sort<#33587#> <#33588#>a-predicate<#33588#> <#33589#>a-list)<#33589#> <#33590#>...)<#33590#>