UMBC CMSC202, Computer Science II, Spring 1998,
Sections 0101, 0102, 0103, 0104 and Honors
Thursday February 17, 1998
Assigned Reading:
- A Book on C:
- Programming Abstractions in C:
Handouts (available on-line):
Asymptotic Analysis
Topics Covered:
- Rehashed tweaking Merge Sort and Quicksort for
better performance. (See previous
lecture.) This only worked because insertion sort is "faster"
for small input sizes.
-
Sample runs on ordered and random data
show that the running times for Insertion Sort and
Quicksort varies greatly. Insertion Sort is fastest on an array
that is already sorted and slower than Selection Sort if the data
is sorted in the opposite order. Quicksort works well on random
data, but is much slower when the data is sorted in either order.
Selection Sort and Merge Sort on the other hand seem to run in
approximately the same amount of time regardless of the ordering
of the data. We used a new program to
generate files of ordered data. (Random data was generated previously.)
- To distinguish Quicksort and Merge Sort (which are quick)
from Selection Sort and Insertion Sort (which are slow), we use
asymptotic analysis. (See
online notes.)
In this approach, we say that the worst case running times of
Selection Sort and Insertion Sort are O(n2). The
worst case running time of Merge Sort is O(n log n) and the
average case running time of Quicksort is O(n log n). Note that
the Big Oh notation ignores the running times of small inputs and
also ignores constant factors. In this scheme, we lump Quicksort
and Merge Sort together as "fast" O(n log n) algorithms and we lump
Selection Sort and Insertion Sort together as "slow" O(n2)
algorithms.
- Next, we considered the difference between the running times
of linear search versus binary search. Our linear search program looks for 100,000
random integers in a sorted array of 800,000 random integers.
(Note that the number of times we find the random number in the
array corresponded to our predictions, which were based upon the
number of positive 4-byte integers.) The binary search program does the same
thing. However, the sample runs for linear
search and for binary search show that
binary search is much faster. In this case, we say that linear
search is a O(n) algorithm, whereas binary search is a O(log n)
algorithm.
Last Modified:
22 Jul 2024 11:27:45 EDT
by
Richard Chang
Back up
to Spring 1998 CMSC 202 Section Homepage