Advertisement
Ryan_Ho

Bubble Sort #3 Gone Wrong (SERIOUS) (COPS CALLED) (TOTALLY NOT CLICKBAIT)

Nov 10th, 2022 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. import random
  2. temp = 0
  3.  
  4. def bubble_sort_version_1(items):
  5.     num_items = len(items)
  6.  
  7.     for pass_num in range(1, num_items):
  8.         for index in range(0, num_items - 1):
  9.             if items[index] > items[index + 1]:
  10.                 temp = items[index]
  11.                 items[index] = items[index + 1]
  12.                 items[index + 1] = temp
  13.         print(f"B1: Pass {pass_num}: {items}   Comparisons: {index + 1}")
  14.  
  15. def bubble_sort_version_2(items):
  16.     num_items = len(items)
  17.     swapped = True
  18.     pass_num = 0
  19.  
  20.     while swapped:
  21.         swapped = False
  22.         for index in range(0, num_items - 1):
  23.             if items[index] > items[index + 1]:
  24.                 temp = items[index]
  25.                 items[index] = items[index + 1]
  26.                 items[index + 1] = temp
  27.                 swapped = True
  28.         pass_num += 1
  29.         print(f"B2: Pass {pass_num}: {items}   Comparisons: {index + 1}")
  30.  
  31. def bubble_sort_version_3(items):
  32.     num_items = len(items)
  33.     swapped = True
  34.     pass_num = 1
  35.  
  36.     while swapped:
  37.         swapped = False
  38.         for index in range(0, num_items - pass_num):
  39.             if items[index] > items[index + 1]:
  40.                 temp = items[index]
  41.                 items[index] = items[index + 1]
  42.                 items[index + 1] = temp
  43.                 swapped = True
  44.  
  45.         print(f"B3: Pass {pass_num}: {items}, Comparisons: {index + 1}")
  46.         pass_num = [pass_num + 1]
  47.  
  48. def main():
  49.     test_items = []
  50.     arr1 = []
  51.     arr2 = []
  52.     arr3 = []
  53.     for i in range(13):
  54.         #test_items.append(random.randrange(400))
  55.         num = random.randrange(400)
  56.         arr1.append(num)
  57.         arr2.append(num)
  58.         arr3.append(num)
  59.  
  60.     print(f"Data: {test_items}")
  61.  
  62. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement