Linear Sorting - Data Structures and Algorithm - Lecture Slides, Slides of Data Structures and Algorithms

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

2012/2013

Uploaded on 04/27/2013

shareeka_555
shareeka_555 ๐Ÿ‡ฎ๐Ÿ‡ณ

4

(6)

74 documents

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Linear Sorting
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download Linear Sorting - Data Structures and Algorithm - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Linear Sorting

Problem of the Day

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.

Solution

Quicksort Pseudocode

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)

Quicksort Animation

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

Best Case for Quicksort

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).

Worst Case for Quicksort

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!

How Many Good Partitions

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 Many Bad Partitions?

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.

Average-Case Analysis of Quicksort (*)

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

Pick a Better Pivot

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:

  1. Use the middle element of the subarray as pivot.
  2. Use a random element of the array as the pivot.
  3. Perhaps best of all, take the median of three elements (first, last, middle) as the pivot. Why should we use median instead of the mean?

Whichever of these three rules we use, the worst case remains O(n^2 ). Docsity.com

Is Quicksort really faster than Heapsort?

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.