From cf658c28dd4132e1117760f50df4af0ccddbe3f1 Mon Sep 17 00:00:00 2001 From: Rsclub2 2 Date: Thu, 28 Apr 2022 14:38:26 +0200 Subject: [PATCH] Nizza --- 3. Übungen/28.04.2022/insertionsort.py | 31 ++++++++++++++++++++++++++ 3. Übungen/28.04.2022/selectionsort.py | 23 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 3. Übungen/28.04.2022/insertionsort.py create mode 100644 3. Übungen/28.04.2022/selectionsort.py diff --git a/3. Übungen/28.04.2022/insertionsort.py b/3. Übungen/28.04.2022/insertionsort.py new file mode 100644 index 0000000..f4fea8e --- /dev/null +++ b/3. Übungen/28.04.2022/insertionsort.py @@ -0,0 +1,31 @@ +# Make a programm which asks the user for an input and then sorts the input of the user + +# First ask the user how long he want the Array to be +array_length=int(input("Wie viele Zahlen sollen sortiert werden")) +# Second make an array +array=[] +# Third ask the user for number input +for i in range(array_length): + number=float(input("Bitte mir einfach eine Zahl geben")) + # Now append that number to the array + array.append(number) +# Now sort the numbers with the help of insertion sort +def insertionSort(array): + + # Traverse through 1 to len(arr) + for i in range(1, len(array)): + + key = array[i] + + # Move elements of arr[0..i-1], that are + # greater than key, to one position ahead + # of their current position + j = i-1 + while j >=0 and key < array[j] : + array[j+1] = array[j] + j -= 1 + array[j+1] = key +insertionSort(array) +# print finished array +for i in range(len(array)): + print(array[i]) \ No newline at end of file diff --git a/3. Übungen/28.04.2022/selectionsort.py b/3. Übungen/28.04.2022/selectionsort.py new file mode 100644 index 0000000..8ae7423 --- /dev/null +++ b/3. Übungen/28.04.2022/selectionsort.py @@ -0,0 +1,23 @@ +# Make a programm which asks the user for an input and then sorts the input of the user + +# First ask the user how long he want the Array to be +array_length=int(input("Wie viele Zahlen sollen sortiert werden")) +# Second make an array +array=[] +# Third ask the user for number input +for i in range(array_length): + number=float(input("Bitte mir einfach eine Zahl geben")) + # Now append that number to the array + array.append(number) + +# Now sort them with the Help of the selection sort algorithm +for i in range(len(array)): + min_= i + for j in range(i+1, len(array)): + if array[min_] > array[j]: + min_ = j + #swap +array[i], array[min_] = array[min_], array[i] +# main +for i in range(len(array)): + print(array[i]) \ No newline at end of file