Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Andrew Childs
reversal-sort
Commits
0e835b88
Commit
0e835b88
authored
Feb 24, 2021
by
Eddie Schoute
Browse files
Add main function for script
parent
9ec0046c
Changes
2
Hide whitespace changes
Inline
Side-by-side
TripartiteBinarySort.py
View file @
0e835b88
...
...
@@ -73,11 +73,22 @@ def perm_to_01(L, i, j):
median
=
(
j
-
i
)
//
2
return
L
[:
i
]
+
[
int
(
sorted_subseq
.
index
(
k
)
>
median
)
for
k
in
subseq
]
+
L
[
j
+
1
:]
def
permutationsort_divideconquer_tbs
(
L
,
i
,
j
):
def
permutationsort_divideconquer_tbs
(
L
):
"""Routes the given permutation using reversals. Returns the reversals"""
j
=
max
(
L
)
return
permutationsort_divideconquer_tbs
(
L
,
0
,
j
)
def
permutationsort_divideconquer_tbs
(
L
,
i
=
None
,
j
=
None
):
"""
Sorts the given permutations L from index i to j (inclusive) using a divide and conquer approach. Returns a list of
reversals used to perform the sort.
"""
# Set default params
if
i
==
None
:
i
=
min
(
L
)
if
j
==
None
:
j
=
max
(
L
)
if
i
==
j
:
return
[]
T
=
perm_to_01
(
L
,
i
,
j
)
...
...
main.py
View file @
0e835b88
...
...
@@ -120,3 +120,12 @@ def main(NUM_PERMS_PER_LENGTH, LENGTH_FROM, LENGTH_TO):
algnames
=
[
'OES'
,
'GDC(TBS)'
,
'GDC(ATBS)'
]
algs
=
[
SBR
.
odd_even_sort
,
TripartiteBinarySort
.
GDC_TBS
,
AdaptiveTBS
.
GDC_ATBS
]
new_rand_perms_file
(
algs
,
algnames
,
list
(
range
(
LENGTH_FROM
,
LENGTH_TO
+
1
)),
NUM_PERMS_PER_LENGTH
)
# Now you can run `echo "0 1 3 2" | python main.py`
if
__name__
==
"__main__"
:
import
sys
for
line
in
sys
.
stdin
:
perm
=
[
int
(
el
)
for
el
in
line
.
split
()]
reversals
=
TripartiteBinarySort
.
permutationsort_divideconquer_tbs
(
perm
)
print
(
reversals
)
# TODO: Implement pretty print
\ No newline at end of file
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