4.8k views
0 votes
Write a Java program to get the values stored in a four-by-five array of integers and store those values in a one-dimensional array referred by the name sorted1Darray . This array will have all the numbers in increasing order (remember the bubble sort!!) !

1 Answer

4 votes

Answer:

done

Step-by-step explanation:

int a[][] = new int[4][4];

Random r = new Random();

for(int i=0;i<4;i++)

for(int j=0;j<4;j++)

{

a[i][j]=r.nextInt()%100;

}

int sorted1Darray[] = new int [16];

int k=0;

for(int i=0;i<4;i++)

for(int j=0;j<4;j++)

sorted1Darray[k++]=a[i][j];

//now sorting using bubble sort

for(int i=0;i<16;i++)

for(int j=0;j<16;j++)

if(sorted1Darray[i]<sorted1Darray[j])

{

int t= sorted1Darray[i];

sorted1Darray[i]=sorted1Darray[j];

sorted1Darray[j]=t;

}

System.out.println("Sorted array:");

for(int i=0;i<16;i++)

System.out.print(sorted1Darray[i]+" ");

System.out.println();

User Mircea Grelus
by
4.1k points