Sunday 24 March 2013

A Java Program to Sort the Elements of an Array in Ascending Order.


import java.io.*;
class array
{
public static void main(String args[])throws IOException
{
System.out.println("Enter the Length of Array:->");
DataInputStream br=new DataInputStream(System.in);
String s=br.readLine();
int n=Integer.parseInt(s);
int arr[]=new int[n];
int i=0,j,temp=0;
for(i=0;i<n;i++)
{
System.out.println("Enter the value of ["+i+"]: ->");
arr[i]=Integer.parseInt(br.readLine());
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
System.out.println("The Value of array["+i+"] is:->"+arr[i]);
}
}
}

No comments:

Post a Comment

How to change Background color by selecting color from Listview

activitymain.xml file::> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xm...