Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Blatt 02
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Algorithmen Datenstrukturen und Datenabstraktion WS22
Blatt 02
Commits
1caa1a69
Commit
1caa1a69
authored
2 years ago
by
nilsl99
Browse files
Options
Downloads
Patches
Plain Diff
Implemented HeapSort
parent
fa27a910
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Aufgabe02.py
+41
-6
41 additions, 6 deletions
Aufgabe02.py
with
41 additions
and
6 deletions
Aufgabe02.py
+
41
−
6
View file @
1caa1a69
def
HeapSort
(
array
):
'''
Update the board according to the move and the player. If player 0 does
the move, set the board cell to -1. If payer 1 makes the move, set the board cell
the move, set the board cell to -1. If payer 1 makes the move, set the board cell
to 1 (empty board cells have value zero).
Parameters
----------
array: list(*int)
...
...
@@ -13,11 +13,46 @@ def HeapSort(array):
sorted_array: a sorted copy of the array
'''
return
p
=
lambda
i
:
(
i
-
1
)
//
2
l
=
lambda
i
:
2
*
i
+
1
r
=
lambda
i
:
2
*
i
+
2
sorted_array
=
list
(
array
)
# Erzeuge einen max-Heap
for
i
in
range
(
1
,
len
(
sorted_array
)):
j
=
i
while
sorted_array
[
j
]
>
sorted_array
[
p
(
j
)]
and
j
!=
0
:
sorted_array
[
j
],
sorted_array
[
p
(
j
)]
=
sorted_array
[
p
(
j
)],
sorted_array
[
j
]
j
=
p
(
j
)
# Entferne max aus dem max-Heap
for
i
in
range
(
len
(
sorted_array
)
-
1
,
1
,
-
1
):
sorted_array
[
0
],
sorted_array
[
i
]
=
sorted_array
[
i
],
sorted_array
[
0
]
j
=
0
while
True
:
if
l
(
j
)
>=
i
or
r
(
j
)
>=
i
:
break
if
r
(
j
)
>=
i
and
sorted_array
[
l
(
j
)]
>
sorted_array
[
j
]:
sorted_array
[
j
],
sorted_array
[
l
(
j
)]
=
sorted_array
[
l
(
j
)],
sorted_array
[
j
]
j
=
l
(
j
)
if
sorted_array
[
j
]
>=
sorted_array
[
l
(
j
)]
and
sorted_array
[
j
]
>=
sorted_array
[
r
(
j
)]:
break
if
sorted_array
[
l
(
j
)]
>
sorted_array
[
r
(
j
)]:
sorted_array
[
j
],
sorted_array
[
l
(
j
)]
=
sorted_array
[
l
(
j
)],
sorted_array
[
j
]
j
=
l
(
j
)
else
:
sorted_array
[
j
],
sorted_array
[
r
(
j
)]
=
sorted_array
[
r
(
j
)],
sorted_array
[
j
]
j
=
r
(
j
)
return
sorted_array
def
InsertionSort
(
array
):
'''
Sort the array by inserting each element into an already sorted part of the array.
Parameters
----------
array: list(*int)
...
...
@@ -40,8 +75,8 @@ def InsertionSort(array):
return
sorted_array
def
CountingSort
(
array
):
'''
Sort the array by counting the oc
curr
ence of each element in the array.
'''
Sort the array by counting the oc
j
ence of each element in the array.
Parameters
----------
array: list(*int)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment