2. You can actually find the median by running a sorting algorithm and stopping early, as soon as you know the median.
(a) Assume you use Bubble Sort to find the median of 13 elements (i.e. n = 13), but stop as soon as you know the median. Exactly how many comparisons do you use (in the worst case)?
(b) Assume you use Mergesort to find the median of 13 elements (i.e. n = 13), but stop as soon as you know the median. Exactly how many comparisons do you use (in the worst case)?
For Bubble sort, Worst case occurs when array is reverse sorted.
In bubble sort , in every pass one element is put to the correct place i.e., from the last. ( Observe j<n-i-1 in the below code)
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
Let us consider an example where we have to bubble sort the elements:
13,12,11,10,9,8,7,6,5,4,3,2,1
In every pass, we have one
element placed at the right place. Since we have to find median
which is [ (n/2)+1]
i.e., [(13/2)+1)] = 6+1 = 7.
So after 7 passes we will have the median placed at the middle position.
In every pass, we have ( n - i ) comparisons. The example we discussed here is the worst possible case, hence to find the median of 13 elements using bubble sort, we require
12+11+10+9+8+7+6 = 63 comparisons.
In merge sort, the approach is divide and conquer. Hence, the complete solution is yielded at last only.
Merge sort does n-1 comparisons for any given problem while merging any two arrays.
Hence, total number of comparisons = ( 5 x 1 ) + ( 2 x 3 ) + 3 + 5 + 6 + 12 = 37.
Merge sort does 37 comparisons when n=13 and median can be find after this many comparisons.
2. You can actually find the median by running a sorting algorithm and stopping early, as soon as...
C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential search of a list assumes that the list elements are sorted in ascending order. b. A binary search of a list assumes that the list is sorted. 2. Consider the following list: 63 45 32 98 46 57 28 100 Using a sequential search, how many comparisons are required to determine whether the following items are in the list or not? (Recall that comparisons...
2. Suggest a structured plan (algorithm) for the bubble sort and selection sort, and perform running time analysis for both best and worst case. 3. Consider the age data of 12 children who are supposed to undergo for vaccination in ascending order of their age. Suggest and apply a sorting technique which can efficiently handle this data. Show all the intermediate steps clearly. Child ID 01 02 03 04 05 06 07 08 09 10 11 12 2. Age 1...
4. [30 pts:10+9+11] a) You are given traffic data (number of page hits) for n websites for some time period. (n is a large number, in the order of hundreds of thousands.) You are asked to print the top k websites with greatest amount of traffic (k is much smaller than n). Which of insertion sort, mergesort quicksort, or heapsort may be used to devise the fastest (in worst case) algorithm for this task? What would be its worst case...
Solve ques no. 2 a, b, c, d .
Algorithm 1 Sort a list al,..., an for i=1 to n-1 do for j=1 to n-i do if aj > aj+1 then interchange a; and a;+1 end if end for end for (b) Algorithm 1 describes a sorting algorithm called bubble sort for a list al,...,an of at least two numbers. Prove that the algorithm is complete, correct and terminates. (2) Complexity of Algorithms (Learning Target C2) (a) What is the...
I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...
Data Structures: For each of the following situations, name the best sorting algorithm we studied. (For one or two questions, there may be more than one answer deserving full credit, but you only need to give one answer for each.) (a) The array is mostly sorted already (a few elements are in the wrong place). (b) You need an O(n log n) sort even in the worst case and you cannot use any extra space except for a few local...
. Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply that to the following array. Show your work in Detail. [15 points] 45 20 50 10 80 30 60 70 40 90 2. Is Shell sort a stable sorting algorithm? Answer this with an example. [10 points] 3. Apply Merge Sort to sort the following list. Show your work in Detail. [15 Points] 45 20 50 10 80 30 60 70 40 90 4....
Consider a MergeSort-like algorithm in which the array is split into thirds, rather than halves. (a) Write pseudo-code for your algorithm. You may assume a three-way merge algorithm is available. Your algorithm should work for general n, i.e., even if the size of the array is not a power of 3. (b) Use the tree method to analyze exactly how many comparisons your algorithm uses. You may assume that the size of the array is a power of 3. Assume...
Subject: Algorithm.
solve only part 3 and 4 please.
2.2 Selection- 5 points each 1. Run the simultaneous min-and-max algorithm on the array A 4, 2, 12, 6, 13,9,15). (16, 7, 10, 1,5, 11,3,8, 14, 2. Explain why the above algorithm is better than the naive algorithm for finding minimum and maximum separately. How many comparisons does the naive algorithm do? How many comparisons does the simultaneous min and max do? 3. Use the randomized select algorithm based on partition...
4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...