Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dijkstra
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Julien David
dijkstra
Commits
c6918dc6
Commit
c6918dc6
authored
3 years ago
by
Julien David
Browse files
Options
Downloads
Patches
Plain Diff
benchmark swap
parent
f12e4545
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
results/heap_sort_swap.plot
+10
-0
10 additions, 0 deletions
results/heap_sort_swap.plot
src/binary_heap.hpp
+53
-7
53 additions, 7 deletions
src/binary_heap.hpp
with
63 additions
and
7 deletions
results/heap_sort_swap.plot
0 → 100644
+
10
−
0
View file @
c6918dc6
4 2.691000
8 11.625000
16 38.618000
32 110.312000
64 287.477000
128 709.014000
256 1682.503000
512 3888.728000
1024 8823.067000
This diff is collapsed.
Click to expand it.
src/binary_heap.hpp
+
53
−
7
View file @
c6918dc6
...
...
@@ -25,6 +25,11 @@ protected:
virtual
bool
heap_cmp
(
T
&
value1
,
T
&
value2
)
=
0
;
public:
/**
Class constructor.
Creates a heap of at most 'max' elements and initializes swap counters to zero.
@input max is the maximal number of elements the heap can contain
*/
BinaryHeap
(
size_t
max
){
this
->
max
=
max
;
this
->
size
=
0
;
...
...
@@ -45,6 +50,11 @@ public:
return
this
->
size
;
}
/**
This method is used during benchmarks, when one wants to know
how many time the swap function has been called.
It is to be called at the beginning of each benchmark.
*/
void
resetCounters
(){
counter_swap_up
=
0
;
counter_swap_down
=
0
;
...
...
@@ -53,20 +63,36 @@ public:
size_t
getSwapsCounters
(){
return
counter_swap_down
+
counter_swap_up
;
}
inline
int
parent
(
int
position
){
/**
Returns the position of a node's parent
@requires 0 < position < this->size
@param position is the position of a node of the heap, except the root
@return the position of a node's parent
*/
inline
int
parent
(
size_t
position
){
return
(
position
-
1
)
/
2
;
}
inline
int
left_child
(
int
position
){
/**
Returns the position of a node's left child
@param position is the position of a node of the heap, except the root
@return the position of a node's parent
*/
inline
int
left_child
(
size_t
position
){
return
position
*
2
+
1
;
}
inline
int
right_child
(
in
t
position
){
inline
int
right_child
(
size_
t
position
){
return
position
*
2
+
2
;
}
void
heapify_up
(
int
position
){
/**
This function takes the position of a value as an input
and makes it go UP in the heap until the comparison constraint is satisfied.
@param position is the position of the value we want to check
*/
void
heapify_up
(
size_t
position
){
while
(
position
>
0
&&
heap_cmp
(
data
[
position
],
data
[
parent
(
position
)])){
std
::
swap
(
data
[
parent
(
position
)],
data
[
position
]);
counter_swap_up
++
;
...
...
@@ -74,7 +100,12 @@ public:
}
}
void
heapify_down
(
int
position
){
/**
This function takes the position of a value as an input
and makes it go DOWN in the heap until the comparison constraint is satisfied.
@param position is the position of the value we want to check
*/
void
heapify_down
(
size_t
position
){
int
min_child
;
min_child
=
heap_cmp
(
data
[
left_child
(
position
)],
data
[
right_child
(
position
)])
?
left_child
(
position
)
:
right_child
(
position
);
while
(
min_child
<
(
size
-
1
)
&&
heap_cmp
(
data
[
min_child
],
data
[
position
])){
...
...
@@ -84,7 +115,13 @@ public:
counter_swap_down
++
;
}
}
/* This function insert a value in the heap
@param value is the value we want to add.
@requires this->size != this-> max (the heap is not full)
@ensures the size is incremented
@exception throws a FullBinaryHeapException if the heap is full.
*/
void
insert
(
T
value
)
{
if
(
size
==
max
)
throw
new
FullBinaryHeapException
();
...
...
@@ -92,16 +129,25 @@ public:
heapify_up
(
size
-
1
);
}
/*
Extract the value at the top of the heap
@ensures the size is decremented
@returns the value at the top of the heap
*/
T
extract
(){
std
::
swap
(
data
[
0
],
data
[
size
-
1
]);
heapify_down
(
0
);
return
data
[
--
size
];
}
/**
Outputs the content of the heap a a regular tabular
*/
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
BinaryHeap
<
int
>&
heap
);
};
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
BinaryHeap
<
int
>&
heap
)
{
for
(
int
i
=
0
;
i
<
heap
.
size
;
i
++
)
...
...
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