15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

394<br />

Using Bubble Sort<br />

Speed measures how fast the algorithm can sort data of different sizes. Some<br />

sorting algorithms work quickly with small lists but slow down dramatically<br />

when dealing with larger lists. Other sorting algorithms work quickly when a<br />

list is mostly sorted but slow to a crawl when working with completely<br />

unsorted lists.<br />

Memory requirements define how much memory the sorting algorithm needs<br />

to run. Some sorting algorithms can accept and sort data while they receive<br />

the data, which is an online algorithm. (An offline algorithm has to wait to<br />

receive the complete set of data be<strong>for</strong>e it can even start the sorting<br />

process.) Sorting algorithms that use recursion (see Book II, Chapter 6) may<br />

be easy to implement and fast but can require lots of memory to sort large<br />

amounts of data.<br />

Another factor that determines a sorting algorithm’s memory requirements is<br />

whether the algorithm is an in-place algorithm. An in-place algorithm can sort<br />

data by using the existing data structure that holds the data. For example, if<br />

you have an array of numbers, an in-place algorithm can sort the data in that<br />

array without needing a separate data structure to hold the data temporarily.<br />

Stability refers to whether a sorting algorithm preserves the order of identical<br />

data. Suppose you had a list of first and last names, such as John Smith<br />

and Mary Smith, and the name John Smith appears at the beginning of a list<br />

and Mary Smith appears at the end. A stable sorting algorithm sorts both<br />

names and keeps John Smith ahead of Mary Smith, but an unstable algorithm<br />

moves Mary Smith ahead of John Smith. (Out of all the sorting algorithms<br />

presented in this chapter, heap sort is the only unstable algorithm.)<br />

Ultimately, there’s no perfect sorting algorithm; however, a trade-off in size,<br />

speed, and ease of implementation finds the best sorting algorithm.<br />

Using Bubble Sort<br />

The simplest way to sort any amount of data is to start at the beginning and<br />

compare the first two adjacent items. So if you need to sort a list of numbers,<br />

you compare the first number with the second number. If the first<br />

number is bigger, you swap its place with the second number. If the second<br />

number is bigger, you don’t do anything at all.<br />

After comparing the first two items, you move down to comparing the<br />

second and third items, and so on. Repetitively comparing two adjacent<br />

items is the basic idea behind the bubble sort algorithm, as shown in<br />

Figure 1-1. The bubble sort algorithm gets its name because small values<br />

tend to bubble up to the top.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!