Download code

Jump to: navigation, search

Back to Quicksort_(Oz)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

quicksort-tests.oz

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/Quicksort_(Oz)?oldid=15589
14 
15 functor
16 import
17    Application
18    System
19    Q at 'QuicksortList.ozf' 
20 define
21    proc {TestSort F Testname S R}
22       Result
23    in
24       if
25          R == {F S Value.'=<'}
26       then
27          Result = "passed"
28       else 
29          Result =  "failed"
30       end
31       {System.showInfo
32        {System.printName F} # " - " # Result # " - " # Testname # "."}
33    end
34    proc {TestNumericSort F}
35       Stimulus = [3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3]
36       Response = [1 1 2 3 3 3 4 5 5 5 6 7 8 9 9 9]
37    in
38       {TestSort F "numeric sort" Stimulus Response}
39    end
40    proc {TestStringSort F}
41       Stimulus = ['bob' 'alice' 'barry' 'zoe' 'charlotte' 'fred' 'marvin']
42       Response = ['alice' 'barry' 'bob' 'charlotte' 'fred' 'marvin' 'zoe']
43    in
44       {TestSort F "string sort" Stimulus Response}
45    end
46 in     
47    {System.showInfo "Testing QuicksortList...\n"}
48    
49    {TestNumericSort Q.qsort1}
50    {TestStringSort Q.qsort1}
51    {TestNumericSort Q.qsort2}
52    {TestStringSort Q.qsort2}
53    {TestNumericSort Q.qsort3}
54    {TestStringSort Q.qsort3}
55    
56    {Application.exit 0}
57 end


hijacker
hijacker
hijacker
hijacker

QuicksortList.oz

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/Quicksort_(Oz)?oldid=15589
14 
15 functor
16 export
17    Qsort1
18    Qsort2
19    Qsort3
20 define
21    fun {Qsort1 Xs P}
22       case Xs
23       of nil then nil
24       [] [X] then [X]
25       [] X|Xr then
26          L G
27       in
28          {List.partition Xr fun {$ Y} {P Y X} end L G}
29 	 {List.append {Qsort1 L P} X|{Qsort1 G P}}
30       end
31    end
32 
33    fun {Qsort2 Xs P}
34       proc {Qsort X ?R}
35          case X
36          of nil then Y in R = Y#Y
37          [] [X] then Y in R = (X|Y)#Y
38          [] X|Xr then
39             L G L1 L2 G1 G2
40          in
41             {List.partition Xr fun {$ Y} {P Y X} end L G}
42             {Qsort L L1#L2}
43             {Qsort G G1#G2}
44             L2 = (X|G1)
45             R = L1#G2
46          end
47       end
48       Ys
49    in
50       {Qsort Xs Ys#nil}
51       Ys
52    end
53    fun {Qsort3 Xs P}
54       fun {Qsort X A}
55          case X
56          of nil then A
57          [] [X] then X|A
58          [] X|Xr then
59             L G
60          in
61             {List.partition Xr fun {$ Y} {P Y X} end L G}
62             {Qsort L X|{Qsort G A}}
63          end
64       end
65    in
66       {Qsort Xs nil}
67    end           
68 end


hijacker
hijacker
hijacker
hijacker