Java Reference
In-Depth Information
As soon as i reaches 10, an ArrayIndexOutOfBoundsException is generated and the pro-
gram is terminated.
Try This 5-1 Sorting an Array
Because a one-dimensional array organizes data into an indexable linear list, it is the perfect
data structure for sorting. In this project you will learn a simple way to sort an array. As
you may know, there are a number of different sorting algorithms. There are the quick sort,
the shaker sort, and the shell sort, to name just three. However, the best known, simplest,
and easiest to understand is called the Bubble sort. Although the Bubble sort is not very
efficient—in fact, its performance is unacceptable for sorting large arrays—it may be used
effectively for sorting small arrays.
1. Create a file called Bubble.java .
2. The Bubble sort gets its name from the way it performs the sorting operation. It uses
the repeated comparison and, if necessary, exchange of adjacent elements in the ar-
ray. In this process, small values move toward one end and large ones toward the oth-
er end. The process is conceptually similar to bubbles finding their own level in a
tank of water. The Bubble sort operates by making several passes through the array,
exchanging out-of-place elements when necessary. The number of passes required to
ensure that the array is sorted is equal to one less than the number of elements in the
array.
Here is the code that forms the core of the Bubble sort. The array being sorted is
called nums .
 
Search WWH ::




Custom Search