28.09.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import random
|
||||
import time
|
||||
|
||||
|
||||
def selectionsort(array):
|
||||
anzahlVergleiche = 0
|
||||
anzahlTauschvorgaenge = 0
|
||||
|
||||
n = len(array)
|
||||
|
||||
startzeit = time.perf_counter()
|
||||
|
||||
for i in range(n - 1):
|
||||
min_index = i
|
||||
|
||||
for j in range(i + 1, n):
|
||||
anzahlVergleiche += 1
|
||||
|
||||
if array[j] < array[min_index]:
|
||||
min_index = j
|
||||
|
||||
if min_index != i:
|
||||
array[i], array[min_index] = array[min_index], array[i]
|
||||
anzahlTauschvorgaenge += 1
|
||||
|
||||
endzeit = time.perf_counter()
|
||||
|
||||
return anzahlVergleiche, anzahlTauschvorgaenge, endzeit - startzeit
|
||||
|
||||
|
||||
# 1000 unterschiedliche Zahlen zwischen 1 und 100.000
|
||||
data = random.sample(range(1, 100001), 100000)
|
||||
|
||||
print("=" * 45)
|
||||
print(" SELECTIONSORT")
|
||||
print("=" * 45)
|
||||
|
||||
vergleiche, tausche, zeit = selectionsort(data)
|
||||
|
||||
print("\nSortierung abgeschlossen!")
|
||||
print("-" * 45)
|
||||
print("Anzahl Zahlen: ", len(data))
|
||||
print("Anzahl Vergleiche: ", vergleiche)
|
||||
print("Anzahl Tauschvorgänge:", tausche)
|
||||
print("Benötigte Zeit: ", zeit, "Sekunden")
|
||||
print("-" * 45)
|
||||
|
||||
print("\nErste 20 Zahlen des sortierten Arrays:")
|
||||
print(data[:20])
|
||||
|
||||
print("=" * 45)
|
||||
Reference in New Issue
Block a user