-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubblesort_names.py
36 lines (26 loc) · 1020 Bytes
/
bubblesort_names.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def main():
# Initialize an empty list to store names
names = []
# Prompt the user to input 5 names and add them to the list
for _ in range(0, 5):
names.append(input("Enter a name: "))
# Call the 'sort_names' function to sort the list
sorted_names = sort_names(names)
# Print the sorted names
for x in sorted_names:
print(x)
def sort_names(names):
# This function performs bubble sort on the list of names
while True:
swaps = 0
# Iterate through the list and compare adjacent names
for i in range(0, len(names) - 1):
if names[i] > names[i + 1]:
# If the current name is greater than the next name, swap them
names[i], names[i + 1] = names[i + 1], names[i]
swaps += 1
# If no swaps were made in a pass, the list is sorted
if swaps == 0:
return names
if __name__ == "__main__":
main() # Call the main function when the script is executed.