-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
33 lines (30 loc) · 1.01 KB
/
BubbleSort.java
File metadata and controls
33 lines (30 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
33
import java.util.*;
public class BubbleSort {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Insert number of elements: ");
int n = in.nextInt();
int numbers[] = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Insert element " + Integer.toString(i+1) + ": ");
numbers[i] = in.nextInt();
}
System.out.println("Sorting: " + Arrays.toString(numbers));
sort(numbers, n);
System.out.println("Sorted: " + Arrays.toString(numbers));
}
private static void sort(int[] numbers, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n-i-1; j++) {
if (numbers[j] > numbers[j+1]) {
swap(j, j+1, numbers);
}
}
}
}
private static void swap(int i, int j, int[] numbers) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}