Bubble Sort in JAVA

package Prog;
import java.util.Scanner;


public class BubbleSort
{
public static void main(String a[])
{


Scanner sc=new Scanner(System.in);

System.out.println("Enter no. of intger to be sort" );

   int n=sc.nextInt();

int array[]=new int[n];

System.out.println("Inser values in Array" );


for(int i=0;i<n;i++)
{
array[i]=sc.nextInt();

}


for (int i=0;i<(n-1);i++)
{
for (int j=0;j<(n-i-1);j++)
{
if(array[j]>array[j+1])
{
int swap=array[j];
array[j]=array[j+1];
array[j+1]=swap;

}

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

for(int i=0;i<n;i++)
{

System.out.println(array[i]);
}


}

}



--------------------------------------------------------------------------------------------------------------------------
Output:

Enter no. of intger to be sort
6
Inser values in Array
23
43
12
43
56
52
Sorted array
12
23
43
43
52
56


Post a Comment

1 Comments

Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)