Question

1. [10 marks] Consider the function SumKSmallest(A[0..n – 1), k) that returns the sum of the k smallest elements in an unsortPlease answer this in python pseudocode. It's an algorithm question.

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

Note: You have given in question as "Please answer this in python pseudocode. It's an algorithm question." I haven't understood this sentence. So I hereby just providing the algorithm and pseudocode for every approach. If you want python code also, please mention in comments. Thank you.

(1): Brute force method

Algorithm:

  1. Initialise sum to 0
  2. Iterate the following steps for k times
  3. Get the minimum value of array and store it in minValue variable and store the minimun value index in minIndex variable
  4. Add the minimum value to sum
  5. Update the minimum value index with some maximum value
  6. Atlast after completion of K times iteration return sum.

SumKSmallest(A[],k):

  • sum=0
  • for i=1 to k do
    • minValue=A[0]
    • minIndex=0
    • for j=0 to n-1 do
      • if A[i]<minValue:
        • minValue=A[i]
        • minIndex=i
    • sum=sum+minValue
    • A[minIndex]=9999999
  • return sum

Time Complexity of above algorithm: O(k*n)

(2): Transform and Conquer

Algorithm:

  1. Initially sort the array
  2. initialise sum to 0
  3. Then add first k elements and store it in sum variable and return it

SumKSmallest(A[],k):

  • sort(A), inbuilt method that sorts the array
  • sum=0
  • for i=0 to k do
    • sum=sum+A[i]
  • return sum

Time complexity of above algorithm: O(nlogn)

(3): Efficient Approach using Max heap approach

Algorithm:

  1. In this algorithm, we make use of max heap data structure, where max heap is a partially complete binary tree whose parent value is greater than children values.
  2. So initialise a max heap.
  3. Iterate from i=0 to n-1 and add every element of array into max heap. While inserting, if max heap size is greater than k, then remove root node of max heap.
  4. Continue step-3 till all the values in array are read.
  5. Now, we have K smallest elements in max heap.
  6. So add all elements of max heap and return its sum.

SumKSmallest(A[],k):

  • Initialise a max heap let it be max_heap
  • for i=0 to n-1 do
    • max_heap.add(A[i])
    • if max_heap.size()>k:
      • max_heap.remove()
  • sum=0
  • while(max_heap.isEmpty()):
    • sum=sum+max_heap.remove()
  • return sum

Time complexity of above algorithm : O(nlogk)

Because at any time height of max heap is logk because, we will have at max of k elements in max heap. In first loop, we iterate from i=0 to n-1 which means "n", and for each insertiion and deleteion it takes logk time. So final time complexity is O(nlogk).

Mention in comments if any mistakes or errors are found. Thank you.

Add a comment
Know the answer?
Add Answer to:
Please answer this in python pseudocode. It's an algorithm question. 1. [10 marks] Consider the function...
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
  • Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a...

    Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a and b are integers while (a>0) B- a/2 A a-2 end while return b; i. What is the time complexity of the IterativeFunction pseudocode shown above? ii. What is the space complexity of the IterativeFunction pseudocode shown above? 2. What is the time complexity of the following algorithm (Note that n(n+1) 2,2 n(n+1)(2n+1) 2 and ): Provide both T(n) and order, e(f(n)). int A=0;...

  • Consider the following pseudocode: Algorithm RecursiveFunction (a, b) // a and b are integers if (as1...

    Consider the following pseudocode: Algorithm RecursiveFunction (a, b) // a and b are integers if (as1 ) return b; else return RecursiveFunction (a-2, a/2); endif a. What is the time complexity of the RecursiveFunction pseudocode shown above? b What is the space complexity of the RecursiveFunction pseudocode shown above? n(n+1) C. What is the time complexity of the following algorithm (Note that 21-, i = n(n+1)(2n+1). and Σ.,1 ): Provide both T(n) and order, Ofn)). int A=0; for (int i=0;i<n;i++)...

  • Consider the function FINDDUPLICATES(A[0. 1) that returns a list of all duplicate items in the unsorted integer array A...

    Consider the function FINDDUPLICATES(A[0. 1) that returns a list of all duplicate items in the unsorted integer array A of size n. For example, given the array |3, 2, 4, 3], the function should return the value 3. For the array 11 2,3 5,5,5,66,81, the function should return an array (or list) 5,6 In this task, you will develop two alternative solutions for the FINDDUPLICATES(Ao..n 1 func- tion. In your implementations, you may call any of the algorithms introduced in...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • Subject: Algorithm need this urgent please thank you. 4. Give pseudocode for an algorithm that will solve the following problem. Given an array A[1..n) that contains every number between 1 and n +...

    Subject: Algorithm need this urgent please thank you. 4. Give pseudocode for an algorithm that will solve the following problem. Given an array A[1..n) that contains every number between 1 and n +1 in order, except that one of the numbers is missing. Find the miss sorted ing mber. Your algorithm should run in time (log n). (Hint: Modify Binary search). A pseudocode means an algorithm with if statements and loops, etc. Don't just write a paragraph. Also, if your...

  • a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n...

    a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n positive integers and determines whether there are two distinct elements of the list that have as their sum a third element of the list. That is, whether there exists i, j.k such that iヂj, i关k,j关k and ai + aj = ak. The algorithm should loop through all triples of elements of the list checking whether the sum of the first two is the third...

  • 1. [5 marks Show the following hold using the definition of Big Oh: a) 2 mark...

    1. [5 marks Show the following hold using the definition of Big Oh: a) 2 mark 1729 is O(1) b) 3 marks 2n2-4n -3 is O(n2) 2. [3 marks] Using the definition of Big-Oh, prove that 2n2(n 1) is not O(n2) 3. 6 marks Let f(n),g(n), h(n) be complexity functions. Using the definition of Big-Oh, prove the following two claims a) 3 marks Let k be a positive real constant and f(n) is O(g(n)), then k f(n) is O(g(n)) b)...

  • Q1. [10 pts] Write an algorithm for Bubble sort that sorts array of n integers. Indicate...

    Q1. [10 pts] Write an algorithm for Bubble sort that sorts array of n integers. Indicate the expected time complexity of the algorithm using the big-O notation. Use the following format for an algorithm pseudocode Function header (.....) Input: Output: Algorithm steps: 1. 2.

  • Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for...

    Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for “travel arrangement”. For example, at a high level of abstraction, the algorithm for “travel arrangement” is: book a hotel buy a plane ticket rent a car Using the principle of stepwise refinement, write more detailed pseudocode for each of these three steps at a lower level of abstraction. Exercise 2 Asymptotic Complexity (3 pts) Determine the Big-O notation for the following growth functions: 1....

  • Write a pseudocode description of the printLCS () algorithm, which prints the longest common subs...

    Write a pseudocode description of the printLCS () algorithm, which prints the longest common subsequence of two strings x and y. Your algorithm should take as input the completed ïïcs Π integer array of longest common subsequence lengths, and the two strings x and y. (So, you do not have the path[] [] array - see Lecture 19, slides 100 and 101.) Your algorithm must return the specific string corresponding the length found in 1lcs [n] [m] and it should...

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