

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Some concept of Data Structures and Algorithm are Permutation, Representation, Implemented, Algorithm Design, Dynamic Programming, Graph Data Structures, String Processing, General Trees. Main points of this lecture are: Linear Sorting, Nuts, Bolts, Problem, Collection, Corresponding Nuts, Learn Whether, Bolts, Comparing, Sizes
Typology: Slides
1 / 33
This page cannot be seen from the preview
Don't miss anything!


























The nuts and bolts problem is defined as follows. You are given a collection of n bolts of different widths, and n corresponding nuts. You can test whether a given nut and bolt together, from which you learn whether the nut is too large, too small, or an exact match for the bolt. The differences in size between pairs of nuts or bolts can be too small to see by eye, so you cannot rely on comparing the sizes of two nuts or two bolts directly. You are to match each bolt to each nut.
Sort(A) Quicksort(A,1,n)
Quicksort(A, low, high) if (low < high) pivot-location = Partition(A,low,high) Quicksort(A,low, pivot-location - 1) Quicksort(A, pivot-location+1, high)
Q U I C K S O R T Q I C K S O R T U Q I C K O R S T U I C K O Q R S T U I C K O Q R S T U I C K O Q R S T U
Since each element ultimately ends up in the correct position, the algorithm correctly sorts. But how long does it take? The best case for divide-and-conquer algorithms comes when we split the input as evenly as possible. Thus in the best case, each subproblem is of size n/ 2. The partition step on each subproblem is linear in its size. Thus the total effort in partitioning the 2 k^ problems of size n/ 2 k^ is O(n).
Suppose instead our pivot element splits the array as unequally as possible. Thus instead of n/ 2 elements in the smaller half, we get zero, meaning that the pivot element is the biggest or smallest element in the array.
Now we have nโ 1 levels, instead of lg n, for a worst case time of ฮ(n^2 ), since the first n/ 2 levels each have โฅ n/ 2 elements to partition. To justify its name, Quicksort had better be good in the average case. Showing this requires some intricate analysis. The divide and conquer principle applies to real life. If you break a job into pieces, make the pieces of equal size!
If we assume that the pivot element is always in this range, what is the maximum number of partitions we need to get from n elements down to 1 element?
(3/4)l^ ยท n = 1 โโ n = (4/3)l
lg n = l ยท lg(4/3)
Therefore l = lg(4/3) ยท lg(n) < 2 lg n good partitions suffice.
How often when we pick an arbitrary element as pivot will it generate a decent partition? Since any number ranked between n/ 4 and 3 n/ 4 would make a decent pivot, we get one half the time on average. If we need 2 lg n levels of decent partitions to finish the job, and half of random partitions are decent, then on average the recursion tree to quicksort the array has โ 4 lg n levels.
To do a precise average-case analysis of quicksort, we formulate a recurrence given the exact expected time T (n):
T (n) = โn p=
n
(T (p โ 1) + T (n โ p)) + n โ 1
Each possible pivot p is selected with equal probability. The number of comparisons needed to do the partition is n โ 1. We will need one useful fact about the Harmonic numbers Hn, namely
Hn = โn i=
1 /i โ ln n
It is important to understand (1) where the recurrence relation
comes from and (2) how the log comes out from the summation. The rest is just messy algebra.
T (n) = โn p=
n
(T (p โ 1) + T (n โ p)) + n โ 1
T (n) =
n
โ^ n p=
T (p โ 1) + n โ 1
nT (n) = 2 โn p=
T (p โ 1) + n(n โ 1) multiply by n
(nโ1)T (nโ1) = 2
nโโ 1 p=
T (pโ1)+(nโ1)(nโ2) apply to n-
nT (n) โ (n โ 1)T (n โ 1) = 2T (n โ 1) + 2(n โ 1)
rearranging the terms give us:
T (n) n + 1
T (n โ 1) n
2(n โ 1) n(n + 1) Docsity.com
Having the worst case occur when they are sorted or almost sorted is very bad , since that is likely to be the case in certain applications. To eliminate this problem, pick a better pivot:
Whichever of these three rules we use, the worst case remains O(n^2 ). Docsity.com
Since Heapsort is ฮ(n lg n) and selection sort is ฮ(n^2 ), there is no debate about which will be better for decent-sized files. When Quicksort is implemented well, it is typically 2-3 times faster than mergesort or heapsort. The primary reason is that the operations in the innermost loop are simpler. Since the difference between the two programs will be limited to a multiplicative constant factor, the details of how you program each algorithm will make a big difference.