OOPS Java Program | WAP to create a function sort() to sort the given integers in ascending order. - Helpwalaa - Free IT Updates & Opportunities

New Updates

OOPS Java Program | WAP to create a function sort() to sort the given integers in ascending order.



OOPS, Java Program | WAP to create a function sort() to sort the given integers in ascending order.



import java.util.*;
class array{
void sort(){
Scanner t=new Scanner(System.in);
System.out.println("ENTER SIZE OF ARRAY");
int n=t.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++){
System.out.println("ENTER ELEMENT NO."+(i+1));
a[i]=t.nextInt();
}
System.out.println("\nARRAY BEFORE SORTING:");
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
System.out.println();
for(int i=1;i<n;i++){
for(int j=0;j<n-i;j++){
if(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("\nARRAY AFTER SORTING:");
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
System.out.println();
}
}
class test{
public static void main(String x[]){
array a=new array();
a.sort();
}
}

Most Popular