19
Recap
In general, for
n
items, it will take
n
! (
n
factorial) operations to
compute the result. So this is O(
n
!) time, or
factorial time
. It takes a
lot of operations for everything except the smallest numbers. Once
you’re dealing with 100+ cities, it’s impossible
to calculate the answer in
time—the Sun will collapse first.
This is a terrible algorithm! Opus should use a different one, right? But
he can’t. This is one of the unsolved problems in computer science.
There’s no fast known algorithm for it, and smart people think it’s
impossible
to have a smart algorithm for this problem. The best we can
do is come up
with an approximate solution; see chapter 10 for more.
One final note: if you’re an advanced reader, check out binary search
trees! There’s a brief description of them in the last chapter.
Recap
• Binary search is a lot faster than simple search.
• O(log
n
) is faster than O(
n
), but it gets a lot faster once the list of
items you’re searching through grows.
• Algorithm speed isn’t measured in seconds.
• Algorithm times
are measured in terms of
growth
of an algorithm.
• Algorithm times are written in Big O notation.
2
In this chapter
• You learn about arrays and linked lists—two of the
most basic data structures. They’re used absolutely
everywhere. You already used arrays in chapter 1,
and you’ll use them in almost
every chapter in this
book. Arrays are a crucial topic, so pay attention!
But sometimes it’s better to use a linked list instead
of an array. This chapter explains the pros and cons
of both so you can decide which one is right for
your algorithm.
• You learn your first sorting algorithm.
A lot of algo-
rithms only work if your data is sorted. Remember
binary search? You can run binary search only
on a sorted list of elements. This chapter teaches
you selection sort. Most
languages have a sorting
algorithm built in, so you’ll rarely need to write
your own version from scratch. But selection sort is
a
stepping stone to quicksort, which I’ll cover in the
next chapter. Quicksort is an important algorithm,
and it will be easier to understand if you know one
sorting algorithm already.
Dostları ilə paylaş: