First NIT release and new clean mercurial repository
[nit.git] / tests / bench_random_n_sort.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2005-2008 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import example_sorts
18
19 redef class Int
20 meth random: Int
21 # Return a random generated number between 0 and self
22 do
23 var val = (last_number.value * 3877 + 29573) % 139968
24 last_number.value = val
25 return val % self
26 end
27
28 meth last_number: Integer
29 do
30 return once new Integer
31 end
32 end
33
34 class Integer
35 readable writable attr _value: Int
36
37 init
38 do
39 _value = 42
40 end
41 end
42
43 var n = 100
44 if not args.is_empty then
45 n = args.first.to_i
46 n = n * n
47 end
48
49 # Build an array
50 var array = new Array[Int].with_capacity(n)
51 for i in [0..n[ do
52 array.add(n.random)
53 end
54
55 # Clone the array
56 var array2 = new Array[Int].with_capacity(n)
57 for i in [0..n[ do
58 array2.add(array[i])
59 end
60
61 # Sort now
62
63 for i in [0..10[ do
64 printn(array[n*i/10], " ")
65 end
66 print(array.last)
67
68 quicksort(array)
69
70 for i in [0..10[ do
71 printn(array[n*i/10], " ")
72 end
73 print(array.last)
74
75
76 for i in [0..10[ do
77 printn(array2[n*i/10], " ")
78 end
79 print(array2.last)
80
81 heapsort(array2)
82
83 for i in [0..10[ do
84 printn(array2[n*i/10], " ")
85 end
86 print(array2.last)