I want to do a bubble sort of numbers input by the user without asking the user how many numbers he wants to enter. User can enter as many numbers he can and the program should sort them all. So I want to find the number elements in the input before start sorting them. Here is the working code with user enter the array size himself
Update: it is one string of multiple integers eg "4 87 32 112 7" output I want is "4 7 32 87 112"(Ascending order)
import java.util.Scanner;
public class ArraySorting{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("Enter size of the array");
int z=in.nextInt();
int a[]=new int[z];
System.out.println("Enter numbers");
for(int i=0;i<z;i++){
a[i]=in.nextInt();
}
for(int j=0;j<z-1;j++){
for(int p=0;p<(z-1)-j;p++){
if(a[p]>a[p+1]){
int n=a[p];
a[p]=a[p+1];
a[p+1]=n;
}//if loop
}//for loop 2
}//for loop 1
System.out.println("Numbers in Ascending order:");
for(int k=0;k<z;k++){
System.out.print(a[k]+" ");
}
}
}