The Quickest Way to Sort an Array in Java
In one of my last articles, I showed how to used an insertion sort that allowed you to traverse an array and sort the data that was stored in that array. There are also other sorting methods like the bubble sort and the quicksort that also are good algorithms for sorting arrays.
However, there are two main problems with these different types of algorithms. The first problem is that you have to create them and if there are any bugs (that is, they don’t work properly), you have to debug them and that could take some time. The second problem is that they can take up a lot of CPU time and could be very limited if you have arrays that contain more than a thousand elements.
However, there is an easy way to sort an array in Java and that is to make a call to Java’s Arrays class sorting function. It is very easy and only takes one line of code to create a new sorted array. In this article, I will show you an example in code on how to make a call to this internal class.
First, a Little Description on What Exactly is an Array.
An array is a variable that you can create that can store a collection of data. Basically, instead of declaring a variable that can only hold one piece of data in memory, an array can hold a whole collection of data in memory as long as all the data is the same type (i.e. a string, integer, double, etc.) Indeed, you can even create an array that can hold a whole collection of objects.
When you declare an array in Java, you declare it slightly different than a regular variable. For example, if you want to declare regular integer variable you would declare it like the following:
int myInteger.
However, since arrays are a collection of data, you have to declare them using brackets (i.e. []). With that said, the following is how you would create an array of integers with 21 records:
int [20] myIntegerArray.
So, with the above declaration, you would create a new array that can contain up to 21 records (note: 0 is considered the first record in an array), and each record would contain one integer. However, if you don’t know how many elements (records) you will need you could also declare an array with just open brackets which would allow you to create an array that can take in an infinite number of records. The following is an open bracket declaration of the above mentioned array:
int [] myIntegerArray.
Now For the Code:
Before I show the code I used for this article, I just wanted to tell you that the IDE (compiler) I used for this example is NetBeans 7.2.1 which is a free download at their website. All I did is to create a regular Java application project and named the class SorthingArrays.
With that stated, the code I am about to show you declares two different arrays, one is a String array and the other is a double. It then populates the arrays with data and makes a call to the Arrays class sort method. So, here is the code:
//
// Program sortingarrays: This program demostrates how to
// use Java’s Arrays class to sort data within an array.
//************************************************************
package sortingarrays;
/**
*
* @author Bink
* version: 1.0
*/
import java.util.Arrays;
public class SortingArrays {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Create a string Array and populate it with some data
String[] classNames = {“Biology”, “Math”, “Chemistry”, “History”};
//Makes a call to Array’s sort class to sort the array
Arrays.sort(classNames);
//Print out to the output window the sorted array
System.out.println(Arrays.toString(classNames));
//Create a double array called grades
double[] grades = {2.34, 3.77, 4.0, 2.98, 3.00, 2.99};
//Make another call to Array’a sort function
Arrays.sort(grades);
//Print out the results
System.out.println(Arrays.toString(grades));
}//end main
}//end class
To use the Arrays class, I imported the class via java.util.Arrays. Then in the main function, I declared a String array, populated it, and then made a call to the Arrays sort function. I then created a new double array, populated it, and sorted it as well. When you run the above program, you will get the following in your output window:
Output Window After the Program Runs
Conclusion
Conclusion:
So, that is it! By using the Arrays class sort function, all you have to do is to make the call and don’t have to worry too much about having to create your own algorithm to sort and array. HAPPY CODING!!!!