From 76c48c308ec5e209ce03c48af535b7debda940ed Mon Sep 17 00:00:00 2001 From: Sam Millington Date: Tue, 17 Sep 2019 15:03:20 +0100 Subject: [PATCH] [BAEL-3193] - changes made to clean up code (#7760) * [BAEL-3193] - changes made to clean up code * Fixed missing linkedList import * [BAEL-3193] - changing the default value in the findMax function --- .../bucketsort/IntegerBucketSorter.java | 34 +++++++++---------- .../algorithms/bucketsort/Sorter.java | 0 .../IntegerBucketSorterUnitTest.java | 0 3 files changed, 16 insertions(+), 18 deletions(-) rename {algorithms-miscellaneous-3 => algorithms-sorting}/src/main/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorter.java (57%) rename {algorithms-miscellaneous-3 => algorithms-sorting}/src/main/java/com/baeldung/algorithms/bucketsort/Sorter.java (100%) rename {algorithms-miscellaneous-3 => algorithms-sorting}/src/test/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorterUnitTest.java (100%) diff --git a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorter.java b/algorithms-sorting/src/main/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorter.java similarity index 57% rename from algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorter.java rename to algorithms-sorting/src/main/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorter.java index 4d885a6b3a..002d4997f4 100644 --- a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorter.java +++ b/algorithms-sorting/src/main/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorter.java @@ -2,6 +2,7 @@ package com.baeldung.bucketsort; import java.util.ArrayList; import java.util.Comparator; +import java.util.LinkedList; import java.util.List; public class IntegerBucketSorter implements Sorter { @@ -28,43 +29,40 @@ public class IntegerBucketSorter implements Sorter { } private List concatenateSortedBuckets(List> buckets){ - List sortedArray = new ArrayList<>(); - int index = 0; + List sortedArray = new LinkedList<>(); for(List bucket : buckets){ - for(int number : bucket){ - sortedArray.add(index++, number); - } + sortedArray.addAll(bucket); } return sortedArray; } private List> splitIntoUnsortedBuckets(List initialList){ - final int[] codes = createHashes(initialList); + final int max = findMax(initialList); + final int numberOfBuckets = (int) Math.sqrt(initialList.size()); - List> buckets = new ArrayList<>(codes[1]); - for(int i = 0; i < codes[1]; i++) buckets.add(new ArrayList<>()); + List> 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 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 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)); } + } diff --git a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/bucketsort/Sorter.java b/algorithms-sorting/src/main/java/com/baeldung/algorithms/bucketsort/Sorter.java similarity index 100% rename from algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/bucketsort/Sorter.java rename to algorithms-sorting/src/main/java/com/baeldung/algorithms/bucketsort/Sorter.java diff --git a/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorterUnitTest.java b/algorithms-sorting/src/test/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorterUnitTest.java similarity index 100% rename from algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorterUnitTest.java rename to algorithms-sorting/src/test/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorterUnitTest.java