mqxym

BubbleSort.py

Feb 19th, 2024 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | Source Code | 0 0
  1. # Tauscht die Array-Werte
  2. def swap(arr, pos1, pos2):
  3.     tmp = arr[pos1]
  4.     arr[pos1] = arr[pos2]
  5.     arr[pos2] = tmp
  6.     return arr
  7.  
  8. # Variante 1
  9. arr = [57,19,48,26,25,90,73,12,34,29,29,29,200,29,212,73,293,2,203,284,39,123]
  10. pos = 0
  11. max = len(arr)-1
  12.  
  13. while (pos < max):
  14.     if (arr[pos] > arr[pos+1]):
  15.         arr = swap(arr, pos, pos+1)
  16.        
  17.     pos += 1
  18.     if pos == max:
  19.         pos = 0
  20.         max -= 1
  21.        
  22. print('Fertig:')
  23. print(arr)
  24.  
  25. # Variante 2
  26. arr = [7, 3, 5, 9, 4, 1]
  27. for x in range(len(arr)):
  28.     for i in range(len(arr)-x-1):
  29.         if arr[i] > arr[i+1]:
  30.             arr = swap(arr, i, i+1)
  31.  
  32. print('Fertig:')
  33. print(arr)
Add Comment
Please, Sign In to add comment