-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram51.java
More file actions
32 lines (25 loc) · 784 Bytes
/
Program51.java
File metadata and controls
32 lines (25 loc) · 784 Bytes
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
public class Program51 {
//buble sort
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
//time complexity: O(n^2)
//space complexity: O(1)
public static void main(String[] args) {
int[] arr = {7, 8, 3, 1, 2};
for (int i = 0; i < arr.length - 1; i++) {//n-1 passes
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
//swap
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printArray(arr);
}
}