Bubble Sort

Apr 25th, 2021
59
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from random import randint
2. from random import sample
3.
4. def generateRandomList(numberOfElements):
5.     elements = []
6.     for i in range(0,numberOfElements):
7.         value = randint(1,100)
8.         elements.append(value)
9.     return elements
10.
11. def generateRandomAlphabet():
12.     letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
13.     elements = sample(letters,26)
14.     return elements
15.
16. def bubbleSort(elements):
17.     print('Bubble Sort')
18.     swapped = True
19.     n = len(elements) - 1
20.     while swapped:
21.         swapped = False
22.         for i in range(n):
23.             if elements[i] > elements[i+1]:
24.                 elements[i], elements[i+1] = elements[i+1],elements[i]
25.                 swapped = True
26.         n -= 1
27.     return elements
28.
29. #elements = generateRandomList(20)
30. elements = generateRandomAlphabet()
31. elementsSorted = bubbleSort(elements[:])
32. print('Original List: {}'.format(elements))
33. print('Sorted List  : {}'.format(elementsSorted))
34.