2022-04-28 14:40:33 +02:00

31 lines
991 B
Python

# 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])