-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubblesort.java
32 lines (32 loc) · 1 KB
/
Bubblesort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
public class Bubblesort {
public static void main(String[] args) {
int n;
Scanner s=new Scanner(System.in);
System.out.print("Enter the length of array:");
n=s.nextInt();
int [] ar=new int[n];
System.out.println("Enter the elements for the array:");
for(int i=0;i<n;i++){
ar[i]=s.nextInt();
}
System.out.print("The array initially : ");
for(int i=0;i<n;i++){
System.out.print(ar[i]+" ");
}
System.out.println("Lets sort thhis as per bubble sort!");
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(ar[i]<ar[j]){
int temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}
}
System.out.print("The array after applying the bubble sort:");
for(int i=0;i<n;i++){
System.out.print(ar[i]+" ");
}
}
}