Overview:
Some of the most important algorithms used in computing are in the
category "search and sort." These are useful in a variety of settings, such
as web search, alphabetic sorting of a list of names, etc. They are also
related, since the most efficient search algorithms occur on lists that are
sorted. This lab exercises algorithms for searching and sorting, and their
efficiency.
The (suggested) reading for this lab is Chapter 3 in the SG textbook.
BinarySearch
get n, a1, a2, ...an, t
start = 1
end = n
found = "false"
repeat until found="true" or ...
m = ceiling( (start + end)/2) //the middle between start and end
if (t = am) then
print "Target found at position " m
found = "true"
if (t < am) then
end = m-1
if (t > am) then
start = m+1
if (found="false") then
print "Target not in the list"
Using this algorithm, search for target t=3 in the list
2,4,5,7,9. At some point the values of the variables
start and end will be equal. What happens if we let the
loop execute when start=end? What will the values of
start and end be after this last interation? With this
insight, how would you write the condition to stop the repeat loop (so that
it executes one last time start=end)?
3, 6, 7, 9, 12, 14, 18, 21, 22, 31, 43What numbers are compared to 35?
Arturo, Elsa, JoAnn John, Jose, Lee, Snyder, Tracy
As part of your algorithm you will need to compute the product xn (you cannot assume that you can use xn in pseudocode).
Analyze the worst-case and best-case running time of your algorithm (the order of magnitude, or theta-notation is enough).
SelectionSort
get n, a1,...an
set unsortedEnd = n
repeat until (unsortedEnd = 1)
find the position of the largest element among a1, a2,..,aunsortedEnd
assign this position to pl
swap apl with aunsortedEnd
unsortedEnd = unsortedEnd - 1
print "The list in sorted order is" a1, a2,..,an
6 1 5 3 2 6your algorithm should print
The missing number is 4
Analyze the worst-case and best-case running time of your algorithm (the order of magnitude, or theta-notation is enough).
4 3 1 2 5 7 6the median is 4. For the list
5 4 3 8 6 7 1 2the medians are 4 and 5.
You can use any of the algorithms studied in class as a black box, without writing the code for them.
Analyze the worst-case and best-case running time of your algorithm (the order of magnitude, or theta-notation is enough).
Be sure to justify your answers! You may do this work either with a word processor or by hand. If you type your solutions, bring a hard copy of your assignment and hand it in class. If you write by hand, do your best to write legibly and leave space between problems.
You may choose to do this assignment either by yourself or in a group. However, solutions should be written up individually and handed in on the due date, at the beginning of class.
Once you are finished in the lab, if you want to save any documents, you'll have to drag them to your network folder (mounted to your desktop). Files left on the desktop and in your local folder (on the local machine) will be erased!