#Bubble sort | #Largest Number |#second largest number| #sorting
package javaprogram;
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
System.out.println("how many inputs you have to sort");
Scanner sc=new Scanner(System.in);
int arrSize = sc.nextInt();
int[] arr=new int[arrSize];
int temp;
for(int k=0;k<arrSize;k++)
{
System.out.println("Enter "+(k+1)+"th element");
arr[k]=sc.nextInt();
}
System.out.println("Entered Arrays are :");
for(int m=0;m<arrSize;m++)
{
System.out.print(arr[m]);
}
System.out.println();
System.out.println("Sorted Array in each loop are");
for(int i=0;i<arrSize;i++)
{
System.out.println("iteration (i)"+i);
for(int j=0;j<arrSize-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
for(int l=0;l<arrSize;l++)
{
System.out.print(arr[l]);
}
System.out.println();
}
System.out.println();
}
}
}
--------------------------------------------------------------------------------------------------------
output:
how many inputs you have to sort
5
Enter 1th element
5
Enter 2th element
1
Enter 3th element
4
Enter 4th element
2
Enter 5th element
1
Entered Arrays are :
51421
Sorted Array in each loop are
iteration (i)0
15421
14521
14251
14215
iteration (i)1
14215
12415
12145
iteration (i)2
12145
11245
iteration (i)3
11245
iteration (i)4
Comments
Post a Comment