-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathBeadSort.java
61 lines (51 loc) · 1.56 KB
/
BeadSort.java
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
51
52
53
54
55
56
57
58
59
60
61
import java.util.Arrays;
import java.util.Scanner;
public class BeadSort {
public static void display(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void beadSort(int[] arr) {
int max = Arrays.stream(arr).max().getAsInt();
int len = arr.length;
int[][] beads = new int[len][max];
for (int i = 0; i < len; i++) {
for (int j = 0; j < arr[i]; j++) {
beads[i][j] = 1;
}
}
for (int j = 0; j < max; j++) {
int sum = 0;
for (int i = 0; i < len; i++) {
sum += beads[i][j];
beads[i][j] = 0;
}
for (int i = len - sum; i < len; i++) {
beads[i][j] = 1;
}
}
for (int i = 0; i < len; i++) {
int j;
for (j = 0; j < max && beads[i][j] != 0; j++) {
}
arr[i] = j;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = scanner.nextInt();
System.out.print("Enter the elements of the array: ");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
System.out.print("Original array: ");
display(arr);
beadSort(arr);
System.out.print("Sorted array: ");
display(arr);
}
}