Skip to content

Commit

Permalink
[BAEL-3193] - changes made to clean up code (eugenp#7760)
Browse files Browse the repository at this point in the history
* [BAEL-3193] - changes made to clean up code

* Fixed missing linkedList import

* [BAEL-3193] - changing the default value in the findMax function
  • Loading branch information
sjmillington authored and pivovarit committed Sep 17, 2019
1 parent 8da0c75 commit 76c48c3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class IntegerBucketSorter implements Sorter<Integer> {
Expand All @@ -28,43 +29,40 @@ public List<Integer> sort(List<Integer> arrayToSort) {
}

private List<Integer> concatenateSortedBuckets(List<List<Integer>> buckets){
List<Integer> sortedArray = new ArrayList<>();
int index = 0;
List<Integer> sortedArray = new LinkedList<>();
for(List<Integer> bucket : buckets){
for(int number : bucket){
sortedArray.add(index++, number);
}
sortedArray.addAll(bucket);
}
return sortedArray;
}

private List<List<Integer>> splitIntoUnsortedBuckets(List<Integer> initialList){

final int[] codes = createHashes(initialList);
final int max = findMax(initialList);
final int numberOfBuckets = (int) Math.sqrt(initialList.size());

List<List<Integer>> buckets = new ArrayList<>(codes[1]);
for(int i = 0; i < codes[1]; i++) buckets.add(new ArrayList<>());
List<List<Integer>> buckets = new ArrayList<>();
for(int i = 0; i < numberOfBuckets; i++) buckets.add(new ArrayList<>());

//distribute the data
for (int i : initialList) {
buckets.get(hash(i, codes)).add(i);
buckets.get(hash(i, max, numberOfBuckets)).add(i);
}
return buckets;

}

private int[] createHashes(List<Integer> input){
int m = input.get(0);
for (int i = 1; i < input.size(); i++) {
if (m < input.get(i)) {
m = input.get(i);
}
private int findMax(List<Integer> input){
int m = Integer.MIN_VALUE;
for (int i : input){
m = Math.max(i, m);
}
return new int[]{m, (int) Math.sqrt(input.size())};
return m;
}

private static int hash(int i, int[] code) {
return (int) ((double) i / code[0] * (code[1] - 1));
private static int hash(int i, int max, int numberOfBuckets) {
return (int) ((double) i / max * (numberOfBuckets - 1));
}


}

0 comments on commit 76c48c3

Please sign in to comment.