Sorting an Array

When you have grouped a bunch of similar items together into an array, one thing you can do is rearrange items. The following statements swap the values of two elements in an integer array called numbers:

int temp = numbers[7];
numbers[5] = numbers[6];
numbers[6] = temp;

These statements result in numbers[5] and numbers[6] trading values with each other. The integer variable called temp is used as a temporary storage place for one of the values being swapped. Sorting is the process of arranging a list of related items into a set order, such as when a list of numbers is sorted from lowest to highest.

Santa Claus could use sorting to arrange the order of gift recipients by last name with Willie Aames and Hank Aaron raking in their Yuletide plunder much earlier than alphabetical unfortunates Dweezil Zappa and Jim Zorn.

Sorting an array is easy in Java because the Arrays class does all of the work. Arrays, which is part of the java.util group of classes, can rearrange arrays of all variable types.

To use the Arrays class in a program, use the following steps:

1. Use the import java.util.* statement to make all the java.util classes available in the program.

2. Create the array.

3. Use the sort() method of the Arrays class to rearrange an array.

An array of variables that is sorted by the Arrays class are rearranged into ascending numerical order. Characters and strings are arranged in alphabetical order.

To see this in action, create a new Empty Java File named Name and enter the text of Listing 9.2, a short program that sorts names, in the source editor.

Listing 9.2. The Full Source Code of Name.java


 1: import java.util.*;
 2:
 3: class Name {
 4:     public static void main(String[] args) {
 5:         String names[] = { "Lauren", "Audrina", "Heidi", "Whitney",
 6:             "Stephanie", "Spencer", "Lisa", "Brody", "Frankie",
 7:             "Holly", "Jordan", "Brian", "Jason" };
 8:         System.out.println("The original order:");
 9:         for (int i = 0; i < names.length; i++) {
10:             System.out.print(i + ": " + names[i] + " ");
11:         }
12:         Arrays.sort(names);
13:         System.out.println(" The new order:");
14:         for (int i = 0; i < names.length; i++) {
15:             System.out.print(i + ": " + names[i] + " ");
16:         }
17:         System.out.println();
18:     }
19: }


When you run this Java program, it displays a list of 13 names in their original order, sorts the names, and then redisplays the list. Here’s the output:

Output


The original order:
0: Lauren 1: Audrina 2: Heidi 3: Whitney 4: Stephanie 5: Spencer
6: Lisa 7: Brody 8: Frankie 9: Holly 10: Jordan 11: Brian
12: Jason
The new order:
0: Audrina 1: Brian 2: Brody 3: Frankie 4: Heidi 5: Holly
6: Jason 7: Jordan 8: Lauren 9: Lisa 10: Spencer 11: Stephanie 12: Whitney


When you’re working with strings and the basic types of variables such as integers and floating-point numbers, you only can sort them by ascending order using the Arrays class. You can write code to do your own sorts by hand if you desire a different arrangement of elements during a sort, or you want better efficiency than the Arrays class provides.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.81.173