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

1. Write a python program to create a list of integers using random function. Use map...
1. Write a python program to create a list of integers using random function. Use map function to process the list on the expression: 3x2+4x+5 and store the mapped elements in another list. Now use filter to do sum of all the elements in another list. 2. Write a function that takes n as input and creates a list of n lists such that ith list contains first 10 multiples of i. 3. Write a function that takes a number...
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 program in Python that given a list of positive integers, return another list where...
#Write a program in Python that given a list of positive integers, return another list where each element corresponds to the sum of the digits of the elements o#f the given list. #Example: # input # alist=[124, 5, 914, 21, 3421] # output # sum=[7, 5, 14, 3, 18]
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.
In python write a program that gets a list of integers from input, and outputs non-negative...
In python write a program that gets a list of integers from input, and outputs non-negative integers in ascending order (lowest to highest). Ex: If the input is: 10 -7 4 39 -6 12 2 the output is: 2 4 10 12 39 For coding simplicity, follow every output value by a space. Do not end with newline
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
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...
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()
1.)recursive merge sort on a list.(Python) 2.)recursive bubble sort using a list without enumerate() function.(python) Show...
1.)recursive merge sort on a list.(Python) 2.)recursive bubble sort using a list without enumerate() function.(python) Show Base case, and recursive case.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT