Java program for Sorting Elements In An Array

Sort the array in Java

Here, in this page we will discuss the program to sort the array in java. We are given with an array and need to print the sorted array. In this page we will discuss various algorithms to sort the given input array.

Sorting element in array by frequency using C++

Method Discussed in this page are :

  • Method 1 : Using naive approach.
  • Method 2 : Using inbuilt function

Method 1 :

  • Run a loop from 0 to n-1
  • Inside that loop run another loop from i+1 to n
  • Check if(arr[i]>arr[j])
  • Swap arr[i] with arr[j]

Method 1 : Code in Java

Run
public class Main { 
    public static void main(String[] args) { 

       //Initialize array 
       int [] arr = new int [] {10, 40, 30, 20}; 
       int temp = 0; 

       //Sort the array in ascending order 
       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; 
              } 
           } 
       } 


    //Displaying elements of array after sorting 
    for (int i = 0; i < arr.length; i++) { 
     System.out.print(arr[i] + " "); 
   } 
  } 
}

Output

10 20 30 40

Method 2 :

  • Declare the array.
  • Sort the array using sort function Arrays.sort()
Sort the array in java

Method 2 : Code in Java

Run

import java.util.*;
class Main
{

// function to sort the array
static void printOrder(int[] arr, int n)
{
// sorting the array
Arrays.sort(arr);

// printing first half in ascending order
for (int i = 0; i < n ; i++)
{
System.out.print(arr[i] + " ");
}

}

// Driver code
public static void main(String[] args)
{
int[] arr = {10, 30, 40, 20};
int n = arr.length;
printOrder(arr,n); } }

Output

10 20 30 40

3 comments on “Java program for Sorting Elements In An Array”


  • bhondaverupesh0

    Method3=Parallel sort Approach
    package Sort_Array;
    import java.util.*;

    public class Sort_Array_Using_Inbuilt_Method
    {
    public static void main(String[]args)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println(“Enter size of array “);
    int n=sc.nextInt();
    int []a=new int[n];

    System.out.println(“Enter element in array “);
    for(int i=0;i<a.length;i++)
    {
    a[i]=sc.nextInt();
    }

    Arrays.parallelSort(a);
    System.out.println(Arrays.toString(a));
    }
    }


  • Pranjal

    import java.util.Arrays;
    import java.util.Scanner;

    public class BubbleSort {
    public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println(“Enter the number of elements you want to enter:”);
    int n = s.nextInt();
    int[] arr = new int[n];

    System.out.println(“Enter the elements: “);
    for (int i = 0; i < n; i++) {
    arr[i] = s.nextInt();
    }
    System.out.println("The number you entered: " + Arrays.toString(arr));
    for (int i=0; i< arr.length; i++)
    {
    for (int j =0; jarr[j+1])
    {
    int temp= arr[j];
    arr[j]= arr[j+1];
    arr[j+1]=temp;
    }
    }
    }
    System.out.println(” The sorted array is” +Arrays.toString(arr));
    }
    }


  • Durai

    package com.javabasic;
    import java.util.Scanner;
    public class Main {
    public static void prep(int[] a, int total){
    int temp;
    for (int i = 0; i < total; i++)
    {
    for (int j = i + 1; j a[j])
    {
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    }
    }
    }
    for (int i=0;i<total;i++) {
    System.out.println(a[i]);
    }
    }
    public static void main(String[] args) {
    int []a=new int[50];
    var sc=new Scanner(System.in);
    System.out.println("Enter Array Size : ");
    int size=sc.nextInt();
    System.out.println("Enter the elements one by one ");
    for (int i=0;i<size;i++) {
    a[i]=sc.nextInt();
    }
    prep(a,size);
    }
    }

    This is a correct program