Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #This program takes an unsorted list and sorts it, then stores the results in copy_list
- from random import randint
- #my_list becomes filled with a collection of random integers, the amount of which is specified the user;
- #individual list items could be specified by replacing randint with another input block
- my_list = []
- for i in range(int(input("Number of items in unsorted list:"))+1):
- my_list.append(randint(0, 10))
- copy_list = []
- def bubble_sort(unsorted):
- #iterates through original list and copies each item into copy_list
- for item in my_list:
- copy_list.append(item)
- #sets loop condition so loop will proceed
- swapped = True
- while swapped == True:
- #sets loop to terminate after having iterated and swapped all necessary items in copy list
- swapped = False
- for num in range(len(copy_list)-1):
- #swaps item order if a preceding number is greater than that which follows
- if copy_list[num] > copy_list[num + 1]:
- copy_list[num], copy_list[num + 1] = copy_list[num + 1], copy_list[num]
- #sets loop to continue until numbers are ordered
- swapped = True
- #Calls function with input list
- bubble_sort(my_list)
- #prints original list and copied list
- print(my_list)
- print(copy_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement