15 122 hw2
15 122 hw2
15 122 hw2
Fall 2010
Assignment 2: Searching, Sorting, and Strings
1.1 Warmup
Exercise 1 (6 pts). Consider the following implementation of the linear search algo-
rithm that finds the last occurrence of x in array A:
(a) Add loop invariants to the code and show that the loop invariants hold for this
loop. (Hint: Recall the linear search example from Lecture 3.)
1
(b) Add one or more ensures clause(s) to describe the intended postcondition in a
precise manner.
(c) Suppose you are looking for the integer 1,000,000 (1 million) in the array. Does
it pay to look for it by scanning from the end of the array as we do above rather
than from the beginning of the array as we did in lecture? Why or why not?
Exercise 2 (2 pts). Let x be an int in the C0 language. Express the following operations
in C0 using only one statement each. (Do not use an if statement here.) You should
think about using some of the bitwise operators: (&, |, ˆ, ˜, <<, >>).
(a) Rotate x left one bit. (The leftmost bit reenters x in the rightmost position.) Store
the result back in x.
(b) Rotate x right one bit. (The rightmost bit reenters x in the leftmost position.)
Store the result back in x.
Exercise 3 (4 pts). An array can have duplicate values. A programmer wrote the
following variant of binary search to find the first occurrence of x in a sorted array A
of n integers so that the asymptotic complexity is still O(log n):
There is a bug in this implementation. Describe the bug and fix the code (and the
annotations if necessary) so that it works correctly.
2
1.2 Run-time Complexity
Exercise 4 (4 pts). Consider the following function that sorts the integers in an array.
(You may assume the code is correct so most annotations are not shown for now.)
(a) Let T(n) be the number of comparisons between array elements as a function of
n. What is T(n) in the worst cast? Justify your answer.
(b) Using big-O notation, we can say that this function has a worst-case running time
complexity of O( f (n)). What is f (n) in this case? Use the simplest form of f (n)
that is possible to describe the run-time complexity of the function.
(c) Using your answers from the previous two parts, show that T(n) ∈ O( f (n)) using
the formal definition of big O(−). (That is, find a c > 0 and n0 ≥ 0 such that for
every n ≥ n0 , T(n) ≤ c f (n).)
Exercise 5 (4 pts). For each of the following problems, a programmer has a function
that processes an array with n integers in it. The programmer wrote down the
value of n along with the execution time for each algorithm. Using this information,
determine the running time complexity of the algorithm as a function of n using
big-O notation in its simplest form and briefly explain how you found each answer.
3
(b) n Execution Time (in seconds)
1000 0.584
2000 1.190
4000 2.367
8000 4.801
4
2 Programming: String Processing (30 points)
For the programming portion of this week’s homework, you’ll write two C0 files
corresponding to two different string processing tasks: duplicates.c0 (described in
Section 2.2) and common.c0 (described in Section 2.3).
You should submit your code electronically by 11:59 pm on the due date. Detailed
submission instructions can be found below.
Starter code. Download the file hw2-starter.zip from the course website. When
you unzip it, you will find two C0 files, stringsearch.c0 and readfile.c0. The
first contains linear and binary search as developed in class, adapted to work over
string arrays. The second contains functionality for reading a text file into an array
of strings with its length, a type called string bundle.
You need not understand anything about this type other than that you can extract its
underlying string array and the length of that array:
You can assume that all the strings returned have been converted to lowercase. You
will also see a texts/ directory with some sample text files you may use to test your
code.
For this homework, you are not provided any main() functions. Instead, you
should write your own main() functions for testing your code. You should put this
test code in separate files from the ones you will submit for the problems below, but
you may additionally submit your test code for extra credit—use any file names you
like, as long as they are distinct from the ones we require you to submit.
You should not modify or submit the starter code.
Compiling and running. For this homework, we are not providing you with a
script to compile your code. Instead, you will use the standard cc0 command with
command line options. For this assignment, you will need to use libraries for file I/O
(file), console I/O (conio), and strings (string). To compile one of your files against
one of our main() functions, just specify both files on the command line along with
library switches -l<lib> (dash-lowercase-L) for each library <lib>. For example, if
you’ve completed the program common and you’ve implemented some test code in
common-test.c0, you might compile with a command like the following:
Don’t forget to include the -d switch if you’d like to enable dynamic annotation
checking.
5
Submitting. Once you’ve completed some files, you can submit them by running
the command
handin -a hw2 <file1>.c0 ... <fileN>.c0
The handin utility accepts a number of other switches you may find useful as well;
try handin -h for more information.
You can submit files as many times as you like and in any order. When we grade
your assignment, we will consider the most recent version of each file submitted
before the due date. If you get any errors while trying to submit your code, you
should contact the course staff immediately.
Style. Strive to write code with good style: indent every line of a block to the same
level, use descriptive variable names, keep lines to 80 characters or fewer, document
your code with comments, etc. We will read your code when we grade it, and
good style is sure to earn our good graces. Feel free to ask on the course bboard
(academic.cs.15-122) if you’re unsure of what constitutes good style.
6
// Returns the substring composed of the characters of s beginning
// at index given by start and up to but not including the index
// given by end>
// If end <= start, the empty string is returned
// If end < 0 or end > the length of the string, it is treated as
// though it were equal to the length of the string.
// If start < 0 the empty string is returned.
string string_sub(string a, int start, int end);
where n represents the number of strings in the array A. This function should return
true if the given string array contains no repeated strings and false otherwise.
7
Figure 1: The ASCII table (from http://ascii-table.com/img/table.gif)
8
You must include annotations for the precondition(s), postcondition(s) and loop
invariant(s) for each function. You may include additional annotations for assertions
as necessary. You may include any auxiliary functions you need in the same file, but
you should not include a main() function.
9
Extra Credit Task 1. Implement a subquadratic sort—possibly the one seen in
lecture—and use it to test that your common1 and common2 functions compute the
same answer for the Shakespeare/Scrabble task above. Submit the test code in a file
common compare.c0.
You must include annotations for the precondition(s), postcondition(s) and loop
invariant(s) for each function. You may include additional annotations for assertions
as necessary. You may include any auxiliary functions you need in the same file, but
you should not include a main() function.
(a) How long did it take you to complete the written portion of this homework?
(b) How long did it take you to complete the programming portion of this home-
work?
Submit this file along with the rest of your code. If you wish, you may also include
any additional explanations of your testing code in the README file.
10