Upcoming SlideShare
×

# chapter-8.ppt

1,177 views
1,002 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,177
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
27
0
Likes
0
Embeds 0
No embeds

No notes for slide

### chapter-8.ppt

1. 1. Lower bound for Sorting We use only comparisons between elements to gain order information about an input sequence a 1 ,a 2 ,………… a n .
2. 2. The decision tree model A decision tree is a fully binary tree that represents the comparisons between elements that are performed by a particular sorting algorithm operating on an input of a given size
3. 3. The decision tree model 1:2 2:3 1:3 1:3 2:3 <1,2,3> <1,3,2> <2,1,3> <3,2,1> <2,3,1> <3,1,2> ≤ ≤ ≤ ≤ ≤ > > > > >
4. 4. A lower bound for the worst case The length of the longest path from the root of a decision tree to any of its reachable leaves represents the worst-case number of comparisons that the corresponding sorting algorithm performs.
5. 5. Counting Sort The basic idea of counting sort is to determine, for each input element x, the number of elements less than x. This information can be used to place element x directly into its position in the output array.
6. 6. Counting Sort COUNTTING-SORT(A, B, k) for i  0 to k do C[i]  0 for j  1 to length[A] do C[A[j]]  C[A[j]]+1 for i  1 to k do C[i]  C[i] + C[i-1] for j  length[A] downto 1 do B[C[A[j]]]  A[j] C[A[j]]  C[A[j]]-1
7. 7. Counting Sort (a) A 3 0 3 2 0 3 5 2 5 4 3 2 1 8 7 6 C 1 0 3 2 0 2 4 3 2 1 0 5
8. 8. Counting Sort (b) C 8 7 7 4 2 2 4 3 2 1 0 5
9. 9. Counting Sort (c) B 3 5 4 3 2 1 8 7 6 C 8 7 6 4 2 2 4 3 2 1 0 5
10. 10. Counting Sort (d) B 3 0 5 4 3 2 1 8 7 6 C 8 7 6 4 2 1 4 3 2 1 0 5
11. 11. Counting Sort (e) B 3 3 0 5 4 3 2 1 8 7 6 C 8 7 5 4 2 1 4 3 2 1 0 5
12. 12. Counting Sort (f) B 5 3 3 3 2 2 0 0 5 4 3 2 1 8 7 6
13. 13. How much time does Counting Sort require? The first for loop takes time θ (k) second for loop takes time θ (n) third for loop takes time θ (k) fourth for loop takes time θ (n) Thus the overall time is θ (k+n). In which case the running time is θ (n.