def letsSort(list): # ask what method user wants print("""How would you like to sort the list: Bubble Sort [1] Selection Sort [2] Insertion Sort [3] Merge Sort [4] Quick Sort [5] Quit [6]""") choice = input("> ") choice = int(choice) # check choice for quit option if choice == 6: print("You are now leaving... Goodbye.") return # ask how many numbers in list print("\nHow many random integers do you want in the list?") num = int(input("> ")) # add random numbers to list x = 0 for x in range(num): number = random.randint(1, 10000) list.append(number) x = x + 1 #send list to sort method if choice == 1: list = bubble_sort(list) elif choice == 2: list = selection_sort(list) elif choice == 3: list = insertion_sort(list) elif choice == 4: list = merge_sort(list) elif choice == 5: list = quick_sort(list) #print list print("Here is the sorted list:") print(list) print() #clear list list.clear() #loop back to start letsSort(list)
def sorting(): x = True y = 0 while x is True: y = int(input("""Please type the number of the sort method you want to use: 1. Bubble sort (not that good) 2. Selection sort 3. Insertion sort 4. Merge sort 5. Quick sort 6. Exit >> """)) num_of_sort = int(input("How many numbers would you like to sort: ")) my_list = [] for num in range(num_of_sort): my_list.append((random.randint(1,100000))) if y == 1: method = bubble_sort(my_list) print(" Your list sorted using bubble sort.") print(method) sorting() elif y == 2: method = selection_sort(my_list) print(" Your list sorted using selection sort.") print(method) sorting() elif y == 3: method = insertion_sort(my_list) print(" Your list sorted using insertion sort.") print(method) sorting() elif y == 4: method = merge_sort(my_list) print(" Your list printed using merge sort") print(method) sorting() elif y == 5: method = quick_sort(my_list) print(" Your list printed using quick sort") print(method) sorting() elif y == 6: x = False
) print(""" 1. Bubble sort 2. Selection Sort 3. Insertion Sort 4. Merge Sort 5. Quick Sort 6. Quit""") algorithm = int(input()) if algorithm != 6: # asking user for length of list print("How many numbers would you like in the list") listLength = int(input()) # creating the list and putting it into the algorithm for x in range(listLength): examList.append(randint(0, 100000)) if algorithm == 1: examList = bubble_sort(examList) elif algorithm == 2: examList = selection_sort(examList) elif algorithm == 3: examList = insertion_sort(examList) elif algorithm == 4: examList = merge_sort(examList) elif algorithm == 5: examList = quick_sort(examList) # printing and clearing the list to run it again print("This is the sorted list after using the method you selected") print(examList) examList.clear()
# not just this one Sorting_List.clear() # This only shows up here because it repeats after each menu option # It also gives the visual to the user that the list is now empty print( "We just cleared the list so you can try other sorts or try this type again." ) print(Sorting_List) if menu == 2: print("How many times do you want to add a number to the list?") times_to_run = int(input()) for i in range(times_to_run): x = random.randint(0, 100000) Sorting_List.append(x) selection_sort(Sorting_List) print(Sorting_List) Sorting_List.clear() if menu == 3: print("How many times do you want to add a number to the list?") times_to_run = int(input()) for i in range(times_to_run): x = random.randint(0, 100000) Sorting_List.append(x) insertion_sort(Sorting_List) print(Sorting_List) Sorting_List.clear() if menu == 4: print("How many times do you want to add a number to the list?")
def menu(): print(""" Enter 1 to use bubble sort, Enter 2 to use selection sort, Enter 3 to use insertion sort, Enter 4 to use merge sort, Enter 5 to use quick sort. Enter 6 to exit """) answer = int(input(">")) while answer <= 1 >= 6: # This is the infinite loop menu() if answer == 1: print("You chose bubble sort.") number = int(input("How many numbers would you like to sort?>")) print( f"Ok so there will be {number} numbers randomly generated from 1-100,000 and added to this list" ) print("Give me a couple seconds will ya??") time.sleep(2) for x in range( number ): # gets the number from the user and runs the randint as many times as specified arr.append(randint(1, 100000)) print("Here is your list", arr) # Shows the array before the sort input("Press enter to sort!") bubble_sort( arr ) # shows the array after the sort, # Calls the bubble_sort method from Search_methods.py print("Here is your list sorted using the bubble sort method!") print(arr) return arr.clear(), input("Enter to continue"), menu() elif answer == 2: print("You chose selection sort.") number = int(input("How many numbers would you like to sort?>")) print( f"Ok so there will be {number} numbers randomly generated from 1-100,000 and added to this list" ) print("Give me a couple seconds will ya, I hate being rushed!!") time.sleep(2) # calls the time function imported for num in range(number): arr.append(randint(1, 100000)) # Calls the randint function imported print("Here is your list", arr) input("Press enter to sort!") selection_sort( arr) # Calls the selection_sort method from Search_methods.py print(arr) return arr.clear(), input("Enter to continue"), menu() elif answer == 3: number = int(input("How many numbers would you like to sort?>")) print( f"Ok so there will be {number} numbers randomly generated from 1-100,000 and added to this list" ) print("Give me a couple seconds will ya, I hate being rushed!!") time.sleep(2) for num in range(number): arr.append(randint(1, 100000)) print("Here is your list", arr) input("Press enter to sort!") insertion_sort( arr) # Calls the insertion_sort method from Search_methods.py print(arr) return arr.clear(), input("Enter to continue"), menu() elif answer == 4: number = int(input("How many numbers would you like to sort?>")) print( f"Ok so there will be {number} numbers randomly generated from 1-100,000 and added to this list" ) print("Give me a couple seconds will ya, I hate being rushed!!") time.sleep(2) for num in range(number): arr.append(randint(1, 100000)) print("Here is your list", arr) input("Press enter to sort!") merge_sort(arr) # Calls the merge_sort method from Search_methods.py print(arr) return arr.clear(), input("Enter to continue"), menu() elif answer == 5: number = int(input("How many numbers would you like to sort?>")) print( f"Ok so there will be {number} numbers randomly generated from 1-100,000 and added to this list" ) print( "Give me a couple seconds will ya, I hate being rushed!!" ) # I really do hate being rushed, Thanks for giving us the weekend time.sleep(2) for num in range(number): arr.append(randint(1, 100000)) print("Here is your list", arr) input("Press enter to sort!") quick_sort(arr) # Calls the quick_sort method from Search_methods.py print(arr) return arr.clear(), input( "Enter to continue"), menu() # Clears the list and reruns the menu else: if answer == 6: exit( "I didn't wanna sort for you anyway." ) # tells the user that it doesnt wanna sort for them anyway!!!!!
print("6. Exit Program") method = int(input("\n>")) print("\nHow many numbers do you want in your list?") number = int(input(">")) for i in range(0, number): n = random.randint(1, 100000) user_list.append(n) print(f"\nHere's you original list : {user_list}") if method == 1: bubble_sort(user_list) print(f"\nHere's your sorted list {user_list}") elif method == 2: selection_sort(user_list) print(f"\nHere's your sorted list {user_list}") elif method == 3: insertion_sort(user_list) print(f"\nHere's your sorted list {user_list}") elif method == 4: merge_sort(user_list) print(f"\nHere's your sorted list {user_list}") elif method == 5: quick_sort(user_list) print(f"\nHere's your sorted list {user_list}")