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.

Post on 18-Dec-2015

220 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

Transcript

6-1

CM0551 Week 6The Array Data Structure

• Properties of arrays and subarrays – recap.

• Sorting: insertion, quick-sort and their

time and space complexity.

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];

length 0

hols 3Date[]

class tag 1 2

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 0 1

hols 3Date[]

class tag 2

1 12002Date

5 12001Date

y m d

12 252001Date

y m dclass tag

y m dclass tag

class tag

6-4

80 1 7

a2 3 4 5 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.

subarray a[6…9]subarray a[1…3]

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

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

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.

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.

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.

foxleft = 0 1 2 3 4 5 8 = right

cow pig cat rat lion tigera goat dog6 7

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.

foxleft = 0 1 2 3 4 5 8 = right

cow pig cat rat lion tiger

1

a

r

goat dog6 7

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.

cowleft = 0 1 2 3 4 5 8 = right

fox pig cat rat lion tiger

1

a

r

goat dog6 7

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.

cowleft = 0 1 2 3 4 5 8 = right

fox pig cat rat lion tiger

2

a

r

goat dog6 7

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.

cowleft = 0 1 2 3 4 5 8 = right

fox pig cat rat lion tiger

2

a

r

goat dog6 7

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.

cowleft = 0 1 2 3 4 5 8 = right

fox pig cat rat lion tiger

3

a

r

goat dog6 7

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.

catleft = 0 1 2 3 4 5 8 = right

cow fox pig rat lion tiger

3

a

r

goat dog6 7

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.

catleft = 0 1 2 3 4 5 8 = right

cow fox pig rat lion tiger

4

a

r

goat dog6 7

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.

catleft = 0 1 2 3 4 5 8 = right

cow fox pig rat lion tiger

4

a

r

goat dog6 7

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.

catleft = 0 1 2 3 4 5 8 = right

cow fox pig rat lion tiger

5

a

r

goat dog6 7

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.

catleft = 0 1 2 3 4 5 8 = right

cow fox lion pig rat tiger

5

a

r

goat dog6 7

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.

catleft = 0 1 2 3 4 5 8 = right

cow fox lion pig rat tiger

6

a

r

goat dog6 7

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.

catleft = 0 1 2 3 4 5 8 = right

cow fox lion pig rat tiger

6

a

r

goat dog6 7

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.

catleft = 0 1 2 3 4 5 8 = right

cow fox lion pig rat tiger

7

a

r

goat dog6 7

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.

7r

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.

7r

catleft = 0 1 2 3 4 5 8 = right

cow fox goat lion pig rata tiger dog6 7

catleft = 0 1 2 3 4 5 8 = right

cow fox goat lion pig rata tiger dog6 7

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.

catleft = 0 1 2 3 4 5 8 = right

cow fox goat lion pig rat

8

a

r

tiger dog6 7

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.

catleft = 0 1 2 3 4 5 8 = right

cow dog fox goat lion pig

8

a

r

rat tiger6 7

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.

catleft = 0 1 2 3 4 5 8 = right

cow dog fox goat lion pig

9

a

r

rat tiger6 7

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 = 0 1 2 3 4 5 8 = right

cow dog fox goat lion piga rat tiger

6 7

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= (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).

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.

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.

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 = 0 1 2 3 4 5 8 = right

cow pig cat rat lion tigera goat dog

6 7

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 = 0 1 2 3 4 5 8 = right

cow pig cat rat lion tigera goat dog

6 7

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 = 0 1 2 3 4 5 8 = right

cat dog fox pig rat liona tiger goat

6 7

3p

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 = 0 1 2 3 4 5 8 = right

cow dog fox pig rat liona tiger goat

6 7

3p

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 = 0 1 2 3 4 5 8 = right

cow dog fox goat lion piga rat tiger

6 7

3p

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 = 0 1 2 3 4 5 8 = right

cow dog fox goat lion piga rat tiger

6 7

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!)

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

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(n2).

The worst case arises if the array is already sorted!

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

}}

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.

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;

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;

}

6-21

Comparison of sorting algorithms

Algorithm No. of comparisons

Time complexity

Space complexity

Selection sort ~ n2/2 O(n2) O(1)

Insertion sort ~ n2/4 O(n2) O(1)

Merge-sort ~ n log2n O(n log n) O(n)

Quick-sort ~ n log2n~ n2/2

O(n log n)O(n2)

O(log n)O(n)

best worst

top related