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?
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
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...
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...
How can I determine or explain how BingoSort is a stable sorting algorithm or not?
The following submission rules apply: • For those questions requiring programs, the solutions must be implemented using JavaScript or Java. o Appropriate self-documenting comments in the source code are mandatory, consistent with good programming practices. o Solutions must be provided in plain text so that formatting is not lost. • All answers must be provided in this document. • Sources must be given accurate and complete citations sufficient for the instructor to find and confirm them.How can I determine or...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT