Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Lecture 1 (Assignment)

Theory:
1. Suppose that for inputs of size 𝒏 on a particular computer, insertion sort runs in 𝟖 𝒏𝟐 steps
and merge sort runs in 𝟔𝟒 𝒏 𝐥𝐠 𝒏 steps. For which values of 𝒏 does insertion sort run faster
than merge sort? (1 mark)

2. Rewrite INSERTION-SORT (slide 16) to sort into decreasing instead of increasing order.
(1 mark)

3. For each function 𝒇(𝒏) and time 𝒕 in the following table, determine the largest size 𝒏 of a
problem that can be solved in time 𝒕, assuming that the algorithm to solve the problem
takes 𝒇(𝒏) microseconds. (2 marks)

𝒕 1 1 1 1 1 1 1
𝒇(𝒏) second minute hour week month year century
𝟔
𝐥𝐠 𝒏 𝟐𝟏𝟎
√𝑛
𝑛
𝑛 lg 𝑛
𝑛#
𝑛$
2%
𝑛!

For example: to solve the first one, the algorithm needs 𝐥𝐠 𝒏 microseconds, so in 1 second (106
𝟔
microseconds), 𝒏 can be found by solving this equation 𝐥𝐠 𝒏 = 𝟏𝟎𝟔 è 𝒏 = 𝟐𝟏𝟎 .

4. Illustrate the operation of INSERTION-SORT on an array initially containing the sequence


〈23, 44, 56, 18, 22, 37〉. (1 mark)

Practice:
5. Write a Java program and perform the following: (2 marks)
• Write the method int max(int[] arr) that returns the maximum number in an array
• Write the method int[] generateArray(int n) that generate an array of random
integers of size n and returns it.
• Create an array arr of size 1 million of randomly generated integers.
• Call the function max and compute the execution time that it takes to find the maximum
value in arr using max.
• Print the maximum value and the execution time.

You might also like