|
1 | 1 | import java.util.Random; |
2 | 2 |
|
3 | 3 | class Node { |
4 | | - public double data; |
5 | | - public Node next; |
| 4 | + public double data; |
| 5 | + public Node next; |
6 | 6 |
|
7 | | - public Node(double data) { |
8 | | - this.data = data; |
9 | | - next = null; |
10 | | - } |
| 7 | + public Node(double data) { |
| 8 | + this.data = data; |
| 9 | + next = null; |
| 10 | + } |
11 | 11 | } |
12 | 12 |
|
13 | 13 | // Time Complexity O(n)-> avg case and O(n^2)-> Worst Case |
14 | 14 | // constraints:- 0<=A[i]<1 |
15 | 15 | public class BinSort { |
16 | | - public static void binSort(Node[] b, double[] a) { |
17 | | - for (int i = 0; i < a.length; i++) { |
18 | | - int ins = (int) (a[i] * b.length); |
19 | | - if (b[ins] == null) { |
20 | | - b[ins] = new Node(a[i]); |
21 | | - } else { |
22 | | - boolean check = true; |
23 | | - Node temp = b[ins]; |
24 | | - while (temp.next != null) { |
25 | | - if (a[i] > temp.next.data) { |
26 | | - temp = temp.next; |
27 | | - } else { |
28 | | - if (a[i] < temp.data) |
29 | | - break; |
30 | | - Node newNode = new Node(a[i]); |
31 | | - newNode.next = temp.next; |
32 | | - temp.next = newNode; |
33 | | - check = false; |
34 | | - break; |
35 | | - } |
36 | | - } |
37 | | - if (check) { |
38 | | - if (a[i] >= temp.data) { |
39 | | - Node newNode = new Node(a[i]); |
40 | | - temp.next = newNode; |
41 | | - } else { |
42 | | - double loc = temp.data; |
43 | | - temp.data = a[i]; |
44 | | - Node newNode = new Node(loc); |
45 | | - newNode.next = temp.next; |
46 | | - temp.next = newNode; |
47 | | - } |
48 | | - } |
49 | | - } |
50 | | - } |
51 | | - int j = 0; |
52 | | - for (int i = 0; i < b.length; i++) { |
53 | | - while (b[i] != null) { |
54 | | - a[j++] = b[i].data; |
55 | | - b[i] = b[i].next; |
56 | | - } |
57 | | - } |
58 | | - } |
| 16 | + public static void binSort(Node[] b, double[] a) { |
| 17 | + for (int i = 0; i < a.length; i++) { |
| 18 | + int ins = (int) (a[i] * b.length); |
| 19 | + if (b[ins] == null) { |
| 20 | + b[ins] = new Node(a[i]); |
| 21 | + } else { |
| 22 | + boolean check = true; |
| 23 | + Node temp = b[ins]; |
| 24 | + while (temp.next != null) { |
| 25 | + if (a[i] > temp.next.data) { |
| 26 | + temp = temp.next; |
| 27 | + } else { |
| 28 | + if (a[i] < temp.data) |
| 29 | + break; |
| 30 | + Node newNode = new Node(a[i]); |
| 31 | + newNode.next = temp.next; |
| 32 | + temp.next = newNode; |
| 33 | + check = false; |
| 34 | + break; |
| 35 | + } |
| 36 | + } |
| 37 | + if (check) { |
| 38 | + if (a[i] >= temp.data) { |
| 39 | + Node newNode = new Node(a[i]); |
| 40 | + temp.next = newNode; |
| 41 | + } else { |
| 42 | + double loc = temp.data; |
| 43 | + temp.data = a[i]; |
| 44 | + Node newNode = new Node(loc); |
| 45 | + newNode.next = temp.next; |
| 46 | + temp.next = newNode; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + int j = 0; |
| 52 | + for (int i = 0; i < b.length; i++) { |
| 53 | + while (b[i] != null) { |
| 54 | + a[j++] = b[i].data; |
| 55 | + b[i] = b[i].next; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
59 | 59 |
|
60 | | - public static void main(String[] args) { |
61 | | - Node[] b = new Node[10]; |
62 | | - Random ran = new Random(); |
63 | | - int n = 100; |
64 | | - double[] a = new double[n]; |
65 | | - for (int i = 0; i < n; i++) { |
66 | | - a[i] = ran.nextDouble(); |
67 | | - } |
68 | | - binSort(b, a); |
69 | | - for (int i = 0; i < n; i++) { |
70 | | - System.out.print(a[i] + " "); |
71 | | - } |
72 | | - System.out.println(" "); |
73 | | - } |
| 60 | + public static void main(String[] args) { |
| 61 | + Node[] b = new Node[10]; |
| 62 | + Random ran = new Random(); |
| 63 | + int n = 100; |
| 64 | + double[] a = new double[n]; |
| 65 | + for (int i = 0; i < n; i++) { |
| 66 | + a[i] = ran.nextDouble(); |
| 67 | + } |
| 68 | + binSort(b, a); |
| 69 | + for (int i = 0; i < n; i++) { |
| 70 | + System.out.print(a[i] + " "); |
| 71 | + } |
| 72 | + System.out.println(" "); |
| 73 | + } |
74 | 74 | } |
75 | 75 |
|
0 commit comments