-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
50 lines (45 loc) · 1.41 KB
/
BubbleSort.java
File metadata and controls
50 lines (45 loc) · 1.41 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.*;
public class BubbleSort {
public static int[] bsort(int arr[]) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
// Time Comlexity=O(n2) Space Comlexity=O(1)
}
// This is not a bubble sort sorting
public static ArrayList<Integer> bsort(ArrayList<Integer> arr) {
Collections.sort(arr);
return arr;
// Time Comlexity=O(1) Space Comlexity=O(1)
}
public static void main(String[] args) {
int ar[] = { 5, 4, 1, 3, 2 };
for (int num : ar) {
System.out.print(num + " ");
}
System.out.println();
bsort(ar);
for (int num : ar) {
System.out.print(num + " ");
}
System.out.println();
System.out.println("--------------------------------------------------------------");
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(99, 23, 1, 53, 22, 53));
for (int num : arr) {
System.out.print(num + " ");
}
bsort(arr);
System.out.println();
for (int num : arr) {
System.out.print(num + " ");
}
}
}