Program to illustrate Ascending order of n array elements
import java.util.Scanner;
public class Rkasc
{
public static void main(String[] args)
{
int n,temp;
Scanner s=new Scanner(System.in);
System.out.println("Enter Size Of Array :");
n=s.nextInt();
int[] a;//declaration of array
a=new int[n]; // Creation of array with given size
for(int i=0;i<n;i++)
{
System.out.printf("enter a[%d] = ",i);
a[i]=s.nextInt();
}
System.out.print("ARRAY before Ascending order :\n");
for(int i=0;i<n;i++)
{
System.out.print( a[i] +" ");
}
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.print("\nARRAY after Ascending order :\n");
for(int i=0;i<n;i++)
{
System.out.print( a[i] +" ");
}
for(int i=0;i<n;i++)
System.out.printf("\nAddress:%h ---array : a[%d] ---- data :%d ",i*4,i,a[i]);
}
}
output:
import java.util.Scanner;
public class Rkasc
{
public static void main(String[] args)
{
int n,temp;
Scanner s=new Scanner(System.in);
System.out.println("Enter Size Of Array :");
n=s.nextInt();
int[] a;//declaration of array
a=new int[n]; // Creation of array with given size
for(int i=0;i<n;i++)
{
System.out.printf("enter a[%d] = ",i);
a[i]=s.nextInt();
}
System.out.print("ARRAY before Ascending order :\n");
for(int i=0;i<n;i++)
{
System.out.print( a[i] +" ");
}
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.print("\nARRAY after Ascending order :\n");
for(int i=0;i<n;i++)
{
System.out.print( a[i] +" ");
}
for(int i=0;i<n;i++)
System.out.printf("\nAddress:%h ---array : a[%d] ---- data :%d ",i*4,i,a[i]);
}
}
output:
No comments:
Post a Comment