Created
May 14, 2013 09:03
-
-
Save geta6/5574653 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
@geta6 | |
Core i7 2.0GHz | |
Memory 8GB 1600 MHz DDR3 | |
OS X 10.8.3 | |
CoffeeScript 1.6.2 | |
JSNative: 342ms | |
BubbleSort Test: true | |
BubbleSort: 5614ms | |
ShakerSort Test: true | |
ShakerSort: 25ms | |
CombSort Test: true | |
CombSort: 4ms | |
GnomeSort Test: true | |
GnomeSort: 11ms | |
QuickSort Test: true | |
QuickSort: 10ms | |
### | |
{argv} = require 'optimist' | |
array = require './_sample.json' | |
count = -> | |
cnt = 0 | |
cnt++ for val, key of arguments[0] | |
return cnt | |
all = argv.all or argv.a | |
test = (sorted) -> | |
return (JSON.stringify NativeSort array) is (JSON.stringify sorted) | |
if argv.h or argv.help or 3 > count argv | |
console.error '-h, --help' | |
console.error '-a, --all' | |
console.error '-s, --show' | |
console.error '--bubble' | |
console.error '--shaker' | |
if argv.s or argv.show | |
console.log JSON.stringify array | |
if all or argv.native | |
console.time 'JSNative' | |
(NativeSort array) for i in [0...10000] | |
console.timeEnd 'JSNative' | |
console.log '' | |
if all or argv.bubble | |
console.log (name = 'BubbleSort'), 'Test:', test BubbleSort array | |
console.time name | |
(BubbleSort array) for i in [0...10000] | |
console.timeEnd name | |
console.log '' | |
if all or argv.shaker | |
console.log (name = 'ShakerSort'), 'Test:', test ShakerSort array | |
console.time name | |
(ShakerSort array) for i in [0...10000] | |
console.timeEnd name | |
console.log '' | |
if all or argv.comb | |
console.log (name = 'CombSort'), 'Test:', test CombSort array | |
console.time name | |
(CombSort array) for i in [0...10000] | |
console.timeEnd name | |
console.log '' | |
if all or argv.gnome | |
console.log (name = 'GnomeSort'), 'Test:', test GnomeSort array | |
console.time name | |
(GnomeSort array) for i in [0...10000] | |
console.timeEnd name | |
console.log '' | |
if all or argv.quick | |
console.log (name = 'QuickSort'), 'Test:', test QuickSort array | |
console.time name | |
(QuickSort array) for i in [0...10000] | |
console.timeEnd name | |
console.log '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ 138, | |
20, | |
199, | |
171, | |
193, | |
77, | |
137, | |
185, | |
109, | |
103, | |
103, | |
171, | |
176, | |
131, | |
119, | |
141, | |
141, | |
170, | |
99, | |
28, | |
107, | |
9, | |
54, | |
47, | |
70, | |
65, | |
35, | |
59, | |
118, | |
55, | |
51, | |
101, | |
28, | |
17, | |
73, | |
163, | |
3, | |
170, | |
30, | |
22, | |
148, | |
68, | |
107, | |
127, | |
145, | |
82, | |
89, | |
67, | |
112, | |
31, | |
182, | |
191, | |
102, | |
197, | |
109, | |
152, | |
141, | |
93, | |
180, | |
134, | |
116, | |
175, | |
37, | |
26, | |
83, | |
101, | |
56, | |
44, | |
48, | |
27, | |
59, | |
196, | |
79, | |
65, | |
68, | |
102, | |
71, | |
146, | |
35, | |
9, | |
37, | |
145, | |
105, | |
59, | |
145, | |
6, | |
125, | |
29, | |
30, | |
93, | |
9, | |
192, | |
39, | |
150, | |
5, | |
174, | |
176, | |
163, | |
53, | |
197, | |
172, | |
104, | |
187, | |
168, | |
143, | |
125, | |
71, | |
15, | |
165, | |
16, | |
90, | |
79, | |
24, | |
167, | |
22, | |
163, | |
162, | |
154, | |
191, | |
36, | |
181, | |
184, | |
2, | |
132, | |
195, | |
97, | |
148, | |
197, | |
168, | |
163, | |
56, | |
103, | |
155, | |
140, | |
38, | |
152, | |
116, | |
88, | |
130, | |
153, | |
6, | |
38, | |
4, | |
133, | |
11, | |
122, | |
60, | |
120, | |
169, | |
78, | |
101, | |
92, | |
62, | |
149, | |
180, | |
126, | |
96, | |
197, | |
75, | |
161, | |
140, | |
189, | |
124, | |
131, | |
140, | |
173, | |
26, | |
20, | |
60, | |
168, | |
102, | |
42, | |
115, | |
189, | |
191, | |
84, | |
7, | |
10, | |
172, | |
78, | |
63, | |
182, | |
190, | |
54, | |
187, | |
132, | |
141, | |
95, | |
115, | |
175, | |
176, | |
61, | |
56, | |
131, | |
107, | |
108, | |
72, | |
126, | |
175, | |
84 ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BubbleSort = (arr) -> | |
for a, i in arr | |
for a, j in ([2..(arr.length - i + 1)]) | |
if array[j] < array[j-1] | |
[array[j], array[j-1]] = [array[j-1], array[j]] | |
return arr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CombSort = (arr) -> | |
arraylen = arr.length | |
interval = parseInt arraylen*10/13, 10 | |
while 1 | |
swaps = 0 | |
i = 0 | |
while i++ when i+interval < arraylen | |
if arr[i] > arr[i+interval] | |
[arr[i], arr[i+interval]] = [arr[i+interval], arr[i]] | |
++swaps | |
if interval is 1 | |
break if swaps is 0 | |
else | |
interval = parseInt interval*10/13, 10 | |
return array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GnomeSort = (arr) -> | |
i = 1 | |
last = 0 | |
while i < arr.length | |
if arr[i] >= arr[i-1] | |
if last isnt 0 | |
[i, last] = [last, 0] | |
++i | |
continue | |
[arr[i], arr[i-1]] = [arr[i-1], arr[i]] | |
if i > 1 | |
last = i if last is 0 | |
--i | |
continue | |
++i | |
return arr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NativeSort = (arr) -> | |
arr.sort (a, b) -> | |
return if (parseInt a, 10) > (parseInt b, 10) then 1 else -1 | |
return arr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
med3 = (x, y, z) -> | |
if x < y | |
if y < z | |
return y | |
else if z < x | |
return x | |
else | |
return z | |
else if z < y | |
return y | |
else if x < z | |
return x | |
else | |
return z | |
QuickSort = (arr, left, right) -> | |
i = (left or= 0) | |
j = (right or= arr.length - 1) | |
pivot = med3 arr[i], arr[i + (j - i) / 2], arr[j] | |
while 1 | |
i++ while arr[i] < pivot | |
j-- while pivot < arr[j] | |
break if i >= j | |
[arr[i], arr[j]] = [arr[j], arr[i]] | |
i++ | |
j-- | |
QuickSort arr, left, i - 1 | |
QuickSort arr, j + 1, right | |
return arr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ShakerSort = (arr) -> | |
index = | |
top: 0 | |
bot: arr.length | |
while 1 | |
swaps = index.top | |
i = index.top | |
while i++ < index.bot when arr[i] > arr[i+1] | |
[arr[i], arr[i+1]] = [arr[i+1], arr[i]] | |
swaps = i | |
index.bot = swaps | |
break if index.top is index.bot | |
swaps = index.bot | |
i = index.bot | |
while i-- > index.top when arr[i] < arr[i-1] | |
[arr[i], arr[i-1]] = [arr[i-1], arr[i]] | |
swaps = i | |
index.top = swaps | |
break if index.top is index.bot | |
return arr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment