VIEWS: 1 PAGES: 61 POSTED ON: 8/9/2012 Public Domain
Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 3. Array Data Structures Properties of arrays and subarrays Insertion and Deletion Searching: linear and binary search Merging and Sorting Java Vector Class Guide Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 1 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Properties of arrays in general An array is a sequence of indexed components. Each array component has a fixed and unique index. The indices range from a lower bound to an upper bound: low low+1 low+2 high–2 high–1 high indices a components • The length of the array (its number of components) is fixed when the array is constructed. • Any array component can be efficiently accessed (inspected or updated) using its index, in O(1) time. Time Complexity is Excellent! Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 2 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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]”. class tag length 0 1 n–2 n–1 a T[] n components Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 3 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Example: 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 class tag length 0 1 2 3 4 5 int[] 6 2 3 5 7 11 13 Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 4 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Example: 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]; class tag length 0 1 2 hols Date[] 3 Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 5 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Example: Java object array (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); class tag length 0 1 2 hols Date[] 3 class tag y m d Date 2001 12 25 class tag y m d Date 2001 5 1 class tag y m d Date 2002 1 1 Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 6 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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. 0 1 2 3 4 5 6 7 8 9 a subarray a[1…3] subarray a[6…9] • Length of subarray a[l…r] is r – l + 1. Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 7 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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”. Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 8 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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”. Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 9 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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. To insert val at index ins in a[left…right] Animation: To insert val at index ins in a[left…right] (where left ins right) (where left ins right): 1. 1. Copy a[ins…right–1] into a[ins+1…right]. Copy a[ins…right–1] into a[ins+1…right]. 2. 2. Copy val into a[ins]. Copy val into a[ins]. 3. 3. Terminate. Terminate. left = 0 left = 0 1 1 2 2 3 3 4 4 5 5 6 = right 6 = right a The a The fat cat fat cat sat cat sat sat on on the mouse mouse on the mouse the mat val val fat fat ins ins 1 1 Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 10 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Insertion (2) Analysis (counting copies): Let n = right – left + 1 be the length of the array. Step 2 performs 1 copy only Step 1 performs between 0 and n–1 copies, say (n–1)/2 copies on average. Average no. of copies = (n – 1)/2 + 1 = n/2 + 1/2 Time complexity is O(n). Time Complexity is Linear and therefore Normal! Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 11 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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.) 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. left = 0 1 2 3 4 5 6 = right a The fat cat sat cat sat on on the the mouse mouse del 1 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Deletion (2) 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). Time Complexity is Linear and therefore Normal! Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 13 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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). An invariant is a logical statement that always holds TRUE 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). Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Linear search (1) Linear search algorithm: To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Otherwise, Terminate with answer none. Loop invariant: left left+1 p–1 p right–1 right a known not to equal target still to be searched Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 15 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Linear search (2) Animation (successful search): To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. left = 0 1 2 3 4 5 6 7 8 = right a rat cat pig cow fox lion tiger goat dog target cow p 1 0 3 2 Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 16 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Linear search (3) Animation (unsuccessful search): To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. left = 0 1 2 = right a rat cat pig target dog p 2 1 0 3 Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 17 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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). Time complexity is Normal. Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 18 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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 } Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 19 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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: 1.1. If target equals a[p], terminate with answer p. 1.2. If target is less than a[p], terminate with answer none. 2. Terminate with answer none. However, binary search is much better! Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 20 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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. Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 21 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. left l–1 l r r+1 right a Loop invariant: known to be still to be searched known to be less than target greater than target Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Binary search (3) 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: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. left = 0 1 2 3 4 5 6 7 8 = right a cat cow dog fox goat lion pig rat tiger target lion l 5 0 r 5 8 m 4 5 6 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Binary search (4) 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: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. left = 0 1 2 3 4 5 6 7 8 = right a cat cow dog fox goat lion pig rat tiger target dingo l 2 0 r 1 3 8 m 4 2 1 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Binary search (5) 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). Time Complexity has a slower growth rate in the number of multiplication and it is therefore better than normal. Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 25 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Binary search (6) 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; } Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 26 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Comparison of searching algorithms Algorithm No. of Time comparisons complexity Linear search ~ n/2successful O(n) (unsorted array) nunsuccessful Linear search ~ n/2 O(n) (sorted array) Binary search ~ log2 n O(log n) (for sorted array only) Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 27 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Merging (1) Problem: Given two sorted arrays, make a third sorted array containing copies of all components of the two original arrays. Array merging algorithm: To merge a1[l1…r1] and a2[l2…r2] into a3[l3…r3] (a1 and a2 are sorted): 1. Set i = l1, set j = l2, and set k = l3. 2. While i r1 and j r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. OR 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. 3. If i r1, copy a1[i…r1] into a3[k…r3] or 4. If j r2, copy a2[j…r2] into a3[k…r3]. 5. Terminate. Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Merging (2) Loop invariant: l1 i–1 i r1 a1 already merged still to be merged l2 j–1 j r2 a2 already merged still to be merged l3 k–1 k r3 a3 copied from a1 and/or a2 unoccupied Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 29 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Merging (3) To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: Animation: 1. Set i = l1, set j = l2, and set k = l3. 2. While i r1 and j r2, repeat: … 2.1. If a1[i] is less than or equal to a2[j]: 3. If i r1, copy a1[i…r1] into a3[k], then increment i and k. 2.1.1. Copy a1[i] a3[k…r3]. 4. 2.2. r2, copy a2[j…r2] into a3[k…r3].a2[j]: If j If a1[i] is greater than or equal to 2.2.1. 5. Terminate. Copy a2[j] into a3[k], then increment j and k. … l1 = 0 1 = r1 a1 cow goat i 1 0 2 l2 = 0 1 2 3 4 = r2 a2 cat dog fox lion tiger j 2 1 0 3 l3 = 0 1 2 3 4 5 6 a3 cat cow dog fox goat lion tiger k 3 2 1 0 5 4 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Merging (4) 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). Analysis - counting comparisons: 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). Total Time complexity: O(n) + O(n) = 2 O(n) O(n). Time complexity is normal. Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Merging (5) 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++]; } while (i <= r1) a3[k++] = a1[i++]; while (j <= r2) a3[k++] = a2[j++]; } Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Sorting Problem: Given an unsorted array of data, rearrange the data into ascending order. This is important because sorted data can be searched (binary) and merged efficiently. Choice of algorithms: – selection sort – insertion sort – merge-sort – quick-sort – bubble-sort,shell-sort, radix sort, etc. (not covered here). Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 33 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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. Selection sort algorithm: To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p l, swap a[p] and a[l]. 2. Terminate. left left+1 l–1 l right–1 right a Loop invariant: lesser values (sorted) greater values (unsorted) Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Selection sort (2) Animation: To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p l, swap a[p] and a[l]. 2. Terminate. left = 0 1 2 3 4 5 6 7 8 = right cat a fox cow pig dog cat fox goat rat lion pig rat dog pig tiger goat tiger l 8 7 6 5 4 3 2 1 0 p 7 8 1 3 5 Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 35 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Selection sort (3) 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) + … + 2 + 1 = (n–1) + … + 2 + 1 = (n – 1)n/2 = (n2 – n)/2 Time complexity is O(n2). Time complexity is quadratic time and it is sometime feasible (sometime work and sometime don’t work) Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 36 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Selection sort (4) 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; } } } Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 37 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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. Insertion sort algorithm: To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. left left+1 r–1 r right–1 right Loop invariant: a inserted (sorted) still to be inserted Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Insertion sort (2) Animation: To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. left = 0 1 2 3 4 5 6 7 8 = right fox cat a cow fox cow fox pig dog goat goat lion lion fox pig pig cat rat pig rat lion pig goat dog rat rat tiger tiger tiger r 9 8 7 6 5 4 3 2 1 Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 39 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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). Time complexity is quadratic time and is is sometime feasible. Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 40 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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. The divide-and-conquer strategy is effective for solving many problems. Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Merge-sort (2) Animation: To sort a[left…right] into ascending order: 1. If left < right: 1.1. Let m be an integer about midway between left and right. 1.2. Sort a[left…m] into ascending order. 1.3. Sort a[m+1…right] into ascending order. 1.4. Merge a[left…m] and a[m+1…right] into auxiliary array b. 1.5. Copy all components of b into a[left…right]. 2. Terminate. left = 0 1 2 3 4 5 6 7 8 = right a cat fox cow fox pig dog fox pig cat rat goat dog lion goat lion dog pig rat tiger goat tiger b cat cow dog fox goat lion pig rat tiger m 4 Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 42 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Merge-sort (3) 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. Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 43 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Merge-sort (4) 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). Time complexity is log linear time and it is still feasible. Space complexity is O(n), since step 1.4 needs an auxiliary array of length n. Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 44 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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. Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 45 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Quick-sort (2) Quick-sort algorithm: To sort a[left…right] into ascending order: 1. If left < right: 1.1. 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.2. Sort a[left…p–1] into ascending order. 1.3. Sort a[p+1…right] into ascending order. 2. Terminate. Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 46 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Quick-sort (3) Invariants: After left p–1 p p+1 right step 1.1: a less than or equal to pivot greater than or equal pivot (unsorted) to pivot (unsorted) After left p–1 p p+1 right step 1.3: a less than or equal to pivot greater than or equal pivot (sorted) to pivot (sorted) Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 47 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Quick-sort (4) Animation: To sort a[left…right] into ascending order: 1. If left < right: 1.1. 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.2. Sort a[left…p–1] into ascending order. 1.3. Sort a[p+1…right] into ascending order. 2. Terminate. left = 0 1 2 3 4 5 6 7 8 = right a cow cat fox cow cat pig dog cat fox goat pig rat rat lion lion goat goat pig rat dog tiger tiger tiger p 3 Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 48 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java 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. (not sort!) 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). Time complexity is log linear time and it is feasible. Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Quick-sort (6) 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). Time complexity is quadratic time and it is sometime feasible. The worst case arises if the array is already sorted! Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 50 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Quick-sort (7) 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); } } Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 51 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Quick-sort (8) 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: 2.1. If a[r] is less than pivot: 2.1.1. Copy a[r] into a[p], a[p+1] into a[r], and pivot into a[p+1]. 2.1.2. Increment p. 3. Terminate with answer p. Note that other (and better) partitioning algorithms exist. Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 52 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Quick-sort (9) Loop invariant: left p–1 p p+1 r–1 r right a pivot less than or equal to greater than or equal still to be pivot (unsorted) to pivot (unsorted) partitioned Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 53 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Quick-sort (10) 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; 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; } Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Comparison of sorting algorithms Algorithm No. of No. of Time Space comparisons copies complexity complexity Selection sort ~ n2/2 ~ 2n O(n2) O(1) Insertion sort ~ n2/4 ~ n2/4 O(n2) O(1) Merge-sort ~ n log2n ~ 2n log2n O(n log n) O(n) Quick-sort ~ n log2n ~ 2n/3 log2n O(n log n) O(log n) best ~ n2/2 0 O(n2) O(n) worst Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 55 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java The Java Vector Class (1) An object of class Vector is similar to an array in that it stores multiple values. However, a vector – only stores objects – does not have the indexing syntax that arrays have. – A vector can change its length. An array cannot i.e the length is fixed. The methods of the Vector class are used to interact with the elements of a vector. An important difference between an array and a vector is that a vector can be thought of as a dynamic, able to change its size as needed. Each vector initially has a certain amount of memory space reserved for storing elements. If an element is added that doesn't fit in the existing space, more room is automatically acquired Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 56 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java The Java Vector Class (2) The Vector class is part of the java.util package The Vector class is implemented using an array. Whenever new space is required, a new, larger array is created, and the values are copied from the original to the new array. To insert an element, existing elements are first copied, one by one, to another position in the array. Therefore, the implementation of Vector is not very efficient for inserting elements. The cat sat on the mouse Inserting an element into a Vector object The fat cat sat on the mouse Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 57 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java The Java Vector Class (3): Some Methods Vector //constructor void addElement(Object object) // adds the given object into end of this vector void insertElementAt(Object object, int index) // insert the given object into this vector at the given index object remove(int index) // remove the object at the given index from this vector void removeElement(Object object) // remove the given object from this vector int indexOf(Object object); // returns the index of a specific object, or –1 if the object is not found int size(); // returns the number of elements in this vector Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java The Java Vector Class (4): Example Vector units = new Vector(); units.addElement ("CSP1150"); units.addElement ("CSP1250"); units.addElement ("CSP2343"); units.addElement ("CSP3241"); System.out.println (units); units.removeElement ("CSP2343"); System.out.println (units); System.out.println ("At index 1: " + units.elementAt(1)); units.insertElementAt ("CSP2347", 2); System.out.println (units); System.out.println ("Size of the units:“ + units.size()); Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java The Java Vector Class (5): Results [CSP1150, CSP1250, CSP2343, CSP3241] [CSP1150, CSP1250, CSP3241] At index 1: CSP1250 [CSP1150, CSP1250, CSP2347, CSP3241] Size of the units: 4 Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 60 Faculty of Communications, Health and Science School of Computer and Information Science CSP1250 Data Structures with Java Week 3 Study Guide Reading: – Chapter 3: Java Collections by D.A. Watt and D.F. Brown (2001) – Appendix A4: Java Collections by D.A. Watt and D.F. Brown (2001) Workshop 3 – First Test on Algorithm Analysis Next week: – Linked-List Data Structures – Reading: Chapter 4: Java Collections by D.A. Watt and D.F. Brown (2001) Java Collections by D Watt & D Brown (2001) Edited and Modified by Dr W Guo (2003) 61