Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting. How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order.

Similar presentations


Presentation on theme: "Sorting. How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order."— Presentation transcript:

1 Sorting

2 How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order of elements. The best worst-case running time that we’ve seen for comparison sorting is O(nlgn). – Is O(nlgn)the best we can do? Decision treescan help us answer this question. E.g., insertion sort, merge sort, quicksort, heapsort.

3 Decision-tree example Sort 〈 a1, a2, …, an 〉 Each internal node is labeled i:j for i, j ∈ {1, 2,…, n}. The left subtree shows subsequent comparisons if ai≤aj. The right subtree shows subsequent comparisons if ai≥aj.

4 Sort 〈 a1, a2, a3 〉 = 〈 9, 4, 6 〉 :

5 Sort 〈 a1, a2, a3 〉 = 〈 9, 4, 6 〉 :

6 Sort 〈 a1, a2, a3 〉 = 〈 9, 4, 6 〉 :

7 Each leaf contains a permutation 〈 π(1), π(2),…, π(n) 〉 to indicate that the ordering aπ(1 ) ≤aπ(2)≤...≤aπ(n)has been established. Sort 〈 a1, a2, a3 〉 = 〈 9, 4, 6 〉 :

8 Decision-tree model A decision tree can model the execution of any comparison sort: One tree for each input size n. (there is specific tree for different n values tree is not generic) View the algorithm as splitting whenever it compares two elements. The tree contains the comparisons along all possible instruction traces. The running time of the algorithm =the length of the path taken. Worst-case running time =height of tree.

9 Lower bound for decision-tree sorting Theorem. Any decision tree that can sort n elements must have height Ω(nlgn). Proof. The tree must contain ≥n! leaves, since there are n! possible permutations. A height-h binary tree has ≤2 h leaves. – Thus, n! ≤2 h. h≥lg(n!) (lg is mono. İncreasing ) ≥lg ((n/e) n )(Stirling’s formula) = nl(g n–lg e) lg e is constant = Ω(nlg n) monotonically increasing,increasing if for all x and y such that x ≤ y one has f(x) ≤ f(y), so f preserves the order

10 Lower bound for comparison sorting Corollary Heapsort and merge sort are asymptotically optimal comparison sorting algorithms.

11 Counting Sort 1CountingSort(A, B, k) 2for i=1 to k 3C[i]= 0; 4for j=1 to n 5C[A[j]] += 1; 6for i=2 to k 7C[i] = C[i] + C[i-1]; 8for j=n downto 1 9B[C[A[j]]] = A[j]; 10C[A[j]] -= 1; What will be the running time? Takes time O(k) Takes time O(n)

12 Analysis

13 Running time If k= O(n), then counting sort takes Θ(n)time. But, sorting takes Ω(nlgn) time! Answer: Comparison sorting takes Ω(nlgn)time. Counting sort is not a comparison sort. In fact, not a single comparison between elements occurs!

14 Why don’t we always use counting sort? Because it depends on range k of elements Could we use counting sort to sort 32 bit integers? Why or why not? Answer: no, k too large (2 32 = 4,294,967,296)

15 Stable sorting Counting sort is a stablesort: it preserves the input order among equal elements.

16 Dynamic sets Dynamic sets Sets that can grow, shrink, or otherwise change over time. Two types of operations: – queries return information about the set – modifying operationschange the set Common queries Search, Minimum, Maximum, Successor, Predecessor Common modifying operations Insert, Delete

17 Dictionary Dictionary Stores a set S of elements, each with an associated key (integer value). Operations Search(S, k): return a pointer to an element x in S with key[x] = k, or NIL if such an element does not exist. Insert(S, x): inserts element x into S, that is, S ← S ⋃ {x} Delete(S, x): remove element x from S

18 SearchInsertDelete linked list sorted array hash table Θ(1) Θ(n) Θ(log n) Implementing a dictionary Hash table Running times are average times and assume (simple) uniform hashing and a large enough table (for example, of size 2n) TodayBinary search trees Θ(1)

19 Binary search trees Binary search trees are an important data structure for dynamic sets. They can be used as both a dictionary and a priority queue. They accomplish many dynamic-set operations in O(h) time, where h = height of tree.

20 5 Binary search trees root of T denoted by root[T] internal nodes have four fields: key (and possible other satellite data) left: points to left child right: points to right child p: points to parent. p[root[T]] = NIL 1 73 8 4 Binary tree T root[T] = NIL

21 Binary search trees 1 5 73 8 4 Binary tree T root[T] Keys are stored only in internal nodes! There are binary search trees which store keys only in the leaves …

22 Binary search trees a binary tree is – a leaf or – a root node x with a binary tree as its left and/or right child Binary-search-tree property – if y is in the left subtree of x, then key[y] ≤ key[x] – if y is in the right subtree of x, then key[y] ≥ key[x] Keys don’t have to be unique … 1 5 73 8 4

23 Binary search trees 1 5 73 8 4 1 3 7 85 4 height h = 2 height h = 4 Binary-search-tree property – if y is in the left subtree of x, then key[y] ≤ key[x] – if y is in the right subtree of x, then key[y] ≥ key[x]

24 Inorder tree walk binary search trees are recursive structures ➨ recursive algorithms often work well Example: print all keys in order using an inorder tree walk InorderTreeWalk(x) 1. if x ≠ NIL 2. then InorderTreeWalk(left[x]) 3. print key[x] 4. InorderTreeWalk(right[x]) Correctness: follows by induction from the binary search tree property Running time? Intuitively, O(n) time for a tree with n nodes, since we visit and print each node once. 1 5 73 8 4

25 Inorder tree walk InorderTreeWalk(x) 1. if x ≠ NIL 2. then InorderTreeWalk(left[x]) 3. print key[x] 4. InorderTreeWalk(right[x]) Theorem If x is the root of an n-node subtree, then the call InorderTreeWalk(x) takes Θ(n) time. Proof: – T(n) takes small, constant amount of time on empty subtree ➨ T(0) = c for some positive constant c – for n > 0 assume that left subtree has k nodes, right subtree n-k-1 ➨ T(n) = T(k) + T(n-k-1) + d for some positive constant d use substitution method … to show: T(n) = (c+d)n + c ■

26 Querying a binary search tree TreeSearch(x, k) 1. if x = NIL or k = key[x] 2. then return x 3. if k < key[x] 4. then return TreeSearch(left[x], k) 5. else return TreeSearch(right[x], k) Initial call: TreeSearch(root[T], k) – TreeSearch(root[T], 4) – TreeSearch (root[T], 2) Running time: Θ(length of search path) worst case Θ(h) k ≤ k≥ k Binary-search-tree property 1 5 73 8 4

27 Querying a binary search tree – iteratively TreeSearch(x, k) 1. if x = NIL or k = key[x] 2. then return x 3. if k < key[x] 4. then return TreeSearch(left[x], k) 5. else return TreeSearch(right[x], k) IterativeTreeSearch(x, k) 1. while x ≠ NIL and k ≠ key[x] 2. do if k < key[x] 3. then x ← left[x] 4. else x ← right[x] 5. return x iterative version more efficient on most computers k ≤ k≥ k Binary-search-tree property

28 Minimum and maximum binary-search-tree property guarantees that – the minimum key is located in the leftmost node – the maximum key is located in the rightmost node TreeMinimum(x) 1. while left[x] ≠ NIL 2. do x ← left[x] 3. return x TreeMaximum(x) 1. while right[x] ≠ NIL 2. do x ← right[x] 3. return x k ≤ k≥ k Binary-search-tree property 1 5 73 8 4

29 Minimum and maximum binary-search-tree property guarantees that – the minimum key is located in the leftmost node – the maximum key is located in the rightmost node TreeMinimum(x) 1. while left[x] ≠ NIL 2. do x ← left[x] 3. return x TreeMaximum(x) 1. while right[x] ≠ NIL 2. do x ← right[x] 3. return x k ≤ k≥ k Binary-search-tree property Running time? Both procedures visit nodes on a downward path from the root ➨ O(h) time

30 Successor and predecessor Assume that all keys are distinct Successor of a node x node y such that key[y] is the smallest key > key[x] We can find y based entirely on the tree structure, no key comparisons are necessary … if x has the largest key, then we say x’s successor is NIL

31 Successor and predecessor Successor of a node x node y such that key[y] is the smallest key > key[x] Two cases: 1.x has a non-empty right subtree ➨ x’s successor is the minimum in x’s right subtree 2.x has an empty right subtree ➨ x’s successor y is the node that x is the predecessor of (x is the maximum in y’s left subtree) as long as we move to the left up the tree (move up through right children), we’re visiting smaller keys …

32 Successor and predecessor TreeSucessor(x) 1. if right[x] ≠ NIL 2. then return TreeMinimum(right[x]) 3. y ← p[x] 4. while y ≠ NIL and x = right[y] 5. do x ← y 6. y ← p[x] 7. return y – Successor of 15? – Successor of 6? – Successor of 4? 3 15 186 2017 7 4213 9

33 Successor and predecessor TreeSucessor(x) 1. if right[x] ≠ NIL 2. then return TreeMinimum(right[x]) 3. y ← p[x] 4. while y ≠ NIL and x = right[y] 5. do x ← y 6. y ← p[x] 7. return y Running time? 3 15 186 2017 7 4213 9 O(h)

34 Insertion TreeInsert(T, z) 1. y ← NIL 2. x ← root[T] 3. while x ≠ NIL 4. do y ← x 5. if key[z] < key[x] 6. then x ← left[x] 7. else x ← right[x] 8. p[z] ← y 9. if y = NIL 10. then root[T] ← x 11. else if key[z] < key[y] 12. then left[y] ← z 13. else right[y] ← z to insert value v, insert node z with key[z] = v, left[z] = NIL, and right[z] = NIL traverse tree down to find correct position for z

35 13 7 6 15 Insertion TreeInsert(T, z) 1. y ← NIL 2. x ← root[T] 3. while x ≠ NIL 4. do y ← x 5. if key[z] < key[x] 6. then x ← left[x] 7. else x ← right[x] 8. p[z] ← y 9. if y = NIL 10. then root[T] ← x 11. else if key[z] < key[y] 12. then left[y] ← z 13. else right[y] ← z to insert value v, insert node z with key[z] = v, left[z] = NIL, and right[z] = NIL traverse tree down to find correct position for z 3 18 2017 42 9 14z x y = NIL y xy xy xy x

36 Insertion TreeInsert(T, z) 1. y ← NIL 2. x ← root[T] 3. while x ≠ NIL 4. do y ← x 5. if key[z] < key[x] 6. then x ← left[x] 7. else x ← right[x] 8. p[z] ← y 9. if y = NIL 10. then root[T] ← z 11. else if key[z] < key[y] 12. then left[y] ← z 13. else right[y] ← z Running time? to insert value v, insert node z with key[z] = v, left[z] = NIL, and right[z] = NIL traverse tree down to find correct position for z 3 18 2017 42 9 14 z y 15 6 7 13 O(h)

37 7 Deletion we want to delete node z TreeDelete has three cases: z has no children ➨ delete z by having z’s parent point to NIL, instead of to z 3 15 186 20 9

38 18 Deletion we want to delete node z TreeDelete has three cases: z has no children ➨ delete z by having z’s parent point to NIL, instead of to z z has one child ➨ delete z by having z’s parent point to z’s child, instead of to z 3 15 6 20 9 7

39 we want to delete node z TreeDelete has three cases: z has no children ➨ delete z by having z’s parent point to NIL, instead of to z z has one child ➨ delete z by having z’s parent point to z’s child, instead of to z z has two children ➨ z’s successor y has either no or one child ➨ delete y from the tree and replace z’s key and satellite data with y’s 6 7 Deletion 3 15 20 9 18

40 we want to delete node z TreeDelete has three cases: z has no children ➨ delete z by having z’s parent point to NIL, instead of to z z has one child ➨ delete z by having z’s parent point to z’s child, instead of to z z has two children ➨ z’s successor y has either no or one child ➨ delete y from the tree and replace z’s key and satellite data with y’s Running time? 7 Deletion 3 15 20 9 18 O(h)

41 Minimizing the running time all operations can be executed in time proportional to the height h of the tree (instead of proportional to the number n of nodes in the tree) Worst case: Solution: guarantee small height (balance the tree) ➨ h = Θ(log n) Next week: several schemes to balance binary search trees Θ(n)

42 Balanced binary search trees Advantages of balanced binary search trees over linked lists efficient search in Θ(log n) time over sorted arrays efficient insertion and deletion in Θ(log n) time over hash tables can find successor and predecessor efficiently in Θ(log n) time

43 SearchInsertDelete linked list sorted array hash table binary search tree Θ(1) Θ(n) Θ(log n) Implementing a dictionary balanced binary search trees ➨ height is Θ(log n) Θ(1) Θ(height)


Download ppt "Sorting. How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order."

Similar presentations


Ads by Google