CS100J 31 October 2006 Arrays: searching - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

CS100J 31 October 2006 Arrays: searching

Description:

Haikus (5-7-5) seen on Japanese computer monitors. Do exercises on pp. 311-312 to get ... In the 1960s and early 1970s, a program was punched on such cards. ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 9
Provided by: csCor
Category:

less

Transcript and Presenter's Notes

Title: CS100J 31 October 2006 Arrays: searching


1
CS100J 01 April 2008More on arrays Sorting
insertion- selection- quick- sort
Do exercises on pp. 311-312 to get familiar with
concepts and develop skill. Practice in DrJava!
Test your methods!
Haikus (5-7-5) seen on Japanese computer monitors
Yesterday it worked.Today it is not
working.Windows is like that. A crash
reducesYour expensive computerTo a simple
stone. Three things are certainDeath, taxes and
lost data.Guess which has occurred?
Serious error.All shortcuts have
disappeared.Screen. Mind. Both are blank. The
Web site you seek Cannot be located,
butCountless more exist. Chaos reigns
within.Reflect, repent, and reboot.Order shall
return.
2
Reversing array segment bh..k
b
h
k
pre
not reversed
h j
k
post
b
reversed
change into
3
Remove adjacent duplicates
0
n
change into
1 2 2 4 2 2 7 8 9 9 9 9
b
0 h n
dont care what is in bk1..n
1 2 4 2 7 8 9 8 9 9 9 9
b
postcondition b0..h initial values in
b0..n but with adj dups removed
4
sorted means in ascending order
Sorting
for (int i 0 i lt n i i1)
Push bi down into its sorted position in
b0..i
Iteration i makes up to i swaps. In worst case,
number of swaps needed is 1 2 3 (n-1)
(n-1)n / 2. Called an n-squared, or n2,
algorithm.
5
Add property to invariant first segment contains
smaller values.
for (int i 0 i lt n i i1)
7
int j index of min of bi..n-1 Swap bj and
bi
Also an n-squared, or n2, algorithm.
6
Quicksort
/ Sort bh..k / public static void
qsort(int b, int h, int k)
if (bh..k has fewer than 2 elements)
return
To sort array of size n. e.g. 215 Worst case n2
e.g. 230 Average case n
log n. e.g. 15 215 215 32768
int j partition(b, h, k) // bh..j1 lt x lt
bj1..k
// Sort bh..j1 and bj1..k
qsort(b, h, j1) qsort(b, j1, k)
j partition(b, h, k)
7
Sir Tony Hoare, Fritz Bauer
Quicksort author My advisor
Pioneer in computing in the 50's, 60's. Developer
of the historical computing section of the
Deutsches Museum
Marienplatz, Munich
8
Keypunch machine. In the 1960s and early 1970s, a
program was punched on such cards. A card deck
containing a program to be run would be taken out
to the computer, near the Ithaca airport, and
run. Get output back 3-4 hours after handing in
the card deck.
Zuse built the first working computer in the late
'30s, the Z3, completed in 1941. The original
machine no longer exists.
Konrad Zuse's Z4
Write a Comment
User Comments (0)
About PowerShow.com