left p

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
3-1
Properties of arrays in general
• An array is a sequence of indexed components:
low
a
low+1 low+2
high–2 high–1
high
array
indices
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-2
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
a
T[]
0
1
n–2
n–1
n
array components
3-3
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
class tag length
int[]
6
0
1
2
3
4
5
2
3
5
7
11
13
3-4
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];
hols
class tag length
Date[]
3
0
1
2
3-5
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);
hols
class tag length
Date[]
0
1
2
3
class tag
y
m
d
Date
2001
12
25
y
m
d
2001
5
1
y
m
d
2002
1
1
class tag
Date
class tag
Date
3-6
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.
3-7
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”.
3-8
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”.
3-9
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.
3-10
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.
left = 0
a The
val
fat
1
2
3
cat
fat
cat
sat
sat
on
ins
4
5
6 = right
the
on mouse
the mouse
mat
1
3-11
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)/2 + 1
= n/2 + 1/2
Time complexity is O(n).
3-12
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.
3-13
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.
left = 0
a The
del
1
2
3
cat
fat
cat
sat
sat
on
4
5
6 = right
the
on mouse
mousemouse
the mouse
1
3-14
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).
3-15
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).
3-16
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. 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
3-17
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).
3-18
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
a
rat
1
2
3
4
5
cat
pig
cow
fox
lion
target cow
p
6
7
tiger goat
8 = right
dog
3210
3-19
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
a
rat
1
cat
target dog
2 = right
pig
p
3210
3-20
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).
3-21
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
}
3-22
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!
3-23
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.
3-24
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.
3-25
Binary search (3)
• Loop invariant:
left
l–1
l
r
r+1
right
a
known to be
less than target
still to be searched
known to be
greater than target
3-26
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:
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
a
1
2
3
4
5
6
7
8 = right
cow
dog
fox
goat
lion
pig
rat
tiger
target lion
l
cat
50
r
58
m
564
3-27
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:
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
a
1
2
3
4
5
6
7
8 = right
cow
dog
fox
goat
lion
pig
rat
tiger
target dingo
l
cat
20
r
138
m
214
3-28
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).
3-29
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;
}
3-30
Comparison of searching algorithms
Algorithm
No. of
comparisons
Time
complexity
Linear search
(unsorted array)
~ n/2 successful O(n)
n
unsuccessful
Linear search
(sorted array)
~ n/2
O(n)
Binary search
~ log2 n
O(log n)
3-31
Merging (1)
• Problem: Given two sorted arrays, make a third sorted
array containing copies of all components of the two
original two arrays.
3-32
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 = 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.
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].
4. If j  r2, copy a2[j…r2] into a3[k…r3].
5. Terminate.
3-33
Merging (3)
• Loop invariant:
l1
i–1
i
r1
a1
already merged
l2
j–1
still to be merged
j
r2
a2
already merged
l3
still to be merged
k–1
k
r3
a3
copied from a1 and/or a2
unoccupied
3-34
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 = 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.
a1[i] into a3[k],
then increment i and k.
3. If i  r1,
copyCopy
a1[i…r1]
a3[k…r3].
2.2.
If a1[i]
greater than
equal to a2[j]:
4. If
j  r2,
copyisa2[j…r2]
intoora3[k…r3].
5. Terminate.
2.2.1. Copy a2[j] into a3[k], then increment j and k.
…
l1 = 0
a1 cow
l2 = 0
a2
cat
l3 = 0
a3
cat
1 = r1
goat
1
2
3
4 = r2
dog
fox
lion
tiger
1
2
3
4
5
6
cow
dog
fox
goat
lion
tiger
i
210
j
3210
k
543210
3-35
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).
3-36
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).
3-37
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++];
}
3-38
Merging (8)
• Implementation (continued):
while (i <= r1)
while (j <= r2)
a3[k++] = a1[i++];
a3[k++] = a2[j++];
}
3-39
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.
3-40
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
1+2+3 = 6
n = 4
n(n + 1)/2 = 45/2 = 10
1 + 2 + 3 + 4 = 10
3-41
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).
3-42
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.
3-43
Selection sort (2)
• 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.
• Loop invariant:
left
left+1
l–1
l
right–1
right
a
lesser values (sorted)
greater values (unsorted)
3-44
Selection sort (3)
• 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
a fox
cat
l
0
1
2
3
4
5
6
7
8
1
2
3
4
5
cow
dog
pig
fox
cat
goat
rat
lion
p
6
7
8 = right
tiger
pig goat
rat tiger
dog
pig
1
3
5
8
7
3-45
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) + … + 2 + 1
(n–1) + … + 2 + 1
(n – 1)n/2
(n2 – n)/2
Time complexity is O(n2).
3-46
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; }
}
}
3-47
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.
3-48
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.
• Loop invariant:
left
left+1
r–1
r
right–1
right
a
inserted (sorted)
still to be inserted
3-49
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.
left = 0
a cow
fox
cat
r
1
2
cow
fox
dog
fox
pig
3
4
goat
lion
fox
pig
cat goat
lion
pig
rat
5
lion
pig
rat
6
7
8 = right
tiger
pig
rat tiger
goat
rat tiger
dog
1
2
3
4
5
6
7
8
9
3-50
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).
3-51
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.
3-52
Merge-sort (2)
• Merge-sort algorithm:
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.
3-53
Merge-sort (3)
• 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
fox
cat
cow
dog
fox
pig
fox
pig
cat
goat
rat
lion
dog
tiger
goat
pig goat
lion
rat tiger
dog
b
cat
cow
dog
fox
goat
lion
pig
rat
tiger m
4
3-54
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.
3-55
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.
3-56
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.
3-57
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.
3-58
Quick-sort (3)
• Invariants:
After
step 1.1:
left
p–1
p+1
right
a
less than or equal to
pivot (unsorted)
After
step 1.3:
p
left
p–1
pivot greater than or equal
to pivot (unsorted)
p
p+1
right
a
less than or equal to
pivot (sorted)
pivot greater than or equal
to pivot (sorted)
3-59
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
a cow
fox
cat
cow
cat
dog
pig
fox
cat
goat
pig
rat
lion
rat
6
7
8 = right
tiger
lion
pig tiger
goat
rat tiger
goat
dog
p
3
3-60
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!)
3-61
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
comps(n) = 0
if n > 1
if n  1
Solution:
comps(n)  n log2n
Best-case time complexity is O(n log n).
3-62
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!
3-63
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);
}
}
3-64
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:
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.
3-65
Quick-sort (10)
• Loop invariant:
left
p–1
p
p+1
r–1
r
right
a
less than or equal to
pivot (unsorted)
pivot
greater than or equal
to pivot (unsorted)
still to be
partitioned
3-66
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;
3-67
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;
}
3-68
Comparison of sorting algorithms
Algorithm
No. of
No. of
comparisons copies
Selection sort ~ n2/2
Time
Space
complexity complexity
~ 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
~ n2/2
~ 2n/3 log2n O(n log n)
0
O(n2)
O(log n)
O(n)
best
worst
3-69