-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinSort.java
More file actions
70 lines (54 loc) · 1.73 KB
/
BinSort.java
File metadata and controls
70 lines (54 loc) · 1.73 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
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
62
63
64
65
66
67
68
69
70
package com.mycompany.algorithm_final_project;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
*
* @author israkkayumchowdhury
*/
public class BinSort {
public void bin_sort(double[] arr){
int n = arr.length;
// Create empty bucket
ArrayList<Double>[] buckets = new ArrayList[n];
for (int i = 0; i < n; i++) {
buckets[i] = new ArrayList<>();
}
// Put array elements in different bucket
for (int i = 0; i < n; i++) {
int index = (int) (n * arr[i]);
buckets[index].add(arr[i]);
}
// sort individual buckets
for (int i = 0; i < n; i++) {
Collections.sort(buckets[i]);
}
// Concatenate all the buckets into ar[]
int index = 0;
for (int i = 0; i < n; i++) {
for(int j = 0; j < buckets[i].size(); j++){
arr[index++] = buckets[i].get(j);
}
}
}
public void main_func(){
Scanner s = new Scanner(System.in);
System.out.print(" Enter the number of elements: ");
int n = s.nextInt();
double[] arr = new double[n];
System.out.print(" Enter the elements: ");
for (int i = 0; i < n; i++) {
arr[i] = s.nextDouble();
}
System.out.print(" Original array: ");
for(double num : arr){
System.out.print(num + " ");
}
System.out.println("");
bin_sort(arr);
System.out.print(" Sorted array: ");
for(double num : arr){
System.out.print(num + " ");
}
}
}