Java program to sort array in ascending or descending order

Java Array sorting is another commonly asked interview questions for Java Programmers. The blog provides the solution approach for writing java program to sort array in ascending order or descending order 

Problem Statement 1: Sort an array in Ascending Order 

Consider the below given array (arrayInputFromUser[]) to sort the array elements in ascending order with elements as 12,43,23,99,78

Solution Approach for display array is ascending order

  • Get the Array Element Size as User Input
  • Get the Array Element equal to Array Size
  • Compare the Array [0] with Array [1]
  • If Array [0] > with Array [1]
  • Assign the Higher Array Element Value to Temp
  • Switch the Array Element Position
  • Assign the higher value to Temp
  • Iterate again till the Array Element Count -1
  • If Not , Compare Array[0] with Array[2]
package com.java.planforexams.array;
import java.util.Scanner;
public class ArrayElementsAscOrder {
	public static void main(String[] args) 
	{
		int count, temp;
		// Scanner object for reading User Input
		Scanner scan = new Scanner(System.in);
		// Receive the Array Element Count from User
		System.out.print("Provide the Array Element Count : ");
		// Get the Array Count
		count = scan.nextInt();
		//Set the Array Size with Count value
		int arrayInputFromUser[] = new int[count];
		//read elements 
		System.out.println("Enter the elements equal to the given count:");
		for (int incr = 0; incr < count; incr++) 
		{
			arrayInputFromUser[incr] = scan.nextInt();
		}
		// Iterating Array Element from 0 to Count -1
		for (int outerIncr = 0; outerIncr < count; outerIncr++) 
		{
			// Iterating Array Element from 1 to Count -1
			for (int innerIncr = outerIncr + 1; innerIncr < count; innerIncr++) 
			{
				if (arrayInputFromUser[outerIncr] >
                    arrayInputFromUser[innerIncr]) 
					{
						// Assign the Higher Array Element to Temp
						temp = arrayInputFromUser[outerIncr];
						// Switch the Array Elements position
						arrayInputFromUser[outerIncr] = 
                          arrayInputFromUser[innerIncr];
						arrayInputFromUser[innerIncr] = temp;
					}
			}
		}
		//Print the sorted Array Elements 		
		System.out.println("Array Elements in Ascending Order:");
		for (int arrItr = 0; arrItr < count ; arrItr++) 
		{
			System.out.println(arrayInputFromUser[arrItr]);
		}
	}   
}

Program Output : Array Soring in Ascending Order

Provide the Array Element Count : 5
Enter the elements equal to the given count:
12
43
23
99
78
Array Soring in Ascending Order :
12
23
43
78
99

Problem Statement 2: Sort an array in Descending Order

Consider the below given array (arrayInputFromUser[]) to sort the array elements in ascending order with elements as 12,43,23,99,78 

  • Get the Array Element Size as User Input
  • Get the Array Element equal to Array Size
  • Compare the Array [0] with Array [1]
  • If Array [0] < with Array [1]
  • Assign the Lower Array Element Value to Temp
  • Switch the Array Element Position
  • Assign the lower value to Temp
  • Iterate again till the Array Element Count -1
  • If Not , Compare Array[0] with Array[2]
package com.java.planforexams.array;
import java.util.Scanner;
public class ArrayElementsDescOrder {
	public static void main(String[] args) 
	{
		int count, temp;
		// Scanner object for reading User Input
		Scanner scan = new Scanner(System.in);
		// Receive the Array Element Count from User
		System.out.print("Provide the Array Element Count : ");
		// Get the Array Count
		count = scan.nextInt();
		//Set the Array Size with Count value
		int arrayInputFromUser[] = new int[count];
		//read elements 
		System.out.println("Enter the elements equal to the given count:");
		for (int incr = 0; incr < count; incr++) 
		{
			arrayInputFromUser[incr] = scan.nextInt();
		}
		// Iterating Array Element from 0 to Count -1
		for (int outerIncr = 0; outerIncr < count; outerIncr++) 
		{
			// Iterating Array Element from 1 to Count -1
			for (int innerIncr = outerIncr + 1; innerIncr < count; innerIncr++) 
			{
				if (arrayInputFromUser[outerIncr] < 
                    arrayInputFromUser[innerIncr]) 
					{
						// Assign the Higher Array Element to Temp
						temp = arrayInputFromUser[outerIncr];
						// Switch the Array Elements position
						arrayInputFromUser[outerIncr] = 
                          arrayInputFromUser[innerIncr];
						arrayInputFromUser[innerIncr] = temp;
					}
			}
		}
		//Print the sorted Array Elements 		
		System.out.println("Array Elements in Descending Order:");
		for (int arrItr = 0; arrItr < count ; arrItr++) 
		{
			System.out.println(arrayInputFromUser[arrItr]);
		}
	}   
}

Program Output : Array Soring in Descending Order

Provide the Array Element Count : 5
Enter the elements equal to the given count:
12
43
23
99
78
Array Soring in Descending Order:
99
78
43
23
12

Leave a Reply

Your email address will not be published. Required fields are marked *

*