diff --git a/Rekursion.py b/Rekursion.py index 212ab83..d555c07 100644 --- a/Rekursion.py +++ b/Rekursion.py @@ -6,7 +6,7 @@ def fakultaet(n): else: return n * fakultaet(n-1) -print(fakultaet(int(input("Geben Sie eine Zahl ein: ")))) +#print(fakultaet(int(input("Geben Sie eine Zahl ein: ")))) # FIBONACCI REKURSION # Aufgabe 2 @@ -18,4 +18,15 @@ def fibonacci(n): else: return fibonacci(n-1) + fibonacci(n-2) -print(fibonacci(int(input("Geben Sie eine Zahl ein: ")))) \ No newline at end of file +#print(fibonacci(int(input("Geben Sie eine Zahl ein: ")))) + +#Rekursive Berechnung der Fibonacci-Zahlen +def fibonacci_recursive(n): + if n <= 0: + return 0 + elif n == 1: + return 1 + else: + return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2) + +print(fibonacci_recursive(int(input("Geben Sie eine Zahl ein: ")))) \ No newline at end of file diff --git a/Sort2.py b/SelectionSort.py similarity index 97% rename from Sort2.py rename to SelectionSort.py index faa335f..1948301 100644 --- a/Sort2.py +++ b/SelectionSort.py @@ -1,21 +1,21 @@ -Array = [3,2,1,5,4] -Länge = len(Array) - -def selection_sort(): - anzahlVergleiche = 0 - anzahlTauschungen = 0 - for i in range(Länge): - min_index = i - for j in range(i+1, Länge): - print(f"Vergleich: Array[{min_index}] = {Array[min_index]} und Array[{j}] = {Array[j]}") - if Array[j] < Array[min_index]: - min_index = j - anzahlVergleiche += 1 - if min_index != i: - Array[i], Array[min_index] = Array[min_index], Array[i] - anzahlTauschungen += 1 - print(f"Vertauscht: Array[{i}] = {Array[i]} und Array[{min_index}] = {Array[min_index]}") - print() # Neue Zeile nach jedem Durchlauf - print("Sortiert sieht das Array A so aus:", Array) - print(f"Anzahl der Vergleiche: {anzahlVergleiche}") +Array = [3,2,1,5,4] +Länge = len(Array) + +def selection_sort(): + anzahlVergleiche = 0 + anzahlTauschungen = 0 + for i in range(Länge): + min_index = i + for j in range(i+1, Länge): + print(f"Vergleich: Array[{min_index}] = {Array[min_index]} und Array[{j}] = {Array[j]}") + if Array[j] < Array[min_index]: + min_index = j + anzahlVergleiche += 1 + if min_index != i: + Array[i], Array[min_index] = Array[min_index], Array[i] + anzahlTauschungen += 1 + print(f"Vertauscht: Array[{i}] = {Array[i]} und Array[{min_index}] = {Array[min_index]}") + print() # Neue Zeile nach jedem Durchlauf + print("Sortiert sieht das Array A so aus:", Array) + print(f"Anzahl der Vergleiche: {anzahlVergleiche}") print(f"Anzahl der Tauschungen: {anzahlTauschungen}") \ No newline at end of file diff --git a/inplacequicksort.py b/inplacequicksort.py new file mode 100644 index 0000000..62f70d2 --- /dev/null +++ b/inplacequicksort.py @@ -0,0 +1,24 @@ +# Den quicksort Algorithmus in Python implementieren, der eine Liste von Zahlen sortiert. +# Der Algorithmus wählt ein Pivot-Element aus der Liste und teilt die anderen Elemente in drei Listen auf: +# diejenigen, die kleiner als das Pivot sind, diejenigen, die gleich dem Pivot sind, und diejenigen, die größer als das Pivot sind. +# Dann wird der Algorithmus rekursiv auf die kleineren und größeren Listen angewendet. +# Diesmal soll das Ganze "inplace" geschehen, d.h. die Liste soll direkt sortiert werden, ohne dass zusätzliche Listen erstellt werden. + +array = [7, 2, 9, 4, 1, 8, 3, 6, 5, 0] +def inplace_quicksort(array, low, high): + if low < high: + pivot_index = partition(array, low, high) + inplace_quicksort(array, low, pivot_index - 1) + inplace_quicksort(array, pivot_index + 1, high) +def partition(array, low, high): + pivot = array[high] + i = low - 1 + for j in range(low, high): + if array[j] <= pivot: + i += 1 + array[i], array[j] = array[j], array[i] + array[i + 1], array[high] = array[high], array[i + 1] + return i + 1 + +inplace_quicksort(array, 0, len(array) - 1) +print(array) diff --git a/turtlerekursion.py b/turtlerekursion.py index 98f7f07..cd3fbd1 100644 --- a/turtlerekursion.py +++ b/turtlerekursion.py @@ -1,9 +1,18 @@ import turtle +import sys +sys.setrecursionlimit(1000000) def draw_square(t, size): - for i in range(4): - t.forward(size) - t.left(90) + + t.forward(size / 2) + t.left(90) + t.forward(size) + t.left(90) + t.forward(size) + t.left(90) + t.forward(size) + t.left(90) + t.forward(size / 2) # draw_square(turtle, 100) @@ -28,38 +37,69 @@ def draw_recursive_tree(t, size, größe): draw_recursive_tree(t, size * 0.7, größe - 1) t.left(30) t.backward(size) -turtle.left(90) +#turtle.left(90) #draw_recursive_tree(turtle, 100, 5) + + # Ich möchte einen Code schreiben der erst ein 64 Quadrat zeichnet # und dann rekursiv immer zwei halb so große Quadrate oben links und rechts über das ursprüngliche Quadrat zeichnet. # Bis die Seitenlänge = 1 ist. -def draw_recursive_squares(t, length): +# Es soll nur eine Turtle geben, die wiederverwendet wird. + + +def draw_recursive_squares(t, length, x, y): if length < 1: + draw_recursive_squares(t, length / 2, x - length * 2.5, y + length * 2.5) return - - draw_square(t, length) - - # Position des aktuellen Quadrats x = t.xcor() y = t.ycor() - # Turtle für oben links - left_turtle = t.clone() - left_turtle.penup() - left_turtle.goto(x - length * 1.5, y + length) - left_turtle.pendown() + + t.penup() + t.goto(x + length * 1.5 , y + length * 2.5) + t.pendown() + + + draw_square(t, length) + + - # Turtle für oben rechts - right_turtle = t.clone() - right_turtle.penup() - right_turtle.goto(x + length , y + length) - right_turtle.pendown() + draw_recursive_squares(t, length / 2, x - length * 2.5, y + length * 2.5) +#square = draw_square(turtle, 64) +#turtle.speed(10) +#draw_recursive_squares(turtle, 32, 0,0) - # Beide Äste rekursiv weiterführen - draw_recursive_squares(left_turtle, length / 2) - draw_recursive_squares(right_turtle, length / 2) -turtle.speed(10) -draw_recursive_squares(turtle, 64) +t = turtle.Turtle() +t.speed(2000) +#t.speed(0) -turtle.done() \ No newline at end of file +def square_recursion(t, length, x, y): + if length < 8: + return + print(length) + t.penup() + t.goto(x, y) + t.pendown() + draw_square(t, length) + square_recursion(t, length / 2, x - length * 1.5, y + length * 1.5) + square_recursion(t, length / 2, x + length * 1.5, y + length * 1.5) + + +#square_recursion(t, 64, 0, 0) +#turtle.done() + +# Jetzt möchte ich eine Kochkurve zeichnen, die rekursiv immer kleinere Dreiecke zeichnet. +def draw_koch_Kurve(t, length, depth): + if depth == 0: + t.forward(length) + return + t.forward(length / 3) + t.left(60) + draw_koch_Kurve(t, length / 3, depth - 1) + t.right(120) + draw_koch_Kurve(t, length / 3, depth - 1) + t.left(60) + draw_koch_Kurve(t, length / 3, depth - 1) + +draw_koch_Kurve(t, 300, 10) \ No newline at end of file