Question

Write an algorithm that finds the m smallest numbers in a list of n numbers. Algorithms...

Write an algorithm that finds the m smallest numbers in a list of n numbers.

Algorithms and analysis, thanks

Homework Answers

Answer #1
  1. Take an array, SMALLESTS of m numbers initialized to the first element in the array A which is of n numbers.
  2. (Outer) Loop through each element, x in the array A
  3. (Inner) Loop through each element y in the SMALLESTS
    • If x < y then shift elements in the SMALLESTS right by 1 position from y number only and put x in the place of y and break the inner loop

Once it is done, array SMALLESTS will have m smallest numbers

--------------------------------------
Hit the thumbs up if you are fine with the answer. Happy Learning!

Know the answer?
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
Similar Questions
Devise an algorithm that takes a list of n > 1 integers and finds the largest...
Devise an algorithm that takes a list of n > 1 integers and finds the largest and smallest values in the list.
Pseudocode an algorithm that takes as input a list of n integers and finds the number...
Pseudocode an algorithm that takes as input a list of n integers and finds the number of even integers in the list.
Describe an algorithm that takes as input a list of n integers and finds the location...
Describe an algorithm that takes as input a list of n integers and finds the location of the first even integer or returns -1 if there is no even integer in the list. Here is the operation's header: procedure locationOfFirstEven(a1, a2, a3, ..., an : integers)
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in...
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in the range from start_num to end_num (inclusive). For example, start_num = 5 and end_num = 12, the prime numbers in that range are: [5, 7, 11]. The smallest gap between any two prime numbers in this list is 2, so the function would return 2. Some example test cases (include these test cases in your program): >>> print(smallest_gap(5, 12)) 2 # The primes between...
In this programming exercise you will create an algorithm for solving the following version of the...
In this programming exercise you will create an algorithm for solving the following version of the m Smallest Numbers problem.   Instead of just returning the m smallest values as in homework 1, you want to return a list of the positions where the m smallest values are located without changing the original array. Your algorithm should meet the following specifications: mSmallest( L[1..n], m ) Pre: L is a list of distinct integer values. n is the number of elements in...
3) Write an algorithm to take a list of numbers from user and determine the numbers...
3) Write an algorithm to take a list of numbers from user and determine the numbers divisible by the number 7. You need to first prompt the user that how many numbers s/he wants to input and then take the numbers. After taking the numbers, you check which numbers are divisible by 7 from the list. You need to print the number along with the position of the number. Finally, you need to print how many numbers from the list...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 You can assume that the list of integers will have at least 2 values. To achieve the above, first read the integers...
Describe an algorithm that, given a list of n numbers, decides in linear time whether the...
Describe an algorithm that, given a list of n numbers, decides in linear time whether the list contains at least ⌈n/2⌉ numbers, all with equal value. What if we want to know if there are at least ⌈ n/100 ⌉ numbers with equal value? Hint: suppose some number, x, occurs ⌈n/2⌉ times. Where could all of these copies end up if we sorted? (This is not a suggestion we should actually sort). Wherever they end up, find one invariant.
1). Describe an algorithm that takes a list of n integers a1,a2,...,an and find the average...
1). Describe an algorithm that takes a list of n integers a1,a2,...,an and find the average of the largest and smallest integers in the list.
Write a Scheme function that takes a simple list of numbers as a parameter and returns...
Write a Scheme function that takes a simple list of numbers as a parameter and returns a list with the largest and smallest numbers in the input list.