Question

(b) Given a set S of integers, the range of S is defined as the difference between the largest and the smallest integers in S. Write an algorithm to find the range of S and analyze its run-time (use big-O) in terms of IS| for each of the following cases: i. S is a sorted array of integers. ii. S is an un-sorted array of integers.

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

(i)

Function findRange(S)

BEGIN

    // in a sorted set, the last element is the largest element

    largest = S[ S.length() ]

    // in a sorted set, the first element is the smallest element

    largest = S[ 1 ]

    range = largest - smallest

    return range

END

The Time Complexity of algorithm is O(1) as it takes constant time to subtract two variables.

(ii)

Function findRange(S)

BEGIN

    // store the largest element

    largest = -infinity

    // store the smallest element

    largest = +infinity

    for i = 1 to S.length()

    BEGIN

         If S[i] > largest

         BEGIN

        

             largest = S[i]

         END

         If S[i] < smallest

         BEGIN

        

             smallest = S[i]

         END

    END

    range = largest - smallest

    return range

END

The Time Complexity of algorithm is O(n) as we have to traverse the complete array of size n.

Add a comment
Know the answer?
Add Answer to:
(b) Given a set S of integers, the range of S is defined as the difference...
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
  • 4 Let the set of all possible keys considered is the set of all integers from...

    4 Let the set of all possible keys considered is the set of all integers from 0 to 10,000 inclusive. Consider a closed hashing and a hash table of size M 10 and the hash function h(x) xmod 10. Note: Using a prime number as the size of the table is not a good idea. However, we do so to keep the calculations simple. a) Write an algorithm (using any programming language) to find the largest value in this hash...

  • Given two arrays A and B of n integers both of which are sorted in ascending...

    Given two arrays A and B of n integers both of which are sorted in ascending order. Write an algorithm to check whether or not A and B have an element in common. Find the worst case number of array element comparisons done by this algorithm as a function of n and its Big-O complexity

  • Given an unsorted array of distinct positive integers A [ 1......n ] in the range between...

    Given an unsorted array of distinct positive integers A [ 1......n ] in the range between 1 and 10000 and an integer i in the sane range. Here n can be arbitrary large You want to find out whether there are 2 elements of the array that add up to i. Give an algorithm that runs in time (O(n).

  • write a MIPS program that does the following Create an array of 10 INTEGERS.  Create procedures to...

    write a MIPS program that does the following Create an array of 10 INTEGERS.  Create procedures to find the largest, and find the smallest. Create another procedure called range, which is the difference between largest and smallest (range should call findLargest and findSmallest) Convert your find largest procedure to be recursive

  • 1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers,...

    1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers, you want to find out whether there is an index i for which Al = i. Give a divide-and-conquer algorithm that runs in time O(log n). Provide only the main idea and the runtime analysis.

  • Given an array A[1..n] of positive integers and given a number x, find if any two...

    Given an array A[1..n] of positive integers and given a number x, find if any two numbers in this array sum upto x. That is, are there i,j such that A[i]+A[j] = x? Give an O(nlogn) algorithm for this. Suppose now that A was already sorted, can you obtain O(n) algorithm?

  • Hi, I am having trouble with the following question: Given an unsorted array with integers, find...

    Hi, I am having trouble with the following question: Given an unsorted array with integers, find the median of it using the Quick Select method we’ve discussed in the class (Hint: We use the quick select to find the kth smallest element in an unsorted array in O(n) time complexity). Note: A median is the middle number of the array after it is sorted. And in this problem, we return the N/2-th number after sorted if there are even numbers...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

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