Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CS253: Distribution-based Sort (2019)

Jinho D. Choi
September 25, 2019

CS253: Distribution-based Sort (2019)

Jinho D. Choi

September 25, 2019
Tweet

More Decks by Jinho D. Choi

Other Decks in Programming

Transcript

  1. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket.
  2. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket.
  3. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket.
  4. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket.
  5. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket.
  6. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket.
  7. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket.
  8. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket.
  9. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket.
  10. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket. Put all keys in each bucket back to the array.
  11. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket. Put all keys in each bucket back to the array.
  12. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket. Put all keys in each bucket back to the array.
  13. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket. Put all keys in each bucket back to the array.
  14. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket. Put all keys in each bucket back to the array.
  15. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket. Put all keys in each bucket back to the array.
  16. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket. Put all keys in each bucket back to the array. How many comparisons & assignments?
  17. Bucket Sort - Integer 2 3 2 5 4 2

    3 1 5 1 2 3 4 5 Add each key to an appropriate bucket. Put all keys in each bucket back to the array. How many comparisons & assignments? How much extra spaces?
  18. BucketSort.java public abstract class BucketSort<T extends Comparable<T>> extends AbstractSort<T> {

    protected List<Deque<T>> buckets; /** @param numBuckets the total number of buckets. */ public BucketSort(int numBuckets) { super(null); buckets = Stream.generate(ArrayDeque<T>::new) .limit(numBuckets) .collect(Collectors.toList()); } ArrayDeque
  19. protected void sort(T[] array, int beginIndex, int endIndex, Function<T, T>

    f) { // add each element in the input array to the corresponding bucket for (int i = beginIndex; i < endIndex; i++) buckets.get(getBucketIndex(array[i], f)).add(array[i]); // merge elements in all buckets to the input array for (Deque<T> bucket : buckets) { while (!bucket.isEmpty()) array[beginIndex++] = bucket.remove(); } } /** * @param key a comparable key. * @param f takes one argument of type T, and return a value of type T (optional). * @return the index of the bucket that the key should be inserted. */ abstract protected int getBucketIndex(T key, Function<T, T> f); getBucketIndex() bucket.removeLast()
  20. IntegerBucketSort.java public class IntegerBucketSort extends BucketSort<Integer> { private final int

    GAP; /** * @param min the minimum integer (inclusive). * @param max the maximum integer (exclusive). */ public IntegerBucketSort(int min, int max) { super(max - min); GAP = -min; } @Override public void sort(Integer[] array, int beginIndex, int endIndex) { sort(array, beginIndex, endIndex, null); } @Override protected int getBucketIndex(Integer key, Function<Integer, Integer> f) { return key + GAP; } } GAP
  21. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  22. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  23. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  24. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  25. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  26. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  27. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  28. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  29. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  30. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  31. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  32. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  33. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  34. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD
  35. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  36. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  37. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  38. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  39. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  40. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  41. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  42. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  43. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  44. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  45. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  46. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  47. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  48. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD
  49. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD
  50. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD
  51. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD
  52. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD
  53. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD
  54. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD
  55. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD
  56. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD
  57. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD
  58. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD
  59. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD
  60. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD
  61. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD
  62. LSD Radix Sort 3 120 34 344 320 201 1

    13 132 0 1 2 3 4 1st LSD 2nd LSD 3rd LSD How many comparisons & assignments?
  63. RadixSort.java public abstract class RadixSort extends BucketSort<Integer> { public RadixSort()

    { super(10); } @Override protected int getBucketIndex(Integer key, Function<Integer, Integer> f) { return f.apply(key) % 10; } protected int getMaxBit(Integer[] array, int beginIndex, int endIndex) { Integer max = Arrays.stream(array, beginIndex, endIndex) .reduce(Integer::max) .orElse(null); return (max != null && max > 0) ? (int)Math.log10(max) + 1 : 0; } throw new UnsupportedOperationException() getMaxBit()
  64. LSDRadixSort.java public class LSDRadixSort extends RadixSort { @Override public void

    sort(Integer[] array, int beginIndex, int endIndex) { int maxBit = getMaxBit(array, beginIndex, endIndex); for (int bit = 0; bit < maxBit; bit++) { int div = (int) Math.pow(10, bit); sort(array, beginIndex, endIndex, k -> k / div); } } } k -> k / div