asdf
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import random
|
||||
|
||||
def array(low, high, item):
|
||||
arr = []
|
||||
for i in range(item):
|
||||
arr.append(random.randint(low, high))
|
||||
return arr
|
||||
|
||||
arr = array(1, 3, 10)
|
||||
|
||||
print(arr)
|
||||
print(arr[0])
|
||||
print(arr[len(arr) - 1])
|
||||
|
||||
def checkfour(arr):
|
||||
for i in arr:
|
||||
if i == 4:
|
||||
print('vier enthalten')
|
||||
break
|
||||
else:
|
||||
print('keine Vier')
|
||||
break
|
||||
|
||||
checkfour(arr)
|
||||
|
||||
def randomnum(arr, num):
|
||||
for i in arr:
|
||||
if i == num:
|
||||
print('vier enthalten')
|
||||
break
|
||||
else:
|
||||
print('keine Vier')
|
||||
break
|
||||
randomnum(arr)
|
||||
@@ -0,0 +1,14 @@
|
||||
10 3 5 2 6 1
|
||||
3 5 2 6 1 10
|
||||
3 2 5 1 6 10
|
||||
2 3 1 5 6 10
|
||||
2 1 3 5 6 10
|
||||
1 2 3 5 6 10
|
||||
|
||||
10 3 5 2 6 1
|
||||
|
||||
|
||||
Rekursion
|
||||
|
||||
Rekursion ist eine Problemlösestrategie, bei der sich eine Funktion selbst aufruft.
|
||||
Dieses Verfahren wird so lange wiederholt, bis eine Abbruchbedingung erreicht ist.
|
||||
@@ -0,0 +1,21 @@
|
||||
import numpy as np
|
||||
|
||||
random_arr = np.random.randint(1,101,10)
|
||||
|
||||
print(random_arr)
|
||||
|
||||
sorted = False
|
||||
|
||||
while sorted == False:
|
||||
sorted = True
|
||||
for index in range(len(random_arr) - 1):
|
||||
if random_arr[index] > random_arr[index + 1]:
|
||||
|
||||
temp = random_arr[index]
|
||||
random_arr[index] = random_arr[index + 1]
|
||||
random_arr[index + 1] = temp
|
||||
sorted = False
|
||||
|
||||
print(random_arr)
|
||||
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import numpy as np
|
||||
|
||||
rand_arr = np.random.randint(1,11,10)
|
||||
|
||||
print(rand_arr)
|
||||
|
||||
# https://pythonguides.com/python-program-to-print-the-smallest-element-in-an-array/
|
||||
def sort():
|
||||
for index in range(len(rand_arr) - 1):
|
||||
min_index = index
|
||||
|
||||
for next_index in range(index + 1, len(rand_arr)):
|
||||
|
||||
if rand_arr[next_index] < rand_arr[min_index]:
|
||||
min_index = next_index
|
||||
|
||||
|
||||
rand_arr[index], rand_arr[min_index] = (rand_arr[min_index], rand_arr[index])
|
||||
print(rand_arr)
|
||||
|
||||
sort()
|
||||
@@ -0,0 +1,26 @@
|
||||
import numpy as np
|
||||
arr = np.random.randint(0, 11, 1000)
|
||||
print(arr)
|
||||
def part(arr, low, high):
|
||||
pivot = arr[high]
|
||||
swap_number = low - 1
|
||||
|
||||
for counter in range(low, high):
|
||||
if arr[counter] <= pivot:
|
||||
swap_number += 1
|
||||
arr[swap_number],arr[counter] = arr[counter], arr[swap_number]
|
||||
|
||||
arr[swap_number + 1],arr[high] = arr[high], arr[swap_number + 1]
|
||||
print(arr)
|
||||
|
||||
return swap_number + 1
|
||||
|
||||
def quicksort(arr, low, high):
|
||||
if low < high:
|
||||
recursion = part(arr, low, high)
|
||||
quicksort(arr, low, recursion - 1)
|
||||
quicksort(arr, recursion + 1, high)
|
||||
|
||||
quicksort(arr, 0, len(arr) - 1)
|
||||
print(arr)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import numpy as np
|
||||
|
||||
arr = np.random.randint(0, 1001, 10)
|
||||
print(arr)
|
||||
|
||||
def part(arr, low, high):
|
||||
pivot = arr[high]
|
||||
swap_number = low - 1
|
||||
|
||||
for i in range(low, high):
|
||||
if arr[i] <= pivot:
|
||||
swap_number += 1
|
||||
arr[swap_number],arr[i] = arr[i], arr[swap_number]
|
||||
|
||||
arr[swap_number + 1],arr[high] = arr[high], arr[swap_number + 1]
|
||||
print(arr)
|
||||
|
||||
return swap_number + 1
|
||||
|
||||
def quicksort(arr, low, high):
|
||||
if low < high:
|
||||
recursion = part(arr, low, high)
|
||||
quicksort(arr, low, recursion - 1)
|
||||
quicksort(arr, recursion + 1, high)
|
||||
|
||||
quicksort(arr, 0, len(arr) - 1)
|
||||
print('Final Sort:')
|
||||
print(arr)
|
||||
@@ -0,0 +1,35 @@
|
||||
7 2 9 4 1 8 3 6 5 0
|
||||
|
||||
1. Jeder Index im Array +1 wird durchlesen.
|
||||
2. Der Wert des Index wird in min-index gespeichert
|
||||
3. Gleichzeitig wird der rechte wert von dem ausgelesenem Wert ausgelesen und abgespeichert
|
||||
4. Wenn der Rechte verglichene Wert Kleiner als der ausgelesene Wert ist, dann wird der wert in min-index gespeichert
|
||||
5. Wenn er einmal durch den Array gegangen ist dann tausche Den min-index mit index und gehe zum nächsten index
|
||||
6. Wiederhole bis nicht getauscht wurde
|
||||
|
||||
Selection-Sort:
|
||||
|
||||
0 2 9 4 1 8 3 6 5 7
|
||||
0 1 9 4 2 8 3 6 5 7
|
||||
0 1 2 4 9 8 3 6 5 7
|
||||
0 1 2 3 9 8 4 6 5 7
|
||||
0 1 2 3 4 8 9 6 5 7
|
||||
0 1 2 3 4 5 9 6 8 7
|
||||
0 1 2 3 4 5 6 9 8 7
|
||||
0 1 2 3 4 5 6 7 8 9
|
||||
|
||||
Quicksort:
|
||||
|
||||
7 294183650
|
||||
links: 2413650
|
||||
rechts: 89
|
||||
gleich: 7
|
||||
|
||||
links + gleich + rechts
|
||||
2 4 1 3 6 5 0 7 8 9
|
||||
|
||||
7 steht an der richtigen Stelle.
|
||||
|
||||
quicksort(2413650) + 7 + quicksort(89)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user