Merge: doc: fixed some typos and other misc. corrections
[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 fun 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 fun last_number: Integer
29 do
30 return once new Integer
31 end
32 end
33
34 class Integer
35 var value: Int = 42
36 end
37
38 var n = 100
39 if not args.is_empty then
40 n = args.first.to_i
41 n = n * n
42 end
43
44 # Build an array
45 var array = new Array[Int].with_capacity(n)
46 for i in [0..n[ do
47 array.add(n.random)
48 end
49
50 # Clone the array
51 var array2 = new Array[Int].with_capacity(n)
52 for i in [0..n[ do
53 array2.add(array[i])
54 end
55
56 # Sort now
57
58 for i in [0..10[ do
59 printn(array[n*i/10], " ")
60 end
61 print(array.last)
62
63 quicksort(array)
64
65 for i in [0..10[ do
66 printn(array[n*i/10], " ")
67 end
68 print(array.last)
69
70
71 for i in [0..10[ do
72 printn(array2[n*i/10], " ")
73 end
74 print(array2.last)
75
76 heapsort(array2)
77
78 for i in [0..10[ do
79 printn(array2[n*i/10], " ")
80 end
81 print(array2.last)