Professional Documents
Culture Documents
Topics
• Sequential Search on an Unordered File
• Sequential Search on an Ordered File
• Binary Search
• Bubble Sort
• Insertion Sort
Reading
• Sections 6.6 - 6.8
CMSC 104, Version 8/06 L24Searching&Sorting.ppt
Common Problems
• There are some very common problems that
we use computers to solve:
o Searching through a lot of records for a specific
record or set of records
o Placing records in order, which we call sorting
• There are numerous algorithms to perform
searches and sorts. We will briefly explore a
few common ones.
o Binary search
• Basic algorithm:
Get the search criterion (key)
Get the first record from the file
While ( (record != key) and (still more records) )
Get the next record
End_while
Is this fast ?
• 32 = 25 and 512 = 29
• 8 < 11 < 16 23 < 11 < 24
• 128 < 250 < 256 27 < 250 < 28
CMSC 104, Version 8/06 L24Searching&Sorting.ppt 1
A Very Fast Algorithm!
5 7
5 7
5 6 7
5 6 7 K
5 6 7 8 K
7 K Unsorted - shaded
Look at 2nd item - 5.
Compare 5 to 7.
1
7 5 5 is smaller, so move 5
to temp, leaving
v an empty slot in
7 5 position 2.
Move 7 into the empty
slot, leaving position 1
7
> open.
2
Compare to next - 6.
King is larger, so
leave 6 where it is.
Compare to next - 7.
King is larger, so
leave 7 where it is.
5 6 8 1
7 K
v
5 6 7 K 8
5 6 7 K
>
2
5 6 7 8 K 3
<