Fakultas Ilmu Komputer UI

Commit dadb946b authored by Avatar Azka's avatar Avatar Azka

Modified bucket sort algorithm

parent cdd8f98d
......@@ -3,9 +3,11 @@ package daa;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import daa.SortingVariant;
public class Main {
......@@ -31,13 +33,15 @@ public class Main {
return m;
}
// FIXME: Temp bucketsort alg, from baeldung
// Edit if needed
// Bucketsort algorithm, modify for variants
public static List<Integer> bucketSort(int arr[], int size) {
final int numberOfBuckets = (int) Math.sqrt(arr.length);
List<List<Integer>> buckets = new ArrayList<>(numberOfBuckets);
List<int[]> finalBuckets = new ArrayList<>(numberOfBuckets);
for(int i = 0; i < numberOfBuckets; i++) {
buckets.add(new ArrayList<>());
finalBuckets.add(new int[0]);
}
int max = findMax(arr);
......@@ -46,18 +50,38 @@ public class Main {
buckets.get(hash(i, max, numberOfBuckets)).add(i);
}
Comparator<Integer> comparator = Comparator.naturalOrder();
System.out.println(finalBuckets.size());
System.out.println(buckets.size());
for (int i = 0 ; i < numberOfBuckets ; i++) {
finalBuckets.set(i, convertIntegers(buckets.get(i)));
}
for(List<Integer> bucket : buckets){
bucket.sort(comparator);
for(int i = 0 ; i < numberOfBuckets ; i++){
int[] bucket = finalBuckets.get(i);
SortingVariant sv = new SortingVariant();
sv.selectionSort(bucket);
finalBuckets.set(i, bucket);
}
List<Integer> sortedArray = new LinkedList<>();
for(List<Integer> bucket : buckets) {
sortedArray.addAll(bucket);
}
for(int[] bucket : finalBuckets) {
for (int element : bucket) {
sortedArray.add(element);
}
}
return sortedArray;
}
public static int[] convertIntegers(List<Integer> integers)
{
int[] ret = new int[integers.size()];
Iterator<Integer> iterator = integers.iterator();
for (int i = 0; i < ret.length; i++)
{
ret[i] = iterator.next().intValue();
}
return ret;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment