Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
82e6a8f8
Commit
82e6a8f8
authored
Apr 28, 1998
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Quicksort retuned by Tim Peters.
parent
bee64533
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
33 deletions
+26
-33
Objects/listobject.c
Objects/listobject.c
+26
-33
No files found.
Objects/listobject.c
View file @
82e6a8f8
...
...
@@ -634,7 +634,7 @@ static int
insertionsort
(
array
,
size
,
compare
)
PyObject
**
array
;
/* Start of array to sort */
int
size
;
/* Number of elements to sort */
PyObject
*
compare
;
/* Comparison function object, or NULL
for
default */
PyObject
*
compare
;
/* Comparison function object, or NULL
=>
default */
{
register
PyObject
**
a
=
array
;
register
PyObject
**
end
=
array
+
size
;
...
...
@@ -645,6 +645,8 @@ insertionsort(array, size, compare)
register
PyObject
**
q
=
p
;
while
(
--
q
>=
a
)
{
register
int
k
=
docompare
(
*
q
,
key
,
compare
);
/* if (p-q >= MINSIZE)
fprintf(stderr, "OUCH! %d\n", p-q); */
if
(
k
==
CMPERROR
)
return
-
1
;
if
(
k
<=
0
)
...
...
@@ -733,11 +735,14 @@ quicksort(array, size, compare)
{
tmp
=
*
l
;
*
l
=
*
lo
;
*
lo
=
tmp
;
}
pivot
=
*
l
;
/* Move pivot off to the side (swap with lo+1) */
*
l
=
*
(
lo
+
1
);
*
(
lo
+
1
)
=
pivot
;
/* Partition the array */
l
=
lo
+
1
;
l
=
lo
+
2
;
r
=
hi
-
2
;
for
(;;)
{
/* Move left index to element > pivot */
do
{
/* Move left index to element >
=
pivot */
while
(
l
<
hi
)
{
k
=
docompare
(
*
l
,
pivot
,
compare
);
if
(
k
==
CMPERROR
)
...
...
@@ -746,8 +751,8 @@ quicksort(array, size, compare)
break
;
l
++
;
}
/* Move right index to element < pivot */
while
(
r
>
=
lo
)
{
/* Move right index to element <
=
pivot */
while
(
r
>
lo
)
{
k
=
docompare
(
pivot
,
*
r
,
compare
);
if
(
k
==
CMPERROR
)
return
-
1
;
...
...
@@ -756,21 +761,17 @@ quicksort(array, size, compare)
r
--
;
}
/* If they
met
, we're through */
if
(
l
<
r
)
{
/* If they
crossed
, we're through */
if
(
l
<
=
r
)
{
/* Swap elements and continue */
tmp
=
*
l
;
*
l
=
*
r
;
*
r
=
tmp
;
l
++
;
r
--
;
}
else
if
(
l
==
r
)
{
l
++
;
r
--
;
break
;
}
if
(
l
>
r
)
break
;
}
}
while
(
l
<=
r
);
/* Swap pivot back into place; *r <= pivot */
*
(
lo
+
1
)
=
*
r
;
*
r
=
pivot
;
/* We have now reached the following conditions:
lo <= r < l <= hi
...
...
@@ -785,27 +786,19 @@ quicksort(array, size, compare)
n2
=
hi
-
l
;
if
(
n
>
n2
)
{
/* First one is bigger */
if
(
n
>
MINSIZE
)
{
lostack
[
top
]
=
lo
;
histack
[
top
++
]
=
r
;
if
(
n2
>
MINSIZE
)
{
lostack
[
top
]
=
l
;
histack
[
top
++
]
=
hi
;
}
}
}
else
{
/* Second one is bigger */
if
(
n2
>
MINSIZE
)
{
lostack
[
top
]
=
l
;
histack
[
top
++
]
=
hi
;
if
(
n
>
MINSIZE
)
{
lostack
[
top
]
=
lo
;
histack
[
top
++
]
=
r
;
}
}
}
/* Should assert top <
STACKSIZE-1
*/
/* Should assert top <
= STACKSIZE
*/
}
/*
...
...
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