Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
lecture materials
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
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
generic software skills
lecture materials
Commits
796c88f5
Commit
796c88f5
authored
1 year ago
by
Tobias Koelling
Browse files
Options
Downloads
Patches
Plain Diff
refactor bubble sort
parent
04bdb09e
No related branches found
No related tags found
1 merge request
!24
complexity lecture
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lectures/complexity/slides.qmd
+30
-12
30 additions, 12 deletions
lectures/complexity/slides.qmd
with
30 additions
and
12 deletions
lectures/complexity/slides.qmd
+
30
−
12
View file @
796c88f5
...
...
@@ -79,13 +79,13 @@ Tree[^1] $\mathcal{O}(\log{n})$ $\mathcal{O}(\log{n})$
:::{.fragment fragment-index=1}
1. Array w
hich has $n$ entrie
s
1. Array w
ith $n$ element
s
:::
:::{.fragment fragment-index=2}
2. Compar
ing
first elements
. Switch elements if left element is greater than
right
one
2. Compar
e
first
two
elements
, swap them if left >
right
:::
...
...
@@ -97,7 +97,7 @@ Tree[^1] $\mathcal{O}(\log{n})$ $\mathcal{O}(\log{n})$
:::{.fragment fragment-index=4}
4. Keep repeating
with $n-1$, $n-2$, ... entries
until everything is sorted
4. Keep repeating until everything is sorted
:::
...
...
@@ -106,17 +106,35 @@ Tree[^1] $\mathcal{O}(\log{n})$ $\mathcal{O}(\log{n})$
```{python}
#| echo: true
def Bubble_Sort(values):
num_elements = len(values)
for idx_cur_elem in range(num_elements):
for i in range(num_elements-idx_cur_elem-1):
if (values[i] > values[i+1]):
values[i], values[i+1] = values[i+1], values[i]
print(values)
def swap_if_larger(values):
for i in range(len(values) - 1):
if values[i] > values[i+1]:
values[i], values[i+1] = values[i+1], values[i]
print(values)
```
testvec = [6, 0, 3, 1, 5, 7, 4, 2]
Bubble_Sort(testvec)
:::: {.r-stack}
::: {.fragment .fade-out fragment-index=1 style="margin: 0;"}
```{python width=100%}
#| echo: true
test_values = [6, 0, 3, 5, 1]
swap_if_larger(test_values)
```
:::
::: {.fragment .fade-in fragment-index=1 style="margin: 0;"}
```{python}
#| echo: true
def bubble_sort(values):
for _ in values:
swap_if_larger(values)
test_values = [6, 0, 3, 5, 1]
bubble_sort(test_values)
```
:::
::::
## Merge Sort (1/3) {.leftalign}
...
...
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