EC330 Applied Algorithms and Data Structures For Engineers Fall 2018 Homework 4
EC330 Applied Algorithms and Data Structures For Engineers Fall 2018 Homework 4
Fall 2018
Homework 4
This homework has a written part and a programming part. Both are due at 8:59 am on
October 17. You should submit both parts on Blackboard. For the written part, you
should submit a single PDF file containing either typeset answers or scanned copies of
hand-written answers. Make sure you write your answers clearly. For the programming
part, your code should be easy to read and understand, and demonstrate good code
design and style. Your program must compile and run on the lab computers.
a) sortA(Array A[0..n-1])
for i = 0 to n/2
for j = n/2+1 to n-1
if (A[i] <= A[j])
swap(A[i], A[j])
return A;
1
a) [40 pt] In this problem, you are tasked with sorting a string in increasing order
based on the number of occurrences of characters. If there is a tie, output them
based on alphabetical order, e.g., ‘a’ before ‘e’. You can assume that all the
characters are lower-case letters (so a total of 26 possible types of characters).
Below are some example inputs and the corresponding expected outputs.
Input1: “engineers”
Output2: “girsnneee”
Input2: “engineering”
Output2: “rggiieeennn”
Implement sortByFreq in sort.h. You are allowed to use only the libraries
included in sort.h (you may not need to use all of them). You are welcome to
implement your own data structure or use built-in ones like int[]. You cannot use
any of the built-in sort functions including those provided by the <algorithm>
library and will need to implement your own (you should write this as a separate
function and call it from sortByFreq).
In the written part of your submission, state and justify the time and space
complexity of your algorithm (in terms of n which is the length of the input
string).
Your code will be graded not only on correctness but also on efficiency and
memory footprint. For efficiency and memory consideration, you should expect
long string inputs (i.e. large n). You can benchmark your implementation against
our sample solution sort2a.o. To link the object file, you will need to modify
sort.h so that it contains only the function declaration of sortByFreq.
b) [40 pt] Recall in an earlier lecture, I claimed that we could have a divide-and-
conquer algorithm that is faster than 𝑂(𝑛! ) for finding the closest pair of
points on a 2D plane. We will learn to implement this algorithm in this
homework.
2
For simplicity, let us assume that n is a power of two, and all the x and
y coordinates are distinct.
In the written part of your submission, write down and solve the recurrence
relation for this algorithm.
Bonus [10 pt]: Improve your code to bring the running time down to 𝑂(𝑛 log 𝑛).
Implement it in a new function called findClosestPairOptimal (you can reuse your
utility functions for the original code as much as possible). Justify your
improvement in the written part of your submission. Hint: Recall the recurrence
relation I gave in lecture for this problem.