Help with Algorithm Homework: Locating Last Occurrence of Smallest Element

In summary, the conversation discusses the Horner's method for evaluating polynomials and provides pseudocode for its implementation. It then poses a problem of finding the last occurrence of the smallest element in a finite list of integers and asks for an algorithm to solve it. The solution involves iterating through the list and comparing elements until the smallest one is found. The complexity of the algorithm is discussed in terms of the number of comparisons made.
  • #1
hyderman
28
0
hello
any one can help me with this
thanx

The Horner’s method is an algorithm that evaluates polynomials. The following pseudocode shows how to use this method to find the
value of anxn + an-1xn-1 + . . . + a1x + a0 at x = c.
procedure Horner(c, a0, a1, a2, . . . , an : real numbers)
y := an
for i := 1 to n
y := y × c + an-i
end {y = ancn + an-1cn-1 + . . . + a1c + a0}

(a) Describe an algorithm that locates the last occurrence of the smallest element in a finite list of integers, where the integers in the list are not necessarily distinct. [5]
(b) Analyze the complexity of the algorithm you devised in Part (a), measured in terms of the number of comparisons



Homework Equations





The Attempt at a Solution

 
Last edited:
Physics news on Phys.org
  • #2
We will not do your homework for you. Please explain to us your thoughts, and why you're stuck, and we'll try to help you.

- Warren
 
  • #3
Think about how many times you would need to iterate such a list, for the simplest case. Generally they (computer scientists) refer to best, worst and average cases [1].

If you look specifically at the O(1) and O(n) cases, constant and linear respectively. O(1) is a single comparison, O(n) depends on the magnitude (number of elements therein) of the list directly. If you iterate the list once you make n comparisons.
 
  • #4
okey let's try answer part a

procedure LocSmallest(a1, a2, . . . , an : integers) m := a1
location := 1 for i := 2 to n
begin
if ai =< m then
begin

NOW HOW WHAT NEXT
...
...
...

end;

FOR PART B
CAN WE COUNT THE COMPARISON STATEMENT AND WHAT ALGORITHM WE CAN USE ...

PLEASE HELP ME
 
  • #5
hyderman said:
okey let's try answer part a

procedure LocSmallest(a1, a2, . . . , an : integers) m := a1
location := 1 for i := 2 to n
begin
if ai =< m then
begin

NOW HOW WHAT NEXT
What do you think you should do next? What do you WANT m to eventually be? I can assume that a1, 2, ..., an are the original list, but you haven't said what m is. Is it the variable the procedure will return? Okay, if you find a number on the list less than m, what should you do?

...
...
...

end;

FOR PART B
CAN WE COUNT THE COMPARISON STATEMENT AND WHAT ALGORITHM WE CAN USE ...

PLEASE HELP ME
 
  • #6
thanx for replying ... so far i establisshed that minimum=m

i am not sure how to rwrite the next code but i will try

i think
procedure LocSmallest(a1, a2, . . . , an : integers) m := a1
location := 1 for i := 2 to n
begin
if ai =< m then
begin
let ai= m
continue
location=m
end

end;


is the whole procedure right and if not how to fix it... please help
 
  • #7
thanx alot
 

FAQ: Help with Algorithm Homework: Locating Last Occurrence of Smallest Element

How do I locate the last occurrence of the smallest element in an array using an algorithm?

To locate the last occurrence of the smallest element in an array, you can use the following algorithm:

1. Start by initializing two variables, one to store the smallest element and another to store its index.

2. Then, loop through the array and compare each element to the current smallest element. If the element is smaller, update the smallest element variable and its corresponding index.

3. Once the loop is complete, the smallest element and its index will be the last occurrence of the smallest element in the array.

Can I use a built-in function to locate the last occurrence of the smallest element in an array?

Yes, many programming languages have built-in functions that can be used to locate the last occurrence of the smallest element in an array. For example, in Python, you can use the index() function to find the index of the last occurrence of an element in a list.

What is the time complexity of the algorithm for locating the last occurrence of the smallest element?

The time complexity of the algorithm for locating the last occurrence of the smallest element is O(n), where n is the size of the array. This means that the time taken to execute the algorithm increases linearly as the size of the array increases.

Can the algorithm for locating the last occurrence of the smallest element be optimized?

Yes, the algorithm can be optimized by using a different data structure, such as a min-heap, to store the elements of the array. This would reduce the time complexity to O(log n), making the algorithm more efficient for larger arrays.

How can I test the correctness of my algorithm for locating the last occurrence of the smallest element?

You can test the correctness of your algorithm by using different input arrays with known outputs. For example, you can create an array with duplicate elements and check if the algorithm correctly identifies the last occurrence of the smallest element. Additionally, you can also use a debugger or add print statements to track the values of variables and ensure they are correct at each step of the algorithm.

Similar threads

Replies
8
Views
5K
Replies
2
Views
2K
Replies
2
Views
2K
Replies
1
Views
3K
Replies
1
Views
2K
Replies
2
Views
6K
Replies
11
Views
11K
Replies
4
Views
3K
Back
Top