Question

In: Computer Science

1.) (a) There exists a very popular sorting algorithm called Timsort, the default sorting algorithm in...

1.)

(a) There exists a very popular sorting algorithm called Timsort, the default sorting algorithm in both Python and Java. This sort is a combination of two different sorting algorithms: Merge sort, and Insertion sort. Recall that the Big-O of Merge sort is O(nlogn) and the Big-O of Insertion sort is O(n 2 ). What advantage would Timsort have to combine the two algorithms if merge-sort has a better Big-O metric?

(b) Consider two algorithms: f(n) and g(n). You run both algorithms with an input n = 10, 000. You find that f(n) takes 10 ms while g(n) takes 1 min to run. Which of these has a better (i.e. smaller) Big-O metric?

2.)You are given 9 identical looking balls and told that one of them is slightly heavier than the others. Your task is to identify the defective ball. All you have is a balanced scale that can tell you which of two balls is heavier.

(a) Show how to identify the heavier ball in just 2 weighings.

(b) Justify why it is not possible to determine the defective ball in fewer than 2 weighings

Solutions

Expert Solution

1)

a) This is because for small values of n insertion sort runs faster than merge sort . Hence insertion sort can be used to Optimize merge sort. Hence, for smaller values of n, Timesort would give better results than merge sort.

b) f(n) has a smaller Big O metric since it takes lesser time than g(n) for the same input size.

2)

a)

Let label the balls as A, B, C, D, E, F, G, H, I.

1. Weigh ABC versus DEF.

Scenario a: If these (1) balance, then we know the oddball is one of G, H, I.

          2. Weigh G versus H.

          Scenario a.i: If these (2) balance, the oddball is I.

          Scenario a.ii: If these (2) do not balance, the heavier one is the oddball.

Scenario b: If these (1) do not balance, then the oddball is on the heavier side. For simplicity, assume the ABC side is heavier, so the oddball is one of A, B, C.

          2. Weigh A versus B.

          Scenario b.i: If these (2) balance, the oddball is C.

          Scenario b.ii: If these (2) do not balance, the heavier one is the oddball.

b) It is impossible to find the heavier ball in 1 attempt because we need 1 attempt to find the subset of balls which contains the heavier ball.


Related Solutions

Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design in place sorting algorithm? How this in place term is related to the time complexity and space complexity of the program?
Recall the definition of stable sorting: a sorting algorithm is said to be stable when elements...
Recall the definition of stable sorting: a sorting algorithm is said to be stable when elements with equal keys (after the sorting is complete) remain in the same order as they were in the input (before the sorting). Answer the following: Is Radix Sort stable, and why? Show an example: start from 10 random integer numbers (of at least 2 digits per integer number) to be sorted, where at least 2 of those 10 elements need to have equal keys;...
Could the sorting algorithm start out as if then else situation?
Could the sorting algorithm start out as if then else situation?
One of the cars sold by Walt's car dealership is a very popular subcompact car called...
One of the cars sold by Walt's car dealership is a very popular subcompact car called the Rhino. The final sale price of the basic model of this car varies from customer to customer depending on the negotiating skills and persistence of the customer. Assume that these sale prices of this car are normally distributed with a mean of $19900 and a standard deviation of $360. Round your answers to 2 decimal places. a. Dolores paid $19525 for her Rhino....
You are going to apply a very popular investment strategy with options called a straddle. Basically,...
You are going to apply a very popular investment strategy with options called a straddle. Basically, you buy a call option and a put option for the same stock and the same maturity that are as close as possible to being at the money (stock and strike price very close to each other). First, use the information given in the spreadsheet to create the payoffs in dollars. The payoff diagram will fill in as you put in payoffs. Second, go...
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting...
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting .Arrival time, burst time and priority values.The code should also display: (i) Gantt chart and determine the following: (ii) Determine the Turnaround time(TAT), waiting time(WT) of each process (iii) Determine the Average Waiting Time (AWT) and Average Turnaround Time (ATAT) of all processes. please write the comments
Is quicksort a stable sorting algorithm? If yes, prove it, if not, give an example
Is quicksort a stable sorting algorithm? If yes, prove it, if not, give an example
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the...
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the array to be sorted; i is the start index and j is the end index. n = j – i + 1 If (n < 18) { sort A[i…j] by insertion-sort return } m1 = i + 2 * n / 3 m2 = i + n / 3 aSort(A, i, m1) aSort(A, m2, j) aSort(A, i, m1) questions: 1) Please state the asymptotic...
1. Which sorting algorithm can be improved with the following simple optimization? During each pass, you...
1. Which sorting algorithm can be improved with the following simple optimization? During each pass, you keep track of whether a swap was made. At the end of a pass, if no swaps were made, you can assume the list is sorted. a. Insertion sort b. Selection sort c. Bubble Sort d. None of the above 2. If you want to use Java's built-in Collections.sort() method to sort an ArrayList, what do you have to ensure about the type of...
give an algorithm where given as input an array A[1...n] withthe following property. There exists...
give an algorithm where given as input an array A[1...n] with the following property. There exists an index I such that if we append A[1...i−1] after A[i...n], we get an array in sorted increasing order. For simplicity you can assume that n is a power of 2.Give an efficient algorithm that returns the smallest element in A. Analyze the time complexity of your algorithm.Hint: you may want to compare A[1] and A[n/2].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT