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...
Write a python program to create a list of integers using random function. Use map function...
Write a python program to create a list of integers using random function. Use map function to process the list on the series: x + x2/2! + x3/3! + ….n and store the mapped elements in another list. Assume the value of n as 10
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...
Sorting and Searching Given an unsorted array numbers of integers with duplicate values. Sort the array...
Sorting and Searching 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...
Python program please no def, main, functions Given a list of negative integers, write a Python...
Python program please no def, main, functions Given a list of negative integers, write a Python program to display each integer in the list that is evenly divisible by either 5 or 7. Also, print how many of those integers were found. Sample input/output: Enter a negative integer (0 or positive to end): 5 Number of integers evenly divisible by either 5 or 7: 0 Sample input/output: Enter a negative integer (0 or positive to end): -5 -5 is evenly...
Write a Python program containing a function named scrabble_sort that will sort a list of strings...
Write a Python program containing a function named scrabble_sort that will sort a list of strings according to the length of the string, so that shortest strings appear at the front of the list. Words that have the same number of letters should be arranged in alphabetical order. Write your own logic for the sort function (you may want to start from some of the existing sorting code we studied). Do NOT use the built-in sort function provided by Python....
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.
Given a list of negative integers, write a Python program to display each integer in the...
Given a list of negative integers, write a Python program to display each integer in the list that is evenly divisible by either 5 or 7. Also, print how many of those integers were found. Sample input/output: Enter a negative integer (0 or positive to end): 5 Number of integers evenly divisible by either 5 or 7: 0 Sample input/output: Enter a negative integer (0 or positive to end): -5 -5 is evenly divisible by either 5 or 7. Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT