Question

Calculate the space required by the below programs: a- n = len(S) # S has 15...

Calculate the space required by the below programs:

a-

n = len(S) # S has 15 numbers

j = 0

while j < n:

if S[j] == val:

return j # a match was found at index j

j += 1

b-

n = len(S)

total = 0

for j in range(n): # loop from 0 to n-1

for k in range(1+j): # loop from 0 to j

total += S[k]

return total

c-

data=[15,14,8,12,4]

big = data[0]

for val in data:

if val > big

big = val

return big

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

SPACE COMPLEXITY

The space that is required to process an algorithm for an input N

(OR)

The magnitude of auxiliary space your program takes to process the input N.

CALCULATION OF SPACE

Declaring an array of size n would add to the space complexity by a factor of O(n), a 2D array would add to the space complexity by a factor of O(n^2)

Variables add a space complexity of O(1) since the size of a particular datatype is always constant.

Note: User defined datatypes varies in size.

A)

n = len(S) # S has 15 numbers

j = 0

while j < n:

if S[j] == val:

return j # a match was found at index j

j += 1

Space Required for this is Algorithm is O(n) where n is size of array S

Time required is O(n) of worst case (val is not found in array S)

B)

n = len(S)

total = 0

for j in range(n): # loop from 0 to n-1

for k in range(1+j): # loop from 0 to j

total += S[k]

return total

Space Required for this is Algorithm is O(n) where n is size of array S

S is the only array that requires space

total is variable of constant size

Generally O(n) + 1(total)

we omit constants so O(n) is space complexity

Time complexity is n*n ->O(n2 ) because there is nested loop(2)

C)

data=[15,14,8,12,4]

big = data[0]

for val in data:

if val > big

big = val

return big

Space Required for this is Algorithm is O(n) where n is size of array data

big is the variable which has constant space.

generally O(n)+1(big)

we omit constants so O(n) is the space required

Time complexity is O(n) where n is no of elements we traverse

It is said to be worst case because we traverse each and every element in the array to compare with element big.

Thank You

please give a thumbs up if it is helpful....

Add a comment
Know the answer?
Add Answer to:
Calculate the space required by the below programs: a- n = len(S) # S has 15...
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
  • Let T(n) be the running time of function foo. Find the equation of T(n) and find...

    Let T(n) be the running time of function foo. Find the equation of T(n) and find the complexity of T(n) using big-O notation. def foo(L): s = 0 for x in L: j = len(L) while j > 1: j = j / 2 s = s + x print(s) return s

  • 8.         R-4.8 Order the following functions by asymptotic growth rate. 4nlogn + 2n 2^10    2^logn 3n...

    8.         R-4.8 Order the following functions by asymptotic growth rate. 4nlogn + 2n 2^10    2^logn 3n + 100logn      4n     2^n n^2 + 10n        n^3       nlogn 9.         R-4.9 Give a big-Oh characterization, in terms of n, of the running time of the example 1 method shown in Code Fragment 4.12. 10.       R-4.10 Give a big-Oh characterization, in terms of n, of the running time of the example 2 method shown in Code Fragment 4.12. 11.       R-4.11 Give a big-Oh characterization, in...

  • Please, I need help debuuging my code. Sample output is below MINN = 1 MAXX =...

    Please, I need help debuuging my code. Sample output is below MINN = 1 MAXX = 100 #gets an integer def getValidInt(MINN, MAXX):     message = "Enter an integer between" + str(MINN) + "and" + str(MAXX)+ "(inclusive): "#message to ask the user     num = int(input(message))     while num < MINN or num > MAXX:         print("Invalid choice!")         num = int(input(message))     #return result     return num #counts dupes def twoInARow(numbers):     ans = "No duplicates next to each...

  • Python3 Modify the original Bubble Sort code shown below so that it will print out the...

    Python3 Modify the original Bubble Sort code shown below so that it will print out the length of the list, total number of comparisons, and total number of swaps needed to sort the list given. def compare(data, a, b): """Returns True if element at index a > element at index b""" return data[a] > data[b] def swap(data, a, b): """Swaps the element at index a with element at index b""" data[a], data[b] = data[b], data[a] def bubble_sort( data ): """Sorts...

  • Prove Big O in terms of nₒ and C? There are 5 examples: class Exercise {...

    Prove Big O in terms of nₒ and C? There are 5 examples: class Exercise { public static int example1(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j++) // loop from 0 to n-1 total += arr[j]; return total; } public static int example2(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j += 2) // note the increment of 2 total += arr[j]; return...

  • Suppose you have an array S indexed from 1 to n which contains n numbers, not...

    Suppose you have an array S indexed from 1 to n which contains n numbers, not in any particular order, and you wish to count how many times a given number x occurs in S. Consider the recursive algorithm below for this which finds the number of occurrences of x in the index range i...j in S. Of course, solving the problem would involve an initial call to the algorithm for the range 1.n: int CountOccur (int i,j) { int...

  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • Python Merge Sort Adjust the following code so that you can create random lists of numbers of lengths 10, 15, and 20. You will run the merge sort 10 times for each length. Record the run time for the...

    Python Merge Sort Adjust the following code so that you can create random lists of numbers of lengths 10, 15, and 20. You will run the merge sort 10 times for each length. Record the run time for the length and then calculate the average time to sort. Finally, compare the average run time for each length to the average time for the Merge Sort. -------------------------------------------- Initial python code: import random import time def mergeSort(alist): print("Splitting ",alist) if len(alist)>1: mid...

  • Poly-Poly + a[i] * power 6. (15%) Consider the nested loops shown below, where N is...

    Poly-Poly + a[i] * power 6. (15%) Consider the nested loops shown below, where N is assumed to be a power of 2. i.e., N-2 for some positive integer k. i=N; while (i>-1){ while (j =N){ j-2 *j; i=i/2. a) Determine the number of outer iterations (associated with the outer while loop) to be executed? Show your work. b) For each outer iteration (for each value of i), determine the number of iterations of the inner while loop to be...

  • I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ________...

    I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...

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
ADVERTISEMENT