Sort the given number using Bubble Sort algorithm in java
package Loops;import java.util.Scanner;
public class whileloop {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the size of array :");
int n=sc.nextInt();
int A[]=new int[n];
int temp=0;
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
for(int i=0;i<n-1;i++) {
for(int j=0;j<n-1;j++) {
if(A[j+1]<A[j]) { //swap the number
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
}
for(int sorteditem:A) { //advanced for loop
System.out.print(sorteditem+" ");
}
}
}
output:-
enter the size of array :
5
21 32 12 43 26
12 21 26 32 43
Comments
Post a Comment