Question

In: Computer Science

Create a Python program that will take an unsorted list of 1000 integers and sort them...

Create a Python program that will take an unsorted list of 1000 integers and sort them using a bubble sort and an insertion sort. Your output should include displaying both sorted lists (from each algorithm) identifying each sorted list by the algorithm used.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code with no comments, for easy and error free copying

import random


def bubble_sort(lst):
    for i in range(len(lst)):
        for j in range(len(lst) - i - 1):
            if lst[j] > lst[j + 1]:
                lst[j], lst[j + 1] = lst[j + 1], lst[j]


def insertion_sort(lst):
    for i in range(1, len(lst)):
        key = lst[i]
        j = i - 1
        while j >= 0 and key < lst[j]:
            lst[j + 1] = lst[j]
            j -= 1
        lst[j + 1] = key


def main():
    lst=[random.randint(0,1000) for i in range(1000)]
    lst1=lst.copy()
    lst2=lst.copy()
    bubble_sort(lst1)
    print("list sorted using bubble sort:\n")
    print(lst1)
    insertion_sort(lst2)
    print("\nlist sorted using insertion sort:\n")
    print(lst2)

main()

#same code with comments, for learning

import random


# method to sort a list using bubble sort algorithm
def bubble_sort(lst):
    # looping from i=0 to length-1
    for i in range(len(lst)):
        # looping from j=0 to length-i-2
        for j in range(len(lst) - i - 1):
            if lst[j] > lst[j + 1]:  # comparing elements at j and j+1
                # swapping values at j and j+1
                lst[j], lst[j + 1] = lst[j + 1], lst[j]


# method to sort a list using insertion sort algorithm
def insertion_sort(lst):
    # looping from i=1 to length-1
    for i in range(1, len(lst)):
        # storing value at i as key value
        key = lst[i]
        # moving elements between indices 0 and i-1 that are greater than key to one place right
        j = i - 1
        while j >= 0 and key < lst[j]:
            lst[j + 1] = lst[j]
            j -= 1
        # adding key to index j+1
        lst[j + 1] = key


# main method
def main():
    # creating a list containing 1000 random integers between 0 and 1000
    lst = [random.randint(0, 1000) for i in range(1000)]
    # taking two copies of lst
    lst1 = lst.copy()
    lst2 = lst.copy()
    # passing first copy to bubble_sort(), printing sorted list
    bubble_sort(lst1)
    print("list sorted using bubble sort:\n")
    print(lst1)
    # passing second copy to insertion_sort(), printing sorted list
    insertion_sort(lst2)
    print("\nlist sorted using insertion sort:\n")
    print(lst2)


# calling main()
main()

#output (random)


Related Solutions

IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
In python write a program that first creates a list with the integers 0 through 9...
In python write a program that first creates a list with the integers 0 through 9 and then traverses that list RECURSIVELY (no for/while loops allowed) and prints out the integers on the list. NOTE: creating the list does not have to be done recursively.
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Q.1: create a python function that takes two integers from the user ( pass them from...
Q.1: create a python function that takes two integers from the user ( pass them from the main program). The function performs the addition operation and returns the result. In the program, ask the user to guess the result. Then, the program compares the user input ( guess) with the returned value, and displays a message that tells if the user guessing is correct or not. --define the function here --- # the program starts here print(“ This program tests...
Create an unsorted LIST class. Each list should be able to store 100 names.
Create an unsorted LIST class. Each list should be able to store 100 names.
create a file with 1000 alphanumeric ones written one per line. Write a program in python...
create a file with 1000 alphanumeric ones written one per line. Write a program in python that randomly selects 100 of them, holds the index of each alphanumeric, adds the hash of the previous 100 and tries to find a random alphanumeric so that if it adds it to the list of indicators the SHA256 of the whole to have 10 zeros at the end. You start with the original Hash: 1111..111 my solution so far: import random import string...
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program:...
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program: Your program will create a username of your choice using a Kali Linux command "useradd -m mark", then set the password for that user using the Kali Linux Command "echo "mark:10101111" | chpasswd". Then you create a dictionary file using the Kali Linux command "crunch 8 8 01 > mylist.txt" Your python script should crack the password for that user and display on the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT