Java Programs

TheFestFever.com

Our aim is to connect students from campuses all over Delhi by making them aware of all Technical, Cultural, Management fests, workshops, Seminars organized by every college in Delhi.

Join Us Now

Program 23: Write a Java program to sort the array in increasing order.

IncreasingOrder.java

import java.util.Scanner;

public class IncreasingOrder
{
	public static void main(String[] args)
	{
		int n;
		
		Scanner s = new Scanner(System.in);
		
		System.out.println("Enter the number of elements to be included in the array");
		n=s.nextInt();
	
		//Input Elements in array
		int a[] = new int[n];
		System.out.println("Enter elements of array");
		for(int i=0; i < n; i++)
		{
			System.out.print("Enter "+(i+1)+" element: ");
			a[i]=s.nextInt();	
		}
		
		System.out.println("Unsorted array");
		for(int i=0; i < n; i++)
		{
			System.out.print(a[i]+" ");
				
		}

		System.out.println();	
		
		//Sorting procedure.
		for(int i=0;i < n;i++)
		{
			for(int j=i+1;j < n;j++)
			{
				if(a[i]>a[j])
				{
					int temp = a[i];
					a[i] = a[j];
					a[j]=temp;
				}
			}
		}
			
		System.out.println("Sorted array in increasing order");
		
		for(int i = 0;i < n;i++)
		{
			System.out.print(a[i]+" ");
		}
	}
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.