Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

Chapter 15. SORTING, SEARCHING, AND


RANDOM NUMBERS


Sorting


Bubble Sort.........................................................................................................


Quicksort.............................................................................................................


Built-In Sorting Functions


Sorting with a Comparison Function


Searching


Indexing


Random Numbers


Random Identifiers


Choosing Banner Ads


Sorting and searching are two fundamental concepts of computer science. They are
closely tied to almost every application: databases, compilers, even the World Wide Web.
The more information you have online, the more important it becomes to know exactly
where that information is.


Admittedly, sorting is not as serious a topic in the context of PHP as it is for C++. PHP
offers some very powerful sorting functions, even one that allows you to define how to
compare two elements. Since this chapter deals with some classic problems of computer
science, you may be interested in learning about the concepts that become useful as you
use more generalized languages like C or Ada. But further than that, these concepts will
help you understand the internal workings of databases, Web servers, even PHP itself.
You will be more capable of dealing with the inevitable problem unsolved by any built-in
PHP function.


This chapter also discusses random numbers, which are useful for putting data out of
order. The practical application of this usually takes the form of unique identifiers, for
files or sessions.


Sorting


To sort means to put a set of like items into order. The rules of ordering can be simple,
such as strings sorted by the order of the alphabet. They could be complex, such as
sorting addresses first by country, then by state, then by city. The process of sorting can

Free download pdf