1 / 21

CM0551 Week 6 The Array Data Structure

CM0551 Week 6 The Array Data Structure. Properties of arrays and subarrays – recap. Sorting: insertion, quick-sort and their time and space complexity. class tag. length. 0. 1. 2. hols. Date[]. 3. Java object array (1). Suppose that a Date object has fields y , m , d .

joben
Download Presentation

CM0551 Week 6 The Array Data Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CM0551 Week 6The Array Data Structure • Properties of arrays and subarrays – recap. • Sorting: insertion, quick-sort and their time and space complexity.

  2. class tag length 0 1 2 hols Date[] 3 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];

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

  4. 0 1 2 3 4 5 6 7 8 9 a subarray a[1…3] subarray a[6…9] 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. • Length of subarray a[l…r] is r– l + 1.

  5. 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”.

  6. 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: • selectionsort(done in programming 2) • insertionsort • merge-sort(not covered here) • quick-sort • shell-sort, radix sort, etc. (not covered here).

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

  8. Insertion sort (2) • 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.

  9. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 left = 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right a a a a a a a a a a a a a a a a a a a a cat fox cat cat cat cat cat cat cat cat cat cat fox cat cat cow cow cat cow cow fox cow cow cow cow fox cow cow cow cow fox cow cow cow cow fox cow cow cow cow fox pig dog fox fox pig fox fox fox fox dog fox dog fox pig pig pig pig fox fox cat lion cat lion fox goat cat pig pig pig goat fox goat fox cat cat cat lion lion pig rat lion pig rat lion rat rat pig rat rat goat lion rat goat pig pig goat rat rat rat rat lion lion lion lion lion lion rat lion lion pig lion lion pig lion pig lion lion rat rat tiger rat tiger tiger tiger pig tiger tiger tiger pig rat tiger tiger tiger tiger rat pig tiger tiger tiger goat goat goat goat rat goat tiger rat goat goat goat tiger goat goat goat tiger rat goat goat goat dog dog dog dog dog dog dog dog dog dog dog dog dog dog dog tiger tiger dog tiger dog r r r r r r r r r r r r r r r r r r 7 7 2 5 8 8 7 1 6 3 1 6 9 4 4 5 3 2 Insertion sort (3) • Animation:

  10. 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). • Think of this as visiting each array element and then visiting them all again to insert the element in the right position (that’s n x n) and hence it’s time complexity is of O(n2).

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

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

  13. 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. 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. 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. 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. 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. 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 left = 0 left = 0 left = 0 left = 0 left = 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 = right 8 = right 8 = right 8 = right 8 = right 8 = right a a a a a a fox cat cat cat cow fox cow cat cow cow cow cow dog dog pig dog pig dog fox fox cat fox cat fox pig goat rat rat pig goat rat lion rat lion lion lion tiger lion pig tiger lion pig tiger rat goat goat tiger rat tiger dog goat tiger goat dog p p p 3 3 3 Quick-sort (Animation)(3)

  14. Quick-sort (4) • Analysis (counting comparisons): Let n be the no. of values to be sorted. Step 1.1 takes about n–1 comparisons to partition the array. (NB: partition, not sort!)

  15. Quick-sort (5) • In the best case, the pivot turns out to be the middle value in the array. So the left and right subarrays both have length about n/2. So we have half the problem and we repeat this again … Divide and conquer again, so: Best-case time complexity is O(n log n).

  16. Quick-sort (6) • In the worst case, the pivot turns out to be the smallest value. So we only reduce the array by one each time just as in the case of insertion sort, so; Worst-case time complexity is O(n2). The worst case arises if the array is already sorted!

  17. Quick-sort (6) • Implementation in Java: staticvoid 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); }}

  18. Quick-sort (7) • 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.

  19. Quick-sort (8) • Implementation in Java: staticint 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;

  20. Quick-sort (9) • 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;}

  21. Comparison of sorting algorithms

More Related