Download presentation
Presentation is loading. Please wait.
Published byChastity Campbell Modified over 9 years ago
1
6-1 CM0551 Week 6 The Array Data Structure Properties of arrays and subarrays – recap. Sorting: insertion, quick-sort and their time and space complexity.
2
6-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]; length0 hols 3 Date[] class tag12
3
6-3 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); length 01 hols 3 Date[] class tag 2 112002 Date 512001 Date y md 12252001 Date y md class tag y md
4
6-4 8017 a 234569 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. subarray a[6…9]subarray a[1…3]
5
6-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
6-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: selection sort (done in programming 2) insertion sort merge-sort (not covered here) quick-sort shell-sort, radix sort, etc. (not covered here).
7
6-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
6-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
6-9 Insertion sort (3) 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. fox left = 0123458 = right cowpigcatratliontiger a goatdog 67 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. fox left = 0123458 = right cowpigcatratliontiger 1 a r goatdog 67 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. cow left = 0123458 = right foxpigcatratliontiger 1 a r goatdog 67 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. cow left = 0123458 = right foxpigcatratliontiger 2 a r goatdog 67 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. cow left = 0123458 = right foxpigcatratliontiger 2 a r goatdog 67 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. cow left = 0123458 = right foxpigcatratliontiger 3 a r goatdog 67 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. cat left = 0123458 = right cowfoxpigratliontiger 3 a r goatdog 67 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. cat left = 0123458 = right cowfoxpigratliontiger 4 a r goatdog 67 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. cat left = 0123458 = right cowfoxpigratliontiger 4 a r goatdog 67 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. cat left = 0123458 = right cowfoxpigratliontiger 5 a r goatdog 67 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. cat left = 0123458 = right cowfoxlionpigrattiger 5 a r goatdog 67 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. cat left = 0123458 = right cowfoxlionpigrattiger 6 a r goatdog 67 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. cat left = 0123458 = right cowfoxlionpigrattiger 6 a r goatdog 67 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. cat left = 0123458 = right cowfoxlionpigrattiger 7 a r goatdog 67 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. 7 r 7 r cat left = 0123458 = right cowfoxgoatlionpigrat a tigerdog 67 cat left = 0123458 = right cowfoxgoatlionpigrat a tigerdog 67 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. cat left = 0123458 = right cowfoxgoatlionpigrat 8 a r tigerdog 67 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. cat left = 0123458 = right cowdogfoxgoatlionpig 8 a r rattiger 67 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. cat left = 0123458 = right cowdogfoxgoatlionpig 9 a r rattiger 67 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. cat left = 0123458 = right cowdogfoxgoatlionpig a rattiger 67
10
6-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 = (n 2 + n – 2)/4 Time complexity is O(n 2 ). 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(n 2 ).
11
6-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
6-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
6-13 Quick-sort (Animation) (3) 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. fox left = 0123458 = right cowpigcatratliontiger a goatdog 67 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. fox left = 0123458 = right cowpigcatratliontiger a goatdog 67 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. cow left = 0123458 = right catdogfoxpigratlion a tigergoat 67 3 p 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. cat left = 0123458 = right cowdogfoxpigratlion a tigergoat 67 3 p 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. cat left = 0123458 = right cowdogfoxgoatlionpig a rattiger 67 3 p 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. cat left = 0123458 = right cowdogfoxgoatlionpig a rattiger 67
14
6-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
6-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
6-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(n 2 ). The worst case arises if the array is already sorted !
17
6-17 Quick-sort (6) 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); } }
18
6-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
6-19 Quick-sort (8) 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;
20
6-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
6-21 Comparison of sorting algorithms AlgorithmNo. of comparisons Time complexity Space complexity Selection sort~ n 2 /2O(n2)O(n2)O(1) Insertion sort~ n 2 /4O(n2)O(n2)O(1) Merge-sort~ n log 2 nO(n log n)O(n)O(n) Quick-sort~ n log 2 n ~ n 2 /2 O(n log n) O(n 2 ) O(log n) O(n) best worst
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.