Created
June 16, 2019 22:03
-
-
Save Roemerb/911f99ab593227b0ee905fef033f6f96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ding | |
{ | |
public static int[][] splitInBuckets(int min, int max, int nBuckets, int[] arr) throws Exception | |
{ | |
if (min < 0) | |
{ | |
throw new Exception("min cannot be smaller than 0"); | |
} | |
int[][] output = new int[nBuckets][]; | |
int[] distribution = new int[nBuckets]; | |
int[] found = new int[nBuckets]; | |
int useMin = min == 0 ? 1 : min; | |
for (int i = 0; i < arr.length; i++) | |
{ | |
for (int j = 0; j < nBuckets; j++) | |
{ | |
if ( | |
(arr[i] >= (j*useMin)) && (arr[i] <= (int) Math.ceil(max/nBuckets)) | |
) | |
{ | |
distribution[j]++; | |
break; | |
} | |
} | |
} | |
for (int i = 0; i < nBuckets; i++) | |
{ | |
distribution = new int[distribution[i]]; | |
} | |
for (int i = 0; i < arr.length; i++) | |
{ | |
for (int j = 0; j < nBuckets; j++) | |
{ | |
if ( | |
(arr[i] >= (j*useMin)) && (arr[i] <= (int) Math.ceil(max/nBuckets)) | |
) | |
{ | |
output[j][found[j]] = arr[i]; | |
found[j]++; | |
break; | |
} | |
} | |
} | |
return output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment