Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
e89676a6
Unverified
Commit
e89676a6
authored
Oct 18, 2018
by
Yukihiro "Matz" Matsumoto
Committed by
GitHub
Oct 18, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4142 from iij/mergesort
replace quicksort with mergesort.
parents
fdd5ce8f
91444c47
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
36 deletions
+58
-36
mrbgems/mruby-enum-ext/mrblib/enum.rb
mrbgems/mruby-enum-ext/mrblib/enum.rb
+1
-3
mrblib/array.rb
mrblib/array.rb
+51
-33
test/t/array.rb
test/t/array.rb
+6
-0
No files found.
mrbgems/mruby-enum-ext/mrblib/enum.rb
View file @
e89676a6
...
...
@@ -201,9 +201,7 @@ module Enumerable
ary
.
push
([
block
.
call
(
e
),
i
])
}
if
ary
.
size
>
1
ary
.
__sort_sub__
(
0
,
ary
.
size
-
1
)
do
|
a
,
b
|
a
<=>
b
end
ary
.
sort!
end
ary
.
collect
{
|
e
,
i
|
orig
[
i
]}
end
...
...
mrblib/array.rb
View file @
e89676a6
...
...
@@ -193,43 +193,61 @@ class Array
include
Enumerable
##
# Quick sort
# left : the beginning of sort region
# right : the end of sort region
def
__sort_sub__
(
left
,
right
,
&
block
)
stack
=
[
[
left
,
right
]
]
# Sort all elements and replace +self+ with these
# elements.
def
sort!
(
&
block
)
stack
=
[
[
0
,
self
.
size
-
1
]
]
until
stack
.
empty?
left
,
right
=
stack
.
pop
if
left
<
right
i
=
left
j
=
right
pivot
=
self
[
i
+
(
j
-
i
)
/
2
]
while
true
while
((
block
)?
block
.
call
(
self
[
i
],
pivot
):
(
self
[
i
]
<=>
pivot
))
<
0
i
+=
1
end
while
((
block
)?
block
.
call
(
pivot
,
self
[
j
]):
(
pivot
<=>
self
[
j
]))
<
0
j
-=
1
left
,
mid
,
right
=
stack
.
pop
if
right
==
nil
right
=
mid
# sort self[left..right]
if
left
<
right
if
left
+
1
==
right
lval
=
self
[
left
]
rval
=
self
[
right
]
if
(
block
&
.
call
(
lval
,
rval
)
||
(
lval
<=>
rval
))
>
0
self
[
left
]
=
rval
self
[
right
]
=
lval
end
else
mid
=
((
left
+
right
+
1
)
/
2
).
floor
stack
.
push
[
left
,
mid
,
right
]
stack
.
push
[
mid
,
right
]
stack
.
push
[
left
,
(
mid
-
1
)
]
if
left
<
mid
-
1
end
break
if
(
i
>=
j
)
tmp
=
self
[
i
];
self
[
i
]
=
self
[
j
];
self
[
j
]
=
tmp
;
i
+=
1
j
-=
1
end
stack
.
push
[
left
,
i
-
1
]
stack
.
push
[
j
+
1
,
right
]
end
end
end
# private :__sort_sub__
else
lary
=
self
[
left
,
mid
-
left
]
lsize
=
lary
.
size
##
# Sort all elements and replace +self+ with these
# elements.
def
sort!
(
&
block
)
size
=
self
.
size
if
size
>
1
__sort_sub__
(
0
,
size
-
1
,
&
block
)
# The entity sharing between lary and self may cause a large memory
# copy operation in the merge loop below. This harmless operation
# cancels the sharing and provides a huge performance gain.
lary
[
0
]
=
lary
[
0
]
# merge
lidx
=
0
ridx
=
mid
(
left
..
right
).
each
{
|
i
|
if
lidx
>=
lsize
break
elsif
ridx
>
right
self
[
i
,
lsize
-
lidx
]
=
lary
[
lidx
,
lsize
-
lidx
]
break
else
lval
=
lary
[
lidx
]
rval
=
self
[
ridx
]
if
(
block
&
.
call
(
lval
,
rval
)
||
(
lval
<=>
rval
))
<=
0
self
[
i
]
=
lval
lidx
+=
1
else
self
[
i
]
=
rval
ridx
+=
1
end
end
}
end
end
self
end
...
...
test/t/array.rb
View file @
e89676a6
...
...
@@ -386,6 +386,12 @@ assert("Array#rindex") do
assert_equal
0
,
$a
.
rindex
(
1
)
end
assert
(
'Array#sort!'
)
do
a
=
[
3
,
2
,
1
]
assert_equal
a
,
a
.
sort!
# sort! returns self.
assert_equal
[
1
,
2
,
3
],
a
# it is sorted.
end
assert
(
'Array#freeze'
)
do
a
=
[].
freeze
assert_raise
(
RuntimeError
)
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment