How to Sort Arrays in Java

In this post we will learn how to write a  java program to sort array elements. How to sort arrays in java. Sorting array elements means storing or printing it in ascending or descending order. So let's see how to print given array elements in sorted manner.

Basically there are two ways to sort array elements:

  1. Using sort() method
  2. Using for loop

How to sort arrays in java, sort array elements



1. Using sort() method:

  Step by step logic to print array elements in ascending order using sort() method:


1. To use sort() method first we need to import Arrays class which is defined in java.util package.

2. Define a array of integer(int) type.

3. Use sort() method to sort array elements like: Arrays.sort(arr);

4. Now print array elements using for loop or any other loop.

5. It will print all array elements sorted in ascending order.  



  Program to Print Array Elements Sorted in Ascending Order using sort() method:


/**Program to sort array elements in Ascending order*/

import java.util.Arrays;//importing the Arrays class

public class ArraySortingExample {

	public static void main(String[] args) {
		
		int arr[] = new int[] {55,7,12,3,9,23,67,2};
		//Sorting elements using predefined method
		Arrays.sort(arr);
		//printing elements
		System.out.println("Elements after sorting: ");
		for(int i=0;i<arr.length;i++) {
			System.out.println(arr[i]);
		}
	}

}


  Output:

Elements after sorting:
2
7
9
12
23
55
67

2. Same above Program Without using predefined method Using for loop:

This program is same as above program but, only change is... in this program we have used following logic instead of sort() method:
for(int i=0;i<arr.length;i++) {
	for(int j=i+1;j<arr.length;j++) {
		if(arr[i]>arr[j]) {
		temp = arr[i];
		arr[i]=arr[j];
		arr[j]=temp;
		} 
	}
}
  


  Program:


/**Program to sort array elements in Ascending order*/
public class ArraySortingExample2 {

	public static void main(String[] args) {
		int arr[] = new int[] {55,7,12,3,9,23,67,2};
		int temp;
		//sorting elements logic
		for(int i=0;i<arr.length;i++) {
			for(int j=i+1;j<arr.length;j++) {
				if(arr[i]>arr[j]) {
					temp = arr[i];
					arr[i]=arr[j];
					arr[j]=temp;
				} 
			}
		}
		
		//printing elements
		System.out.println("Elements after sorting: ");
		for(int i=0;i<arr.length;i++) {
			System.out.println(arr[i]);
		}

	}
}

  Output:

Elements after sorting:
2
7
9
12
23
55
67



  Program to Sort Array Elements in Descending Order:

Sorting arrays in descending order means storing or printing the elements in highest to lowest order.



/**Program to sort array elements in descending order*/
public class ArraySortingExample3 {

	public static void main(String[] args) {
		int arr[] = new int[] {55,7,12,3,9,23,67,2};
		int temp;
		//sorting elements logic
		for(int i=0;i<arr.length;i++) {
			for(int j=i+1;j<arr.length;j++) {
				if(arr[i]<arr[j]) {
					temp = arr[i];
					arr[i]=arr[j];
					arr[j]=temp;
				} 
			}
		}
		
		//printing elements
		System.out.println("Elements after sorting: ");
		for(int i=0;i<arr.length;i++) {
			System.out.println(arr[i]);
		}

	}

}

  Output of the above program is :

Elements after sorting: 
67
55
23
12
9
7
3
2


If you have any doubts.. feel free to comment below.... :)

No comments:

Post a Comment

If you have any doubts, please discuss here...👇