Wednesday, June 19, 2019

Algoeithm Design, Analysis and Implementation Assignment

Algoeithm Design, Analysis and Implementation - Assignment ExampleThis is done by choosing a comparing subdivision and placing all the elements that ar less than the comparison element in the first group and the rest of the elements in the second group. This procedure is repeated recursively until the elements are sorted (a part consist of only one element). T(n) = (n-1) + ?1 ? i ? n ti As 1,2,....k-elements are already sorted, we can say that ti =0, where i = 1,2, 3... k. Then, the contribution of dissolute sort when early stopping is used can be given by, T(n)=(n+1)( ?k ? i ? n ti + ?(1)) = (n+1)( n lg +?(1)) =2n lg +?(n) Thus, T(n) for quick sort =O(nlg(n/k)). Given that, founding sort is done on a partially sorted soldiery (unsorted k-elements). In general, running time of insertion sort is O(n2 ), where n is the length of the set off (total number of elements). In order to provide a solution to this problem, the total array is divided into subarrays of k-elements each, su ch that k/2? n ? k, accordingly n = O(k) and the running time of insertion sort is O(k2). The total number of such subarrays (m) would then be n/k ? m ? 2n/k., which implies m = O(n/k). The total time spent on insertion sort would then be O(k2)* O(n/k) = O(nk). T(n) for insertion sort = O(nk). Therefore, the total time for this assortment algorithm is as follows T(n) = O(nk + nlg(n/k) ). ... Solution From the above problem (1), we find that quick sort sorts k-elements of an n-element array O(n log(n/k)) time. Quick sort sorts by partitioning the given array Ap...r into devil sub-arrays Ap...q and Aq+1... r such that every element in Ap...q is less than, or equal to, elements in Aq+1... r. This process is repeated until all the elements are sorted. Algorithm for quick sort is given by AP is the pivot key upon which the comparison is made. P is chosen as the median value of the array at each step. If the element is less than, or equal to, the pivot key value, it is moved left. Othe rwise, it is moved right. Assuming the best case scenario where each step produces two equal partitions, then T(n)=T(n/2)+T(n/2)+?(n) =2T(n/2)+ ?(n) By Masters Theorem case 2, T(n) = O(n lg n) In other words, the depth of recursion is log n and at each level/step, the number of elements to be hard-boiled is n. If only k-elements are sorted, then the depth of recursion would be n/k and the number of elements would be n at each level, the time taken by this sorting algorithm is given by T(n) = O(n lg (n/k)). 2.2 Show that we can sort a k-well-sorted array of length n in O(n log k) time. As the array is already sorted for k-elements, the remaining steps required to complete the sort would be k (using the results from 1), then T(n) = O(n lg k). 3. Computing the k-th smallest element in the union of the two mentions m and n using O(lg m +lg n) time algorithm Approach 1 Merge sort can be used in this case. It splits the list into two halves, recursively sorts each half, and then merges the two sorted sub-lists. In the given problem, the lists are already sorted hence, the

No comments:

Post a Comment