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.
Method Discussed in this page are :
- Method 1 : Using naive approach.
- Method 2 : Using inbuilt function
Example
Input : arr[4] = [10, 40, 20, 30]Output : 10 20 30 40
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
Arrays
- Introduction to Arrays – C | C++ | Java
- Introduction to 2-D Arrays – C | C++ | Java
- Sorting of Array – C | C++ | Java
- Array Rotation – C | C++ | Java
- Reverse an array or string- C | C++ | Java
- Find pairs in array with given sum – C | C++ | Java
- Sort the array in Waveform – C | C++ | Java
- Majority Element in Array – C | C++ | Java
- Boyer-Moore’s Voting Algorithm – C | C++ | Java
- K-pairs with smallest sum in 2 arrays – C | C++ | Java
- Largest Sum Contigous SubArray – C | C++ | Java
- Maximum Average Sub-array of K length – C | C++ | Java
- Size of sub-array with max sum – C | C++ | Java
- Sub-array with given sum – C | C++ | Java
- Triplet that sum to a given value – C | C++ | Java
- Segregate 0’s and 1’s in array – C | C++ | Java
- Segregate 0’s 1’s and 2’s in array – C | C++ | Java
- Sort elements in array by frequency – C | C++ | Java
- Finding pythagorean triplets in an array – C | C++ | Java
- Reorder array using given indexes – C | C++ | Java
- Merging two sorted arrays – C | C++ | Java
- Minimum number of Merge Operations to make an Array Palindrome – C | C++ | Java
- Find Zeros to be Flipped so that number of Consecutive 1’s is maximized – C | C++ | Java
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)
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+” “);
}
}
}
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));
}
}
// 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]+" ");
}
}
}
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));
}
}
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