Presentation is loading. Please wait.

Presentation is loading. Please wait.

3 The Array Data Structure

Similar presentations


Presentation on theme: "3 The Array Data Structure"— Presentation transcript:

1 3 The Array Data Structure
Properties of arrays and subarrays. Insertion. Deletion. Searching: linear and binary search. Merging. Sorting: selection-, insertion-, merge-, and quick-sort. © 2001, D.A. Watt and D.F. Brown

2 Properties of arrays in general
An array is a sequence of indexed components: array indices low+1 a low+2 high–1 high low high–2 array components The length of the array (its number of components) is fixed when the array is constructed. Each array component has a fixed and unique index. The indices range from a lower bound to an upper bound. Any array component can be efficiently accessed (inspected or updated) using its index, in O(1) time.

3 Properties of arrays in Java
A Java array’s components are either values of some stated primitive type, or objects of some stated class. A Java array of length n has lower bound 0 and upper bound n–1. A Java array is itself an object. It is allocated dynamically by “new T[n]”. n–1 length 1 n–2 a n T[] class tag array components

4 Example 1: Java primitive array
Code to create, initialize, and inspect an array of integers: int[] primes = {2, 3, 5, 7, 11, 13}; for (int i = 0; i < primes.length; i++) System.out.println(primes[i]); primes 5 length 1 4 2 3 7 11 6 int[] class tag 13

5 Example 2: Java object array (1)
Suppose that a Date object has fields y, m, d. Code to create an array of Date objects: Date[] hols = new Date[3]; length hols 3 Date[] class tag 1 2

6 Example 2 (2) Code to update the array of Date objects:
hols[0] = new Date(2002, 1, 1); hols[1] = new Date(2001, 5, 1); hols[2] = new Date(2001, 12, 25); length 1 hols 3 Date[] class tag 2 2002 Date 5 2001 y m d 12 25

7 Subarrays A subarray is a sequence of consecutive components that forms a part of a larger array. Notation: let a[l…r] be the subarray consisting of components a[l], …, a[r]. Subarray notation is used here, but not supported by Java. 8 1 7 a 2 3 4 5 6 9 subarray a[1…3] subarray a[6…9] Length of subarray a[l…r] is r – l + 1.

8 Sorted arrays A (sub)array is sorted if its components are in ascending order, i.e., each component is less than or equal to the component on its right. The meaning of the comparison “x is less than y” (or “y is greater than x”) must be defined for each data type. Meaning of less for numbers: x is numerically less than y, i.e., x < y. Conventional meaning of less for strings: x precedes y lexicographically. E.g.: “bat” is less than “bath”, which is less than “bay”.

9 Interface java.util.Comparable
Java provides: public interface Comparable { public int compareTo (Object that); // Return a negative integer if this object is less than that, // or zero if this object is equal to that, // or a positive integer if this object is greater than that. } The compareTo method captures the notion of less (greater) for objects. If a class implements Comparable, it must implement compareTo in accordance with its “contract”.

10 Insertion (1) Problem: Given a (sub)array a[left…right], insert a value val at a[ins]. If necessary, shift values right to make way for it. Array insertion algorithm: To insert val at index ins in a[left…right] (where left  ins  right): 1. Copy a[ins…right–1] into a[ins+1…right]. 2. Copy val into a[ins]. 3. Terminate.

11 Insertion (2) Animation:
To insert val at index ins in a[left…right] (where left  ins  right): 1. Copy a[ins…right–1] into a[ins+1…right]. 2. Copy val into a[ins]. 3. Terminate. The left = 0 1 2 3 4 5 6 = right fat cat sat on the mouse a val ins To insert val at index ins in a[left…right] (where left  ins  right): 1. Copy a[ins…right–1] into a[ins+1…right]. 2. Copy val into a[ins]. 3. Terminate. The left = 0 1 2 3 4 5 6 = right fat cat sat on the mouse a val ins To insert val at index ins in a[left…right] (where left  ins  right): 1. Copy a[ins…right–1] into a[ins+1…right]. 2. Copy val into a[ins]. 3. Terminate. The left = 0 1 2 3 4 5 6 = right cat sat on the mouse mat fat a val ins To insert val at index ins in a[left…right] (where left  ins  right): 1. Copy a[ins…right–1] into a[ins+1…right]. 2. Copy val into a[ins]. 3. Terminate. The left = 0 1 2 3 4 5 6 = right cat sat on the mouse fat a val ins

12 Insertion (3) Analysis (counting copies):
Let n = right – left + 1 be the length of the array. Step 2 performs 1 copy. Step 1 performs between 0 and n–1 copies, say (n–1)/2 copies on average. Average no. of copies = (n – 1)/ = n/2 + 1/2 Time complexity is O(n).

13 Deletion (1) Problem: Given a (sub)array a[left…right], delete the value in a[del]. If necessary, shift values left to fill the gap. (Assume that left  del  right.) Array deletion algorithm: To delete the value at index del in a[left…right] (where left  del  right): 1. Copy a[del+1…right] into a[del…right–1]. 2. Make a[right] unoccupied. 3. Terminate.

14 Deletion (2) Animation:
To delete the value at index del in a[left…right] (where left  del  right): 1. Copy a[del+1…right] into a[del…right–1]. 2. Make a[right] unoccupied. 3. Terminate. The left = 0 1 2 3 4 5 6 = right cat sat on the mouse a del To delete the value at index del in a[left…right] (where left  del  right): 1. Copy a[del+1…right] into a[del…right–1]. 2. Make a[right] unoccupied. 3. Terminate. The left = 0 1 2 3 4 5 6 = right cat sat on the mouse a del To delete the value at index del in a[left…right] (where left  del  right): 1. Copy a[del+1…right] into a[del…right–1]. 2. Make a[right] unoccupied. 3. Terminate. The left = 0 1 2 3 4 5 6 = right fat cat sat on the mouse a del To delete the value at index del in a[left…right] (where left  del  right): 1. Copy a[del+1…right] into a[del…right–1]. 2. Make a[right] unoccupied. 3. Terminate. The left = 0 1 2 3 4 5 6 = right cat sat on the mouse a del

15 Deletion (3) Analysis (counting copies):
Let n = right – left + 1 be the length of the array. Step 1 performs between 0 and n–1 copies. Average no. of copies = (n – 1)/2 = n/2 – 1/2 Time complexity is O(n).

16 Searching Problem: Given a (sub)array a[left…right], find which (if any) component equals a given target value. Choice of algorithms: linear search (unsorted or sorted array) binary search (sorted array).

17 known not to equal target
Linear search (1) Linear search algorithm: To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. Loop invariant: left+1 a p right–1 left p–1 right known not to equal target still to be searched

18 Invariants An invariant is a logical statement that always holds at a particular step in an algorithm (or program). A loop invariant is an invariant that holds at every iteration of a loop. Invariants can be expressed: in logical notation (e.g., 0  i < n) as a schematic diagram (as above).

19 Linear search (2) Animation (successful search):
To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p

20 Linear search (3) Animation (unsuccessful search):
To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right 2 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right 3 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right 2 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right 3 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right p

21 Linear search (4) Analysis (counting comparisons):
Let n = right – left + 1 be the length of the array. If the search is unsuccessful, step 1.1 is repeated n times. No. of comparisons = n If the search is successful, step 1.1 is repeated between 1 and n times. Average no. of comparisons = (n + 1)/2 In either case, time complexity is O(n).

22 Linear search (5) Implementation in Java:
static int linearSearch (Object target, Object[] a, int left, int right) { // Find which (if any) component of a[left…right]equals // target. for (int p = left; p <= right; p++) { if (target.equals(a[p])) return p; } return NONE; // –1, say }

23 Linear search (6) If the (sub)array is known to be sorted, linear search can be speeded up in the unsuccessful case: To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. For p = left, …, right, repeat: If target equals a[p], terminate with answer p If target is less than a[p], terminate with answer none. 2. Terminate with answer none. However, binary search is much better!

24 Binary search (1) Assume that the (sub)array is sorted.
Consider searching a dictionary for a target word. Bad idea: Look at page 1, then page 2, etc., until you find the page containing the target word. That is linear search! Better idea: Choose a page near the middle. If the target word happens to be on that middle page, we’re finished. If the target word is less (greater) than the words on that middle page, focus on the pages before (after) that middle page, and repeat. That is binary search.

25 Binary search (2) Binary search algorithm:
To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none.

26 Binary search (3) Loop invariant: a known to be less than target
left r l–1 a right r+1 known to be less than target still to be searched known to be greater than target

27 Binary search (4) Animation (successful search):
To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l 8 r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l 8 r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l 8 r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l 8 r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l 8 r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l 8 r m

28 Binary search (5) Animation (unsuccessful search):
To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l 8 r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l 8 r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l 8 r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: Let m be an integer about midway between l and r If target equals a[m], terminate with answer m If target is less than a[m], set r = m– If target is greater than a[m], set l = m Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r m

29 Binary search (6) Analysis (counting comparisons):
Let n be the length of the array. Assume that steps 2.2–4 perform a single comparison. If the search is unsuccessful, these steps are repeated as often as we must halve n to reach 0: No. of comparisons = floor(log2 n) + 1 If the search is successful, these steps are repeated at most that many times: Max. no. of comparisons = floor(log2 n) + 1 In either case, the time complexity is O(log n).

30 Binary search (7) Implementation in Java:
static int binarySearch (Comparable target, Comparable[] a, int left, int right) { // Find which (if any) component of the sorted (sub)array // a[left…right] equals target. int l = left, r = right; while (l <= r) { int m = (l + r)/2; int comp = target.compareTo(a[m]); if (comp == 0) return m; else if (comp < 0) r = m - 1; else l = m + 1; } return NONE; }

31 Comparison of searching algorithms
No. of comparisons Time complexity Linear search (unsorted array) ~ n/2 successful n unsuccessful O(n) Linear search (sorted array) ~ n/2 Binary search ~ log2 n O(log n)

32 Merging (1) Problem: Given two sorted arrays, make a third sorted array containing copies of all components of the two original two arrays.

33 Merging (2) Array merging algorithm:
To merge a1[l1…r1] and a2[l2…r2] into a3[l3…r3] (where both a1 and a2 are sorted): 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: If a1[i] is less than or equal to a2[j]: Copy a1[i] into a3[k], then increment i and k If a1[i] is greater than or equal to a2[j]: Copy a2[j] into a3[k], then increment j and k. 3. If i  r1, copy a1[i…r1] into a3[k…r3]. 4. If j  r2, copy a2[j…r2] into a3[k…r3]. 5. Terminate.

34 Merging (3) Loop invariant: a1 already merged still to be merged a2
j l2 r2 j–1 a2 already merged still to be merged k l3 k–1 a3 r3 copied from a1 and/or a2 unoccupied

35 Merging (4) Animation: To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: If a1[i] is less than or equal to a2[j]: Copy a1[i] into a3[k], then increment i and k If a1[i] is greater than or equal to a2[j]: Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: If a1[i] is less than or equal to a2[j]: Copy a1[i] into a3[k], then increment i and k If a1[i] is greater than or equal to a2[j]: Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: If a1[i] is less than or equal to a2[j]: Copy a1[i] into a3[k], then increment i and k If a1[i] is greater than or equal to a2[j]: Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: … 3. If i  r1, copy a1[i…r1] into a3[k…r3]. 4. If j  r2, copy a2[j…r2] into a3[k…r3]. 5. Terminate. cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: … 3. If i  r1, copy a1[i…r1] into a3[k…r3]. 4. If j  r2, copy a2[j…r2] into a3[k…r3]. 5. Terminate. cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: … 3. If i  r1, copy a1[i…r1] into a3[k…r3]. 4. If j  r2, copy a2[j…r2] into a3[k…r3]. 5. Terminate. cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: If a1[i] is less than or equal to a2[j]: Copy a1[i] into a3[k], then increment i and k If a1[i] is greater than or equal to a2[j]: Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: If a1[i] is less than or equal to a2[j]: Copy a1[i] into a3[k], then increment i and k If a1[i] is greater than or equal to a2[j]: Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: If a1[i] is less than or equal to a2[j]: Copy a1[i] into a3[k], then increment i and k If a1[i] is greater than or equal to a2[j]: Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: If a1[i] is less than or equal to a2[j]: Copy a1[i] into a3[k], then increment i and k If a1[i] is greater than or equal to a2[j]: Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: If a1[i] is less than or equal to a2[j]: Copy a1[i] into a3[k], then increment i and k If a1[i] is greater than or equal to a2[j]: Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: If a1[i] is less than or equal to a2[j]: Copy a1[i] into a3[k], then increment i and k If a1[i] is greater than or equal to a2[j]: Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: If a1[i] is less than or equal to a2[j]: Copy a1[i] into a3[k], then increment i and k If a1[i] is greater than or equal to a2[j]: Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: If a1[i] is less than or equal to a2[j]: Copy a1[i] into a3[k], then increment i and k If a1[i] is greater than or equal to a2[j]: Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l While i  r1 and j  r2, repeat: If a1[i] is less than or equal to a2[j]: Copy a1[i] into a3[k], then increment i and k If a1[i] is greater than or equal to a2[j]: Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i

36 Merging (5) Analysis (counting copies):
Let n1 and n2 be the lengths of a1 and a2. Let n = n1 + n2. Each component of a1 is copied once, and each component of a2 is copied once. No. of copies = n1 + n2 = n Time complexity is O(n).

37 Merging (6) Analysis (counting comparisons):
Let n1 and n2 be the lengths of a1 and a2. Let n = n1 + n2. Assume that steps 2.1–2 perform a single comparison. Steps 2.1–2 are repeated at most n–1 times. Max. no. of comparisons = n – 1 Time complexity is again O(n).

38 Merging (7) Implementation in Java:
static void merge ( Comparable[] a1, int l1, int r1, Comparable[] a2, int l2, int r2, Comparable[] a3, int l3) { // Merge a1[l1…r1] and a2[l2…r2] into a3[l3…] // (where both a1 and a2 are sorted). int i = l1, j = l2, k = l3; while (i <= r1 && j <= r2) { int comp = a1[i].compareTo(a2[j]); if (comp <= 0) a3[k++] = a1[i++]; else a3[k++] = a2[j++]; }

39 Merging (8) Implementation (continued):
while (i <= r1) a3[k++] = a1[i++]; while (j <= r2) a3[k++] = a2[j++]; }

40 Remedial Mathematics Summing arithmetic series
An arithmetic series is a sequence of numbers with a fixed difference between consecutive numbers. E.g.: 2, 3, 4, 5 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 Observe: The average of the first and last numbers is the average of the whole arithmetic series. To sum an arithmetic series, multiply the length of the series by the average of the first and last numbers.

41 Remedial Mathematics Example: summing a series
Sum the series 1, 2, …, n–1, n (where n  0). Length of series = n Average of first and last numbers = (n + 1)/2 Sum = n(n + 1)/2 Testing: n = 3 n(n + 1)/2 = 34/2 = = 6 n = 4 n(n + 1)/2 = 45/2 = = 10

42 Sorting Problem: Given an unsorted array of data, rearrange the data into ascending order. This is important because sorted data can be searched and merged efficiently. Choice of algorithms: selection sort insertion sort merge-sort quick-sort shell-sort, radix sort, etc. (not covered here).

43 Selection sort (1) Idea: Find the least value in the array, swap it into the leftmost component (where it belongs), and then forget the leftmost component. Do this repeatedly.

44 Selection sort (2) Selection sort algorithm:
To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. Loop invariant: left+1 a l right–1 left l–1 right lesser values (sorted) greater values (unsorted)

45 Selection sort (3) Animation:
To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion tiger a l rat pig 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion tiger 6 a l rat pig 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion tiger a l rat pig 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion tiger a l rat pig 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion tiger 6 a l rat pig 7 8 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion tiger a l rat pig 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 7 a l rat tiger 6 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 8 a l rat tiger 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 8 a l rat tiger 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 7 a l rat tiger 6 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 7 a l rat tiger 6 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox rat lion tiger a l goat pig 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 6 a l rat tiger 7 8 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox rat lion tiger a l goat pig 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow pig fox rat lion tiger a l goat dog 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow pig fox rat lion tiger a l goat dog 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow pig fox rat lion tiger a l goat dog 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a l goat dog 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a l goat dog 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow pig fox rat lion tiger a l goat dog 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow pig fox rat lion tiger a l goat dog 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox rat lion tiger a l goat pig 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox rat lion tiger a l goat pig 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox rat lion tiger a l goat pig 6 7 8 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow pig fox rat lion tiger a l goat dog 6 7 8 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: Set p such that a[p] is the least of a[l…right] If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox rat lion tiger a l goat pig 6 7

46 Selection sort (4) Analysis (counting comparisons):
Let n = right – left + 1 be the length of the array. Step 1.1 performs right–l comparisons. This is repeated with l = left, …, right–2, right–1. No. of comparisons = (right–left) + … = (n–1) + … = (n – 1)n/2 = (n2 – n)/2 Time complexity is O(n2).

47 Selection sort (5) Implementation in Java:
static void selectionSort ( Comparable[] a, int left, int right) { // Sort a[left…right] into ascending order. for (int l = left; l < right; l++) { int p = l; Comparable least = a[p]; for (int k = l+1; k <= right; k++){ int comp = a[k].compareTo(least); if (comp < 0) { p = k; least = a[p]; } } if (p != l) { a[p] = a[l]; a[l] = least; } } }

48 Insertion sort (1) Idea: We can sort a file of values by successively reading each value and inserting it into its correct position in an array. Use the same idea to sort an array of values in place.

49 Insertion sort (2) Insertion sort algorithm:
To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. Loop invariant: left+1 a r right–1 left r–1 right inserted (sorted) still to be inserted

50 Insertion sort (3) Animation:
To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox lion pig rat tiger 7 a r goat dog 6 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox lion pig rat tiger 6 a r goat dog 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox lion pig rat tiger 6 a r goat dog 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. 7 r cat left = 0 1 2 3 4 5 8 = right cow fox goat lion pig rat a tiger dog 6 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox goat lion pig rat 8 a r tiger dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a rat tiger 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 9 a r rat tiger 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 8 a r rat tiger 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox lion pig rat tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cow left = 0 1 2 3 4 5 8 = right fox pig cat rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cow left = 0 1 2 3 4 5 8 = right fox pig cat rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cow left = 0 1 2 3 4 5 8 = right fox pig cat rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cow left = 0 1 2 3 4 5 8 = right fox pig cat rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: Let val = a[r] Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat lion tiger a r goat dog 6 7

51 Insertion sort (4) Analysis (counting comparisons):
Let n = right – left + 1 be the length of the array. Step 1.2 performs between 1 and r – left comparisons, say (r – left + 1)/2 comparisons on average. This is repeated with r = left+1, left+2, …, right. Average no. of comparisons = 2/2 + 3/2 + … + n/2 = (n – 1)(n + 2)/4 = (n2 + n – 2)/4 Time complexity is O(n2).

52 Merge-sort (1) Idea for sorting an array:
Divide the array into two subarrays of about equal length. Sort the subarrays separately. Merge the sorted subarrays. This is an application of the divide-and-conquer strategy. To solve a “hard” problem: Break the problem down into two or more “easier” sub-problems. Solve these sub-problems separately. Combine their answers.

53 Merge-sort (2) Merge-sort algorithm:
To sort a[left…right] into ascending order: 1. If left < right: Let m be an integer about midway between left and right Sort a[left…m] into ascending order Sort a[m+1…right] into ascending order Merge a[left…m] and a[m+1…right] into auxiliary array b Copy all components of b into a[left…right]. 2. Terminate.

54 Merge-sort (3) Animation:
To sort a[left…right] into ascending order: 1. If left < right: Let m be an integer about midway between left and right Sort a[left…m] into ascending order Sort a[m+1…right] into ascending order Merge a[left…m] and a[m+1…right] into auxiliary array b Copy all components of b into a[left…right]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat dog goat a lion tiger 6 7 m b To sort a[left…right] into ascending order: 1. If left < right: Let m be an integer about midway between left and right Sort a[left…m] into ascending order Sort a[m+1…right] into ascending order Merge a[left…m] and a[m+1…right] into auxiliary array b Copy all components of b into a[left…right]. 2. Terminate. left = 0 1 2 3 4 5 8 = right a 6 7 cat cow dog fox goat lion pig rat tiger To sort a[left…right] into ascending order: 1. If left < right: Let m be an integer about midway between left and right Sort a[left…m] into ascending order Sort a[m+1…right] into ascending order Merge a[left…m] and a[m+1…right] into auxiliary array b Copy all components of b into a[left…right]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat dog goat a lion tiger 6 7 m To sort a[left…right] into ascending order: 1. If left < right: Let m be an integer about midway between left and right Sort a[left…m] into ascending order Sort a[m+1…right] into ascending order Merge a[left…m] and a[m+1…right] into auxiliary array b Copy all components of b into a[left…right]. 2. Terminate. left = 0 1 2 3 4 5 8 = right a 6 7 m cat cow dog fox goat lion pig b rat tiger To sort a[left…right] into ascending order: 1. If left < right: Let m be an integer about midway between left and right Sort a[left…m] into ascending order Sort a[m+1…right] into ascending order Merge a[left…m] and a[m+1…right] into auxiliary array b Copy all components of b into a[left…right]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 m To sort a[left…right] into ascending order: 1. If left < right: Let m be an integer about midway between left and right Sort a[left…m] into ascending order Sort a[m+1…right] into ascending order Merge a[left…m] and a[m+1…right] into auxiliary array b Copy all components of b into a[left…right]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 To sort a[left…right] into ascending order: 1. If left < right: Let m be an integer about midway between left and right Sort a[left…m] into ascending order Sort a[m+1…right] into ascending order Merge a[left…m] and a[m+1…right] into auxiliary array b Copy all components of b into a[left…right]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 To sort a[left…right] into ascending order: 1. If left < right: Let m be an integer about midway between left and right Sort a[left…m] into ascending order Sort a[m+1…right] into ascending order Merge a[left…m] and a[m+1…right] into auxiliary array b Copy all components of b into a[left…right]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat lion tiger a goat dog 6 7 m

55 Merge-sort (4) Analysis (counting comparisons):
Let n = right–left+1 be the length of the array. Let the total no. of comparisons required to sort n values be comps(n). The left subarray’s length is about n/2, so step 1.2 takes about comps(n/2) comparisons to sort it. Similarly, step 1.3 takes about comps(n/2) comparisons to sort the right subarray. Step 1.4 takes about n–1 comparisons to merge the subarrays.

56 Merge-sort (5) Analysis (continued):
Therefore: comps(n)  2 comps(n/2) + n – 1 if n > 1 comps(n) = 0 if n  1 Solution: comps(n)  n log2n Time complexity is O(n log n). Space complexity is O(n), since step 1.4 needs an auxiliary array of length n.

57 Quick-sort (1) Idea: Choose any value from the array (called the pivot). Then partition the array into three subarrays such that: the left subarray contains only values less than (or equal to) the pivot; the middle subarray contains only the pivot; the right subarray contains only values greater than (or equal to) the pivot. Finally sort the left subarray and the right subarray separately. This is another application of the divide-and-conquer strategy.

58 Quick-sort (2) Quick-sort algorithm:
To sort a[left…right] into ascending order: 1. If left < right: Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p] Sort a[left…p–1] into ascending order Sort a[p+1…right] into ascending order. 2. Terminate.

59 Quick-sort (3) Invariants: After step 1.1: a
left p–1 right less than or equal to pivot (unsorted) pivot greater than or equal to pivot (unsorted) After step 1.3: p+1 a p left p–1 right less than or equal to pivot (sorted) pivot greater than or equal to pivot (sorted)

60 Quick-sort (4) Animation:
To sort a[left…right] into ascending order: 1. If left < right: Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p] Sort a[left…p–1] into ascending order Sort a[p+1…right] into ascending order. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a rat tiger 6 7 p To sort a[left…right] into ascending order: 1. If left < right: Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p] Sort a[left…p–1] into ascending order Sort a[p+1…right] into ascending order. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a rat tiger 6 7 To sort a[left…right] into ascending order: 1. If left < right: Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p] Sort a[left…p–1] into ascending order Sort a[p+1…right] into ascending order. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox pig rat lion a tiger goat 6 7 p To sort a[left…right] into ascending order: 1. If left < right: Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p] Sort a[left…p–1] into ascending order Sort a[p+1…right] into ascending order. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 To sort a[left…right] into ascending order: 1. If left < right: Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p] Sort a[left…p–1] into ascending order Sort a[p+1…right] into ascending order. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 To sort a[left…right] into ascending order: 1. If left < right: Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p] Sort a[left…p–1] into ascending order Sort a[p+1…right] into ascending order. 2. Terminate. cow left = 0 1 2 3 4 5 8 = right cat dog fox pig rat lion a tiger goat 6 7 p

61 Quick-sort (5) Analysis (counting comparisons):
Let n be the no. of values to be sorted. Let the total no. of comparisons required to sort n values be comps(n). Step 1.1 takes about n–1 comparisons to partition the array. (NB: partition, not sort!)

62 Quick-sort (6) In the best case, the pivot turns out to be the median value in the array. So the left and right subarrays both have length about n/2. Then steps 1.2 and 1.3 take about comps(n/2) comparisons each. Therefore: comps(n)  2 comps(n/2) + n – 1 if n > 1 comps(n) = 0 if n  1 Solution: comps(n)  n log2n Best-case time complexity is O(n log n).

63 Quick-sort (7) In the worst case, the pivot turns out to be the smallest value. So the right subarray has length n–1 whilst the left subarray has length 0. Then step 1.3 performs comps(n–1) comparisons, but step 1.2 does nothing at all. Therefore: comps(n)  comps(n–1) + n – 1 if n > 1 comps(n) = 0 if n  1 Solution: comps(n)  (n2 – n)/2 Worst-case time complexity is O(n2). The worst case arises if the array is already sorted!

64 Quick-sort (8) Implementation in Java:
static void quickSort ( Comparable[] a, int left, int right) { // Sort a[left…right] into ascending order. if (left < right) { int p = partition(a, left, right); quickSort(a, left, p-1); quickSort(a, p+1, right); } }

65 Quick-sort (9) Partitioning algorithm:
To partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]: 1. Let pivot be the value of a[left], and set p = left. 2. For r = left+1, …, right, repeat: If a[r] is less than pivot: Copy a[r] into a[p], a[p+1] into a[r], and pivot into a[p+1] Increment p Terminate with answer p. Note that other (and better) partitioning algorithms exist.

66 Quick-sort (10) Loop invariant: a
left p–1 r–1 r right less than or equal to pivot (unsorted) pivot greater than or equal to pivot (unsorted) still to be partitioned

67 Quick-sort (11) Implementation in Java:
static int partition ( Comparable[] a, int left, int right) { // Partition a[left…right] such that // a[left…p-1] are all less than or equal to a[p], and // a[p+1…right] are all greater than or equal to a[p]. // Return p. Comparable pivot = a[left]; int p = left;

68 Quick-sort (12) Implementation (continued):
for (int r = left+1; r <= right; r++) { int comp = a[r].compareTo(pivot); if (comp < 0) { a[p] = a[r]; a[r] = a[p+1]; a[p+1] = pivot; p++; } } return p; }

69 Comparison of sorting algorithms
No. of comparisons No. of copies Time complexity Space complexity Selection sort ~ n2/2 ~ 2n O(n2) O(1) Insertion sort ~ n2/4 Merge-sort ~ n log2n ~ 2n log2n O(n log n) O(n) Quick-sort ~ n log2n ~ n2/2 ~ 2n/3 log2n 0 O(n log n) O(n2) O(log n) O(n) best worst


Download ppt "3 The Array Data Structure"

Similar presentations


Ads by Google