-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
32 lines (32 loc) · 1.01 KB
/
Copy pathBubbleSort.java
File metadata and controls
32 lines (32 loc) · 1.01 KB
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) {
try (Scanner ab = new Scanner(System.in)) {
int[] arr;
System.out.println("limit");
int n=ab.nextInt();
arr=new int[n];
for (int i = 0; i < arr.length; i++) {
System.out.println("element number"+(i+1)+" : ");
arr[i]=ab.nextInt();
}
BubbleSort ob= new BubbleSort();
ob.bsort(arr);
}
}
void bsort(int[] arr){
int n=arr.length;
for (int i = 0; i < n; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i]>arr[j]){
arr[i]=arr[i]^arr[j];
arr[j]=arr[i]^arr[j];
arr[i]=arr[i]^arr[j];
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}