Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Task 2. Implement Insertion Sort (4 points) # Insertion Sort # Please implement the function which takes as an input list of numbers and
Task 2. Implement Insertion Sort (4 points) # Insertion Sort # Please implement the function which takes as an input list of numbers and returns the sorted list # Use Insertion Sort algorithm def insertion_sort (nums): # implement Working Algorithm for j in range(1, len(nums)): item = nums[j] i = j - 1 while i >= 0 and nums[i] > item: nums[i+1] = nums[i] i i 1 nums[i+1] = item return nums # [5,4,3], j = 1, i = 0 # testing code # do NOT modify testing code below def test_sorting (sorting_algorithm): nums = [5,4,3,2,10] expected = [2,3,4,5,10] actual sorting_algorithm (nums) assert expected == actual, "Mistake in implementation" # if your implementation is correct, you should see "OK" # if your implementation is not correct, you will see "Mistake in implementation" test_sorting (insertion_sort) print('OK') OK https://colab.research.google.com/drive/1BWDgthGE2aEhNLH3R1TfPcvutYcjqXEh?usp=sharing#printMode=true 9/15/23, 11:25 PM CS566Lab2.ipynb - Colaboratory Write analysis of the Memory Complexity and Time Complexity using Asymptotic Notation O. (1 point) Memory Analysis: 0(1) Time Analysis: O(n^2) Invariant: Everything to the left of the index J is sorted in ascending order. Task 3. Write the function which will compute factorial. Use recursion, meaning call the function itself inside of your function. Use the box below, to paste the working code. The format of the code should be identical to LeetCode platform. (4 points) class Solution: def factorial (self, n: int) -> int: # implement Working Algorithm File " ", line 3 # implement Working Algorithm SyntaxError: incomplete input SEARCH STACK OVERFLOW #test_case_1 expected, nums = 120, 5 actual Solution().factorial (nums) assert expected=-actual, "Mistake in test case 1" #test_case_2 expected, nums = 24, 4 actual Solution().factorial (nums) assert expected=-actual, "Mistake in test case 2" #test_case_3 expected, nums = 6, 3 actual Solution().factorial (nums) assert expected== actual, "Mistake in test case 2" NameError Traceback (most recent call last) in () 1 #test_case_1 2 expected, nums = 120, 5 --> 3 actual = Solution().factorial (nums) 4 assert expected=-actual, "Mistake in test case 1" 5 NameError: name 'Solution' is not defined SEARCH STACK OVERFLOW Write analysis of the Memory Complexity and Time Complexity using Asymptotic Notation O. (1 point) Memory Analysis: O - fill your details Time Analysis: O - fill your details
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started