Professional Documents
Culture Documents
• Started Quicksort.
int max = 0;
for (int i = 1; i < A.length; i++)
if (A[i] > A[max]) max = i;
swap(A, A.length-1, max);
quickSort(A, 0, A.length-2);
} // end quickSort(array)
pivot
6 5 4 7 8 1 6 3 8 10 12
lower upper
pivot
6 5 4 3 8 1 6 7 8 10 12
lower upper
Winter 2006 CISC121 - Prof. McLeod 6
• Make the swap, and then upper and lower pass each
other:
pivot
6 5 4 3 6 1 lower
upper 8 7 8 10 12
• Put pivot value back in, and divide into two subarrays:
1 5 4 3 6 6 8 7 8 10 12
• (Note how old pivot and max value are already in correct
positions and do not need to be included in next recursive
call).
pivot pivot
4 5 1 3 6 6 7 8 8 10 12
1 3 4 5 6 6 7 8 8 10 12
• Array is sorted:
1 3 4 5 6 6 7 8 8 10 12
Winter 2006 CISC121 - Prof. McLeod 8
Quicksort – Cont.
• Note that new arrays are not created, only the
bounds of the subarrays in the array A are
changed. The recursive calls will only contain
different bounding values. And the subarrays get
smaller for each call.
• The anchor case is when the size of the subarray
is one.
Collections.sort 250 ? ?
(Vectors)
Winter 2006 CISC121 - Prof. McLeod 16
Number Representation
• Binary numbers or “base 2” is a natural
representation of numbers to a computer.
• As a transition, hexadecimal (or “hex”) numbers
are also used.
• Octal (base 8) numbers are used to a lesser
degree.
• Decimal (base 10) numbers are *not* naturally
represented in computers.
• For example:
(127.4)8 = 1×82+2×81+7×80+4×8-1=87.5.
• For example:
(1001110001.110000101...)2 ...
Winter 2006 CISC121 - Prof. McLeod 25
Aside - Roundoff Error
• From the previous example, you can see that
exact base 10 decimals cannot always be exactly
represented in binary.
• For example 0.110 is:
0.0001100110011001100110011… in base 2.
(010110001101011.111100000110)2=(?)8
(2C6B.F06)16=(?)2
(2 C 6 B . F 0 6)16
( 0010 1100 0110 1011 . 1111 0000 0110)2
10010101 is
1x20 + 1x22 + 1x24 - 1x27
= 1 + 4 + 16 - 128
= -107 base 10
mantissa exponent
http://grouper.ieee.org/groups/754/
float sum = 0;
for (int i = 0; i < 10000; i++)
sum += 0.1;
System.out.println(sum);
Winter 2006 CISC121 - Prof. McLeod 43
Storage of “Real” or “Floating-Point”
Numbers - Cont.
• Prints a value of 999.9029 to the screen.
• If sum is declared to be a double then the
value: 1000.0000000001588 is printed to the
screen.
• So, the individual roundoff errors have piled up to
contribute to a cumulative error in this
calculation.
• As expected, the roundoff error is smaller for a
double than for a float.
f ( x ) 1 cos( x )
for example. As x approaches zero, cos(x)
approaches 1.
b b 4ac 2
x1
2a
• What happens when ac is small, compared to b?
• It is known that for the two roots, x1 and x2:
c
x1 x2
a
Winter 2006 CISC121 - Prof. McLeod 49
The Effects of Roundoff Error - Cont.
• Which leads to an equation for the root which is
not as susceptible to roundoff error:
2c
x1
b b 4ac 2
( 1)i 1 x i
ln(1 x )
i 1 i
• For ln(2):
( 1)i 1 1 1 1
ln(2) 1 ...
i 1 i 2 3 4
Winter 2006 CISC121 - Prof. McLeod 53
The Effect on Summations
• Since we cannot loop to infinity, how many terms
would be sufficient?
• Since the sum is stored in a finite memory space,
at some point the terms to be added will be much
smaller than the sum itself.
• If the sum is stored in a float, which has about 7
significant digits, a term of about 1x10-8 would not
be significant. So, i would be about 108 - that’s a
lot of iterations!
1 x
1
2
2 i 1
ln x
1 x i 0 2i 1