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

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Arrays

  • Introduction to Arrays – CC++ | Java
  • Introduction to 2-D Arrays – CC++ Java
  • Sorting of Array – C | C++ | Java
  • Array Rotation – CC++ | Java
  • Reverse an array or string- CC++ | Java
  • Find pairs in array with given sum – CC++ | Java
  • Sort the array in Waveform – CC++ | Java
  • Majority Element in Array – CC++ | Java
  • Boyer-Moore’s Voting Algorithm – CC++ | Java
  • K-pairs with smallest sum in 2 arrays – C | C++ | Java
  • Largest Sum Contigous SubArray – CC++ | Java
  • Maximum Average Sub-array of K length – CC++ | Java
  • Size of sub-array with max sum – CC++ | Java
  • Sub-array with given sum – CC++ | Java
  • Triplet that sum to a given value – CC++ | Java
  • Segregate 0’s and 1’s in array – CC++ | Java
  • Segregate 0’s 1’s and 2’s in array – CC++ | Java
  • Sort elements in array by frequency – CC++ | Java
  • Finding pythagorean triplets in an array – CC++ | Java
  • Reorder array using given indexes – CC++ | Java
  • Merging two sorted arrays – CC++ | Java
  • Minimum number of Merge Operations to make an Array Palindrome – CC++ | Java
  • Find Zeros to be Flipped so that number of Consecutive 1’s is maximized – CC++ | Java

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


  • Khushi Saxena

    import java.util.*;
    class HelloWorld {
    public static void main(String[] args) {
    int a[]={5,9,3,8,4};
    int n=a.length;
    Arrays.sort(a);
    for(int i=0;i<n;i++){
    System.out.println(a[i]);
    }
    }
    }
    Time Complexity: O(nlogn)


  • Gyanendra

    import java.util.*;
    public class Main{
    public static void main(String[] args){
    Scanner scn=new Scanner(System.in);
    int n=scn.nextInt();
    int[] arr=new int[n];

    for(int i=0;i<n;i++){
    arr[i]=scn.nextInt();
    }

    for(int i=0;i<n-1;i++){

    int temp=0;
    for(int j=i+1;jarr[j]){
    int val=arr[i];
    arr[i]=arr[j];
    arr[j]=val;
    temp=1;
    }
    }

    if(temp==0){
    break;
    }
    }

    for(int val:arr){
    System.out.print(val+” “);
    }
    }
    }


  • 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));
    }
    }


  • bhondaverupesh0

    // Rupesh Bhondave @Mumbai

    ackage Sort_Array;
    import java.util.*;

    public class Sort_Using_Navie_Approch
    {
    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 of array “);
    for(int i=0;i<a.length;i++)
    {
    a[i]=sc.nextInt();
    }

    int temp=0;
    for(int i=0;i<a.length;i++)//first loop start element from o
    {
    for(int j=i+1;ja[j])// i is greater than j then swap elements
    {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
    }
    }

    }
    for(int i=0;i<a.length;i++)
    {
    System.out.print(a[i]+" ");
    }
    }
    }


  • 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