Computer Science, asked by mahapatranandinil4, 5 months ago

Plzzzzzzzzzzzzzz solve this program​

Attachments:

Answers

Answered by anindyaadhikari13
1

Question:-

Write a java program to input 20 integer elements in an array. Now, rearrange all the elements in ascending order using bubble sort.

Program:-

import java.util.*;

class Sort

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

System.out.println("Enter 20 elements in the array.... ");

int arr[]=new int[20];

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

{

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

arr[i]=sc.nextInt();

}

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

{

for(int j=1; j <20-i); j++)

{

if(arr[j-1] > arr[j])

{

int x=arr[j];

arr[j]=arr[j-1];

arr[j-1]=x;

}

}

)

System.out.println("Array after sorting...");

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

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

}

}

Answered by Anonymous
0

import java.util.*;

class abc

{

public static void main ( String args [ ] )

{

Scanner in = new Scanner (System.in );

int i ,n ,j ,temp ;

int A[ ] = new int [ 20 ] ;

for (i=0 ; i <20 ; i++ )

{

System.out.println ( " enter no. " ) ;

A [ i ] = in.nextInt() ;

}

for ( i=0 ; i< 19 ; i++ )

{

for ( j =0; j<19- i ; j++ )

{

if ( A[ j ] > A [j +1] )

{

temp= A [j];

A[j]+ A [j +1 ] ;

A [j +1 ] = temp;

}

}

}

for ( i= 0;i < 19 ; i++ )

System.out.println( "sorted array ="+ A [i] );

}

}

Similar questions