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
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
1 Comments
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletePython Training in electronic city
DataScience with Python Training in electronic city
AWS Training in electronic city
Big Data Hadoop Training in electronic city
Devops Training in electronic city