-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
31 lines (28 loc) · 803 Bytes
/
BubbleSort.java
File metadata and controls
31 lines (28 loc) · 803 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
package Sorting;
public class BubbleSort {
public static void bubbleSort(int[] array) {
for (int i = array.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (array[j] > array[j + 1]) {
swap (array, j, j + 1);
}
}
}
}
private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String[] args) {
int[] numbers = {3, 1, 4, 9, -2, 5};
for (int n : numbers) {
System.out.print(n + " ");
}
bubbleSort(numbers);
System.out.println();
for (int n : numbers) {
System.out.print(n + " ");
}
}
}