Best writers. Best papers. Let professionals take care of your academic papers

Order a similar paper and get 15% discount on your first order with us
Use the following coupon "FIRST15"
ORDER NOW

How to Sort an Array in Java

In this lesson, we will learn how sorting works in Java, the importance of the sorting algorithms and the variables to consider when using sorting to manipulate arrays.

Arrays

You’ve probably worked with arrays before, those created by adding a couple of brackets [] to a variable in the code that will let us store multiple items in list kind of way. You can see this in the code below:

  1. int [] array = {1,2,3,4,5};
  2. int array [] = {1,2,3,4,5};

Both these are valid ways to have arrays in Java.

Now we’re going to explore a new library, java.util.Arrays, that will give us access to more methods to extend what we can do with arrays. But before we do that, we’re going to go over how to sort an array manually since it’s one of the most common operations done on arrays.

Sorting

Sorting is one of the most common operations done on arrays. We’re going to perform a manual sort so we can fully understand what this operation does with our information.

There are multiple sorting algorithms that we can use depending on our needs. This determines how sorted the data is in an array or the number of unique values you can have on it. Let’s look at an example of an insertion-sort. This sorting algorithm is a basic one, very similar to what you do when shuffling the cards in your hand. It will take one item in the array and find the position it should go in according to the sorting criteria. Let’s say we have a random array and we want to sort it in ascending order. You can see here:

  1. public class InsertSort {
  2.   public static void main (String [] args) {
  3.    int [] array = {45,12,85,32,89,39,69,44,42,1,6,8};
  4.    int temp;
  5.    for (int i = 1; i < array.length; i++) {
  6.     for (int j = i; j > 0; j–) {
  7.      if (array[j] < array [j 1]) {
  8.       temp = array[j];
  9.       array[j] = array[j 1];
  10.       array[j 1] = temp;
  11.      }
  12.     }
  13.    }
  14.    for (int i = 0; i < array.length; i++) {
  15.      System.out.println(array[i]);
  16.    }
  17.   }
  18. }

This code will print item by item in the array in ascending order by finding the right spot for each item without having to go through all of the array twice with every single item. It just compares from left to right checking if the number before the one to be analyzed is less, then it switches positions and repeats that process with every item in the array.

There are multiple sorting algorithms that can be used, but this one is simple to remember and quite efficient for its low complexity.

java.util.Arrays

Now it’s time to do some more exploring into arrays. java.util.Arrays is a library included in Java programming language that will let us perform more operations on arrays, giving us access to more than 100 methods. We won’t go through all of them in this lesson, but we will cover the most important ones.

In the java.util.Arrays library we’ve got several sort methods for several different types of arrays. In this case, we’ll work with integers but the principle remains the same for different data types. We’re going to use a sort method which will save us time by not requiring us to manually create the loops for sorting but do it automatically. Let’s see how this works with the code appearing below:

  1. import java.util.Arrays;
  2. public class Sorting {
  3.   public static void main (String [] args) {
  4.    int [] array = {45,12,85,32,89,39,69,44,42,1,6,8};
  5.    Arrays.sort(array);
  6.    for (int i = 0; i < array.length; i++) {
  7.    System.out.println(array[i]);
  8.    };
  9.   }
  10. }

So, as you can see, we get the same array in ascending order without having to create the sorting algorithm. This method not only is faster when coding but also when sorting, since it implements an advanced sorting technique called Dual-Pivot QuickSort, which works more efficiently overall.

Let’s take a look at an example of range sorting. If for some reason we needed to sort only a range of the array, we can still use the same method. We just need to specify the range, like this code appearing below:

 
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"