-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble.java
More file actions
29 lines (27 loc) · 826 Bytes
/
Copy pathbubble.java
File metadata and controls
29 lines (27 loc) · 826 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
public class bubble {
// short array
public static void Bubble_sort(int arr[]) {
for (int turn = 0; turn < arr.length - 1; turn++) {
for (int j = 0; j < arr.length - 1 - turn; j++) {
if (arr[j] > arr[j + 1]) { // currElement > nextElement
// swap
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Printing arrray
public static void PrintArr(int arr[]) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int arr[] = { 5, 4, 1, 3, 2 };
Bubble_sort(arr);
PrintArr(arr);
}
}