19.6.1 Selection Sort Implementation

Class SelectionSortTest (Fig. 19.4) contains:

static method selectionSort to sort an int array using the selection sort algorithm

static method swap to swap the values of two array elements

static method printPass to display the array contents after each pass, and

main to test method selectionSort.

As in the searching examples, main (lines 57–72) creates an array of ints named data and populates it with random ints in the range 1099. Line 68 tests method selectionSort.

 1   // Fig. 19.4: SelectionSortTest.java 2   // Sorting an array with selection sort. 3   import java.security.SecureRandom; 4   import java.util.Arrays; 5  6   public class SelectionSortTest 7   {

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.