Question

PLEASE HELP ASAP! in Python, I am supposed to create a program that compares insertion sorting...

PLEASE HELP ASAP! in Python, I am supposed to create a program that compares insertion sorting and selection sorting. My program only compares insertion and selection once. It is supposed to compare increasing, decreasing and an array of random values for each at 5 different lengths (without looping). Please help me. I have included my code below.

import time

import random

def insertion_sort(arr):

    for i in range(1, len(arr)):

        key = arr[i]

        j = i - 1

        while j >= 0 and key < arr[j]:

            arr[j + 1] = arr[j]

            j = j - 1

        arr[j + 1] = key

def selection_sort(arr):

    for i in range(len(arr)):

        min_index = i

        for j in range(i + 1, len(arr)):

            if arr[min_index] > arr[j]:

                min_index = j

        arr[i], arr[min_index] = arr[min_index], arr[i]

def main():

    arr = [random.randint(1, 10000) for i in range(10000)]

    dup_arr = [num for num in arr]

    start = time.process_time()

    insertion_sort(arr)

    end = time.process_time()

    print('Ten Thousand Increasing Insertion: {:.6f}'.format(end - start))

    start = time.process_time()

    selection_sort(dup_arr)

    end = time.process_time()

    print('Ten Thousand Increasing Selection: {:.6f}'.format(end - start))

    

main()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import time

import random

def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j = j - 1
arr[j + 1] = key
def selection_sort(arr):
for i in range(len(arr)):
min_index = i
for j in range(i + 1, len(arr)):
if arr[min_index] > arr[j]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
'''
methods that takes individual lengths and test selection and insertion sorts.
display reports
'''
def test_random(length):
arr = [random.randint(1, length) for i in range(length)]
dup_arr = [num for num in arr]
start = time.process_time()
insertion_sort(arr)
end = time.process_time()
print(length,'Random Insertion: {:.6f}'.format(end - start))
start = time.process_time()
selection_sort(dup_arr)
end = time.process_time()
print(length,'Random Selection: {:.6f}'.format(end - start))
def test_increasing(length):
arr = [i for i in range(length)]
dup_arr = [num for num in arr]
start = time.process_time()
insertion_sort(arr)
end = time.process_time()
print(length,'Increasing Insertion: {:.6f}'.format(end - start))
start = time.process_time()
selection_sort(dup_arr)
end = time.process_time()
print(length,'Increasing Selection: {:.6f}'.format(end - start))
def test_decreasing(length):
arr = [i for i in range(length)]
dup_arr = [num for num in arr]
start = time.process_time()
insertion_sort(arr)
end = time.process_time()
print(length,'Decreasing Insertion: {:.6f}'.format(end - start))
start = time.process_time()
selection_sort(dup_arr)
end = time.process_time()
print(length,'Decreasing Selection: {:.6f}'.format(end - start))
'''
call above sort testing methods recursively
with random length.
'''
def test_rec(num_of_times):
if(num_of_times>0):
print("\nTest :",num_of_times,"\n")
test_increasing(random.randint(1,1000))
test_decreasing(random.randint(1,1000))
test_random(random.randint(1,1000))
  
test_rec(num_of_times-1)
def main():
test_rec(5)
main()

Screenshots of code and output.

Add a comment
Know the answer?
Add Answer to:
PLEASE HELP ASAP! in Python, I am supposed to create a program that compares insertion sorting...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a file called Sort.py (note the capitalization). Within that file, write two different Python functions....

    Create a file called Sort.py (note the capitalization). Within that file, write two different Python functions. Each function will take an array of integers as a parameter and sort those integers in increasing order. One will use insertion sort, and the other will use selection sort (described below). Simple functions will suffice here; do not create any classes. Your insertion sort function should be called insertion_sort(arr). Your selection sort function should be called selection_sort(arr). Selection sort must use a while...

  • My tests for sorting algorithms are not working. sorting.py has 6 sorting algorithms and test_sorting.py has...

    My tests for sorting algorithms are not working. sorting.py has 6 sorting algorithms and test_sorting.py has test cases to run those algorithms, but test cases are not working. please correct the errors in test_sorting.py file so that it can test all the sorting algorithms. WARNING: DON'T COPY AND PASTE THE QUESTION IN ANSWER. I WILL REPORT YOU. sorting.py # 1. selection sort # 2. insertion sort # 3. shell sort # 4. heap sort # 5. merge sort # 6....

  • I am using python3.6 and i am getting an error on line 42 num = int(fin.readline())...

    I am using python3.6 and i am getting an error on line 42 num = int(fin.readline()) #reading first value valueError: invalid literal, for int() with base 10 Any help is appreciated, here is the code. reads from txt file a few integers in an array and sorts them. thank you! # Function to do insertion sort def insertionSort(arr):     # Traverse through 1 to len(arr)     for i in range(1, len(arr)):         key = arr[i]         # Move elements of...

  • without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr,...

    without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr, low, high, x): # Check base case if low > high : return None else: mid = (high + low) // 2 element arr[mid] == X: if element return mid elif element > X: return binary-search(arr, low, mid 1, x) else: return binary_search(arr, mid + 1, high, x) Selection Sort: def selection_sort (arr): for i in range (len(arr)): smallest index = i smallest value...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • I am trying to create tkinter GUI for guess a number game.I am not able to...

    I am trying to create tkinter GUI for guess a number game.I am not able to complete it .Could someone guide me to complete.I am so confused about using tkinter in while loop . Below code is not working correctly .Could someone please help with this ? def get_binary_digits(dividend): binary_digits = [] while dividend != 0: quot = dividend // 2 remainder = dividend % 2 binary_digits.append(remainder) dividend = quot # The quotient becomes the new dividend. binary_digits.reverse() return binary_digits...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to...

    I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT