SlideShare a Scribd company logo
Sort:
Comparison-based
Data Structures and Algorithms
Emory University
Jinho D. Choi
AbstractSort.java
public abstract class AbstractSort<T extends Comparable<T>> {
protected Comparator<T> comparator;
protected long comparisons; // total # of comparisons performed
protected long assignments; // total # of assignments performed
public AbstractSort(Comparator<T> comparator) {
this.comparator = comparator;
resetCounts();
}
public long getComparisonCount() { return comparisons; }
public long getAssignmentCount() { return assignments; }
public void resetCounts() { comparisons = assignments = 0; }
comparisons assignments
resetCounts()
protected int compareTo(T[] array, int i, int j) {
comparisons++;
return comparator.compare(array[i], array[j]);
}
protected void assign(T[] array, int index, T value) {
assignments++;
array[index] = value;
}
protected void swap(T[] array, int i, int j) {
T t = array[i];
assign(array, i, array[j]);
assign(array, j, t);
}
public void sort(T[] array) { sort(array, 0, array.length); }
/**
* Sorts the array[beginIndex:endIndex].
* @param beginIndex the index of the first key to be sorted (inclusive).
* @param endIndex the index of the last key to be sorted (exclusive).
*/
abstract public void sort(T[] array, int beginIndex, int endIndex);
sort()
Ai ∣A∣ = n i ∈ [0, n − 1)
Am m ∈ [i + 1, n)
Ai Am
O(n )2 O(n)
O(n log n) O(n log n)
SelectionSort.java
@Override
public void sort(T[] array, final int beginIndex, final int endIndex) {
for (int i = beginIndex; i < endIndex - 1; i++) {
int min = i;
for (int j = i + 1; j < endIndex; j++) {
if (compareTo(array, j, min) < 0)
min = j;
}
swap(array, i, min);
}
}
3 2 5 4 6 7 1
Heap Sort
2
3 2 5 4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
6 2
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
6 2
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
6 2
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
6 27 5
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
6 27 5
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
3
6 27 5
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
3
6 27 57 3
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
3
6 27 57 3
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 3
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
7
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
1
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
1
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
3
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
5
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
2
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
4
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
3
1
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
3
3
1
1
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
3
3
1
1
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
3
3
1
1
1
3
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
3
3
1
1
1
3
1 3
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
3
3
1
1
1
3
1 3
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
3
3
1
1
1
3
1 3
2
1
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
3
3
1
1
1
3
1 3
2
1
2 1
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
3
3
1
1
1
3
1 3
2
1
2 1
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
3
3
1
1
1
3
1 3
2
1
2 1
1
2
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
3
3
1
1
1
3
1 3
2
1
2 1
1
2
1 2
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
3
3
1
1
1
3
1 3
2
1
2 1
1
2
1 2
Swap the rightmost leaf with the root, and sink.
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
2
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
1
1
7
7
6
6
1
1
4
4 1
1
3
6
3 6
5
5
3
3
2
2
5
5
4
4
2
2
1
1
4
4
3
3
1
1
1
3
1 3
2
1
2 1
1
2
1 2
Swap the rightmost leaf with the root, and sink.
# of comparison?
HeapSort.java
private void sink(T[] array, int k, int beginIndex, int endIndex) {
for (int i = getLeftChildIndex(beginIndex, k);
i < endIndex; k = i, i = getLeftChildIndex(beginIndex, k)) {
if (i + 1 < endIndex && compareTo(array, i, i + 1) < 0) i++;
if (compareTo(array, k, i) >= 0) break;
swap(array, k, i);
}
}
private int getLeftChildIndex(int beginIndex, int k) {
return beginIndex + 2 * (k - beginIndex) + 1;
}
array[k]
k = i, i = getLeftChildIndex(beginIndex, k)
@Override
public void sort(T[] array, int beginIndex, int endIndex) {
// heapify
for (int k = getParentIndex(beginIndex, endIndex); k >= beginIndex; k--)
sink(array, k, beginIndex, endIndex);
// swap
while (endIndex > beginIndex + 1) {
swap(array, beginIndex, --endIndex);
sink(array, beginIndex, beginIndex, endIndex);
}
}
private int getParentIndex(int beginIndex, int k) {
return beginIndex + (k - beginIndex) / 2 - 1;
}
Ai ∣A∣ = n i ∈ [1, n)
Ai−1 Ai A ≤i−1 Ai
O(n )2
O(n )2
O(n )1.5
O(n )1.5
InsertionSort.java
@Override
public void sort(T[] array, int beginIndex, int endIndex) {
sort(array, beginIndex, endIndex, 1);
}
protected void sort(T[] array, int beginIndex, int endIndex, final int h) {
int begin_h = beginIndex + h;
for (int i = begin_h; i < endIndex; i++)
for (int j = i; j >= begin_h && compareTo(array, j, j - h) < 0; j -= h)
swap(array, j, j - h);
}
h
(3 −k 1)/2 ⇒ {1, 4, 13, 40, 121, …}
2 −k 1 ⇒ {1, 3, 7, 15, 31, 63, …}
2 ⋅p 3 ⇒q {1, 2, 3, 4, 6, 8, 9, 12, …}
n/2 ⇒k {500, 250, 125, …} n = 1000
{13, 4, 1} < n/3 n = 40
Shell Sort
3
3 2 5 4 6 7 1
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 4
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
3 5
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
3 5
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
3 5
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
3 5
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
3 5 4 7
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
3 5 4 74 6
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
3 5 4 74 64 5
Shell Sort
3
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
3 5 4 74 64 5
ShellSort.java
public abstract class ShellSort<T extends Comparable<T>> extends InsertionSort<T> {
protected List<Integer> sequence;
/** @param n the expected size of the list to be sorted. */
public ShellSort(Comparator<T> comparator, int n) {
super(comparator);
sequence = new ArrayList<>();
populateSequence(n);
}
/**
* Populates the gap sequence with respect to the list size.
* @param n the size of the list to be sorted.
*/
protected abstract void populateSequence(int n);
/**
* @param n the size of the list to be sorted.
* @return the starting index of the sequence with respect to the list size.
*/
protected abstract int getSequenceStartIndex(int n);
InsertionSort
populateSequence()
@Override
public void sort(T[] array, int beginIndex, int endIndex) {
int n = endIndex - beginIndex;
populateSequence(n);
for (int i = getSequenceStartIndex(n); i >= 0; i--)
sort(array, beginIndex, endIndex, sequence.get(i));
}
populateSequence()
InsertionSort.java
protected void sort(T[] array, int beginIndex, int endIndex, final int h) {
int begin_h = beginIndex + h;
for (int i = begin_h; i < endIndex; i++)
for (int j = i; j >= begin_h && compareTo(array, j, j - h) < 0; j -= h)
swap(array, j, j - h);
}
ShellSortKnuth.java
public class ShellSortKnuth<T extends Comparable<T>> extends ShellSort<T> {
public ShellSortKnuth(Comparator<T> comparator) {
this(comparator, 1000);
}
public ShellSortKnuth(Comparator<T> comparator, int n) {
super(comparator, n);
}
@Override
protected void populateSequence(int n) {
n /= 3;
for (int t = sequence.size() + 1; ; t++) {
int h = (int) ((Math.pow(3, t) - 1) / 2);
if (h <= n) sequence.add(h);
else break;
}
}
n /= 3
populateSequence()
@Override
protected int getSequenceStartIndex(int n) {
int index = Collections.binarySearch(sequence, n / 3);
if (index < 0) index = -(index + 1);
if (index == sequence.size()) index--;
return index;
}
index == sequence.size()
SortTest.java
@Test
public void testAccuracy() {
final int iter = 100;
final int size = 100;
testAccuracy(iter, size, new SelectionSort<>());
testAccuracy(iter, size, new InsertionSort<>());
testAccuracy(iter, size, new HeapSort<>());
testAccuracy(iter, size, new ShellSortKnuth<>());
}
private void testAccuracy(final int iter, final int size, AbstractSort<Integer> engine) {
final Random rand = new Random();
Integer[] original, sorted;
for (int i = 0; i < iter; i++) {
original = Stream.generate(rand::nextInt).limit(size).toArray(Integer[]::new);
sorted = Arrays.copyOf(original, size);
engine.sort(original);
Arrays.sort(sorted);
assertArrayEquals(original, sorted);
}
}
Arrays.copyOf() Arrays.sort()
O(n )2 O(n log n) O(n) O(n)
O(n )2 O(n log n) O(n )2 O(n )1.5
O(n )2 O(n log n) O(n )2 O(n )1.5
CS253: Comparison-based Sort (2019)
CS253: Comparison-based Sort (2019)
CS253: Comparison-based Sort (2019)
CS253: Comparison-based Sort (2019)
CS253: Comparison-based Sort (2019)
CS253: Comparison-based Sort (2019)
CS253: Comparison-based Sort (2019)
CS253: Comparison-based Sort (2019)
CS253: Comparison-based Sort (2019)
CS253: Comparison-based Sort (2019)
CS253: Comparison-based Sort (2019)

More Related Content

More from Jinho Choi

Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...
Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...
Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...
Jinho Choi
 
The Myth of Higher-Order Inference in Coreference Resolution
The Myth of Higher-Order Inference in Coreference ResolutionThe Myth of Higher-Order Inference in Coreference Resolution
The Myth of Higher-Order Inference in Coreference Resolution
Jinho Choi
 
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
Jinho Choi
 
Abstract Meaning Representation
Abstract Meaning RepresentationAbstract Meaning Representation
Abstract Meaning Representation
Jinho Choi
 
Semantic Role Labeling
Semantic Role LabelingSemantic Role Labeling
Semantic Role Labeling
Jinho Choi
 
CKY Parsing
CKY ParsingCKY Parsing
CKY Parsing
Jinho Choi
 
CS329 - WordNet Similarities
CS329 - WordNet SimilaritiesCS329 - WordNet Similarities
CS329 - WordNet Similarities
Jinho Choi
 
CS329 - Lexical Relations
CS329 - Lexical RelationsCS329 - Lexical Relations
CS329 - Lexical Relations
Jinho Choi
 
Automatic Knowledge Base Expansion for Dialogue Management
Automatic Knowledge Base Expansion for Dialogue ManagementAutomatic Knowledge Base Expansion for Dialogue Management
Automatic Knowledge Base Expansion for Dialogue Management
Jinho Choi
 
Attention is All You Need for AMR Parsing
Attention is All You Need for AMR ParsingAttention is All You Need for AMR Parsing
Attention is All You Need for AMR Parsing
Jinho Choi
 
Graph-to-Text Generation and its Applications to Dialogue
Graph-to-Text Generation and its Applications to DialogueGraph-to-Text Generation and its Applications to Dialogue
Graph-to-Text Generation and its Applications to Dialogue
Jinho Choi
 
Real-time Coreference Resolution for Dialogue Understanding
Real-time Coreference Resolution for Dialogue UnderstandingReal-time Coreference Resolution for Dialogue Understanding
Real-time Coreference Resolution for Dialogue Understanding
Jinho Choi
 
Topological Sort
Topological SortTopological Sort
Topological Sort
Jinho Choi
 
Tries - Put
Tries - PutTries - Put
Tries - Put
Jinho Choi
 
Multi-modal Embedding Learning for Early Detection of Alzheimer's Disease
Multi-modal Embedding Learning for Early Detection of Alzheimer's DiseaseMulti-modal Embedding Learning for Early Detection of Alzheimer's Disease
Multi-modal Embedding Learning for Early Detection of Alzheimer's Disease
Jinho Choi
 
Building Widely-Interpretable Semantic Networks for Dialogue Contexts
Building Widely-Interpretable Semantic Networks for Dialogue ContextsBuilding Widely-Interpretable Semantic Networks for Dialogue Contexts
Building Widely-Interpretable Semantic Networks for Dialogue Contexts
Jinho Choi
 
How to make Emora talk about Sports Intelligently
How to make Emora talk about Sports IntelligentlyHow to make Emora talk about Sports Intelligently
How to make Emora talk about Sports Intelligently
Jinho Choi
 
Text-to-SQL with Data-Driven Templates
Text-to-SQL with Data-Driven TemplatesText-to-SQL with Data-Driven Templates
Text-to-SQL with Data-Driven Templates
Jinho Choi
 
Resume Classification with Term Attention Embeddings
Resume Classification with Term Attention EmbeddingsResume Classification with Term Attention Embeddings
Resume Classification with Term Attention Embeddings
Jinho Choi
 
[DSA-Java] Heap Sort
[DSA-Java] Heap Sort[DSA-Java] Heap Sort
[DSA-Java] Heap Sort
Jinho Choi
 

More from Jinho Choi (20)

Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...
Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...
Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...
 
The Myth of Higher-Order Inference in Coreference Resolution
The Myth of Higher-Order Inference in Coreference ResolutionThe Myth of Higher-Order Inference in Coreference Resolution
The Myth of Higher-Order Inference in Coreference Resolution
 
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
 
Abstract Meaning Representation
Abstract Meaning RepresentationAbstract Meaning Representation
Abstract Meaning Representation
 
Semantic Role Labeling
Semantic Role LabelingSemantic Role Labeling
Semantic Role Labeling
 
CKY Parsing
CKY ParsingCKY Parsing
CKY Parsing
 
CS329 - WordNet Similarities
CS329 - WordNet SimilaritiesCS329 - WordNet Similarities
CS329 - WordNet Similarities
 
CS329 - Lexical Relations
CS329 - Lexical RelationsCS329 - Lexical Relations
CS329 - Lexical Relations
 
Automatic Knowledge Base Expansion for Dialogue Management
Automatic Knowledge Base Expansion for Dialogue ManagementAutomatic Knowledge Base Expansion for Dialogue Management
Automatic Knowledge Base Expansion for Dialogue Management
 
Attention is All You Need for AMR Parsing
Attention is All You Need for AMR ParsingAttention is All You Need for AMR Parsing
Attention is All You Need for AMR Parsing
 
Graph-to-Text Generation and its Applications to Dialogue
Graph-to-Text Generation and its Applications to DialogueGraph-to-Text Generation and its Applications to Dialogue
Graph-to-Text Generation and its Applications to Dialogue
 
Real-time Coreference Resolution for Dialogue Understanding
Real-time Coreference Resolution for Dialogue UnderstandingReal-time Coreference Resolution for Dialogue Understanding
Real-time Coreference Resolution for Dialogue Understanding
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Tries - Put
Tries - PutTries - Put
Tries - Put
 
Multi-modal Embedding Learning for Early Detection of Alzheimer's Disease
Multi-modal Embedding Learning for Early Detection of Alzheimer's DiseaseMulti-modal Embedding Learning for Early Detection of Alzheimer's Disease
Multi-modal Embedding Learning for Early Detection of Alzheimer's Disease
 
Building Widely-Interpretable Semantic Networks for Dialogue Contexts
Building Widely-Interpretable Semantic Networks for Dialogue ContextsBuilding Widely-Interpretable Semantic Networks for Dialogue Contexts
Building Widely-Interpretable Semantic Networks for Dialogue Contexts
 
How to make Emora talk about Sports Intelligently
How to make Emora talk about Sports IntelligentlyHow to make Emora talk about Sports Intelligently
How to make Emora talk about Sports Intelligently
 
Text-to-SQL with Data-Driven Templates
Text-to-SQL with Data-Driven TemplatesText-to-SQL with Data-Driven Templates
Text-to-SQL with Data-Driven Templates
 
Resume Classification with Term Attention Embeddings
Resume Classification with Term Attention EmbeddingsResume Classification with Term Attention Embeddings
Resume Classification with Term Attention Embeddings
 
[DSA-Java] Heap Sort
[DSA-Java] Heap Sort[DSA-Java] Heap Sort
[DSA-Java] Heap Sort
 

Recently uploaded

Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 

Recently uploaded (20)

Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 

CS253: Comparison-based Sort (2019)

  • 1. Sort: Comparison-based Data Structures and Algorithms Emory University Jinho D. Choi
  • 2.
  • 3.
  • 4. AbstractSort.java public abstract class AbstractSort<T extends Comparable<T>> { protected Comparator<T> comparator; protected long comparisons; // total # of comparisons performed protected long assignments; // total # of assignments performed public AbstractSort(Comparator<T> comparator) { this.comparator = comparator; resetCounts(); } public long getComparisonCount() { return comparisons; } public long getAssignmentCount() { return assignments; } public void resetCounts() { comparisons = assignments = 0; } comparisons assignments resetCounts()
  • 5. protected int compareTo(T[] array, int i, int j) { comparisons++; return comparator.compare(array[i], array[j]); } protected void assign(T[] array, int index, T value) { assignments++; array[index] = value; } protected void swap(T[] array, int i, int j) { T t = array[i]; assign(array, i, array[j]); assign(array, j, t); } public void sort(T[] array) { sort(array, 0, array.length); } /** * Sorts the array[beginIndex:endIndex]. * @param beginIndex the index of the first key to be sorted (inclusive). * @param endIndex the index of the last key to be sorted (exclusive). */ abstract public void sort(T[] array, int beginIndex, int endIndex); sort()
  • 6. Ai ∣A∣ = n i ∈ [0, n − 1) Am m ∈ [i + 1, n) Ai Am O(n )2 O(n) O(n log n) O(n log n)
  • 7. SelectionSort.java @Override public void sort(T[] array, final int beginIndex, final int endIndex) { for (int i = beginIndex; i < endIndex - 1; i++) { int min = i; for (int j = i + 1; j < endIndex; j++) { if (compareTo(array, j, min) < 0) min = j; } swap(array, i, min); } }
  • 8. 3 2 5 4 6 7 1 Heap Sort 2
  • 9. 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap.
  • 10. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap.
  • 11. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap.
  • 12. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2
  • 13. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 6 2
  • 14. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 6 2
  • 15. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 6 2
  • 16. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 6 27 5
  • 17. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 6 27 5
  • 18. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 3 6 27 5
  • 19. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 3 6 27 57 3
  • 20. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 3 6 27 57 3
  • 21. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 3
  • 22. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3
  • 23. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 Swap the rightmost leaf with the root, and sink.
  • 24. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 Swap the rightmost leaf with the root, and sink.
  • 25. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 7 Swap the rightmost leaf with the root, and sink.
  • 26. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 Swap the rightmost leaf with the root, and sink.
  • 27. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 Swap the rightmost leaf with the root, and sink.
  • 28. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 1 Swap the rightmost leaf with the root, and sink.
  • 29. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 Swap the rightmost leaf with the root, and sink.
  • 30. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 Swap the rightmost leaf with the root, and sink.
  • 31. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 1 Swap the rightmost leaf with the root, and sink.
  • 32. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 Swap the rightmost leaf with the root, and sink.
  • 33. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 Swap the rightmost leaf with the root, and sink.
  • 34. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 Swap the rightmost leaf with the root, and sink.
  • 35. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 Swap the rightmost leaf with the root, and sink.
  • 36. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 Swap the rightmost leaf with the root, and sink.
  • 37. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 3 Swap the rightmost leaf with the root, and sink.
  • 38. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 Swap the rightmost leaf with the root, and sink.
  • 39. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 Swap the rightmost leaf with the root, and sink.
  • 40. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 5 Swap the rightmost leaf with the root, and sink.
  • 41. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 Swap the rightmost leaf with the root, and sink.
  • 42. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 Swap the rightmost leaf with the root, and sink.
  • 43. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 2 Swap the rightmost leaf with the root, and sink.
  • 44. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 Swap the rightmost leaf with the root, and sink.
  • 45. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 Swap the rightmost leaf with the root, and sink.
  • 46. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 4 Swap the rightmost leaf with the root, and sink.
  • 47. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 Swap the rightmost leaf with the root, and sink.
  • 48. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 Swap the rightmost leaf with the root, and sink.
  • 49. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 3 1 Swap the rightmost leaf with the root, and sink.
  • 50. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 3 3 1 1 Swap the rightmost leaf with the root, and sink.
  • 51. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 3 3 1 1 Swap the rightmost leaf with the root, and sink.
  • 52. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 3 3 1 1 1 3 Swap the rightmost leaf with the root, and sink.
  • 53. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 3 3 1 1 1 3 1 3 Swap the rightmost leaf with the root, and sink.
  • 54. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 3 3 1 1 1 3 1 3 Swap the rightmost leaf with the root, and sink.
  • 55. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 3 3 1 1 1 3 1 3 2 1 Swap the rightmost leaf with the root, and sink.
  • 56. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 3 3 1 1 1 3 1 3 2 1 2 1 Swap the rightmost leaf with the root, and sink.
  • 57. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 3 3 1 1 1 3 1 3 2 1 2 1 Swap the rightmost leaf with the root, and sink.
  • 58. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 3 3 1 1 1 3 1 3 2 1 2 1 1 2 Swap the rightmost leaf with the root, and sink.
  • 59. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 3 3 1 1 1 3 1 3 2 1 2 1 1 2 1 2 Swap the rightmost leaf with the root, and sink.
  • 60. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 3 3 1 1 1 3 1 3 2 1 2 1 1 2 1 2 Swap the rightmost leaf with the root, and sink.
  • 61. 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 2 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3 1 1 7 7 6 6 1 1 4 4 1 1 3 6 3 6 5 5 3 3 2 2 5 5 4 4 2 2 1 1 4 4 3 3 1 1 1 3 1 3 2 1 2 1 1 2 1 2 Swap the rightmost leaf with the root, and sink. # of comparison?
  • 62. HeapSort.java private void sink(T[] array, int k, int beginIndex, int endIndex) { for (int i = getLeftChildIndex(beginIndex, k); i < endIndex; k = i, i = getLeftChildIndex(beginIndex, k)) { if (i + 1 < endIndex && compareTo(array, i, i + 1) < 0) i++; if (compareTo(array, k, i) >= 0) break; swap(array, k, i); } } private int getLeftChildIndex(int beginIndex, int k) { return beginIndex + 2 * (k - beginIndex) + 1; } array[k] k = i, i = getLeftChildIndex(beginIndex, k)
  • 63. @Override public void sort(T[] array, int beginIndex, int endIndex) { // heapify for (int k = getParentIndex(beginIndex, endIndex); k >= beginIndex; k--) sink(array, k, beginIndex, endIndex); // swap while (endIndex > beginIndex + 1) { swap(array, beginIndex, --endIndex); sink(array, beginIndex, beginIndex, endIndex); } } private int getParentIndex(int beginIndex, int k) { return beginIndex + (k - beginIndex) / 2 - 1; }
  • 64. Ai ∣A∣ = n i ∈ [1, n) Ai−1 Ai A ≤i−1 Ai O(n )2 O(n )2 O(n )1.5 O(n )1.5
  • 65. InsertionSort.java @Override public void sort(T[] array, int beginIndex, int endIndex) { sort(array, beginIndex, endIndex, 1); } protected void sort(T[] array, int beginIndex, int endIndex, final int h) { int begin_h = beginIndex + h; for (int i = begin_h; i < endIndex; i++) for (int j = i; j >= begin_h && compareTo(array, j, j - h) < 0; j -= h) swap(array, j, j - h); } h
  • 66. (3 −k 1)/2 ⇒ {1, 4, 13, 40, 121, …} 2 −k 1 ⇒ {1, 3, 7, 15, 31, 63, …} 2 ⋅p 3 ⇒q {1, 2, 3, 4, 6, 8, 9, 12, …} n/2 ⇒k {500, 250, 125, …} n = 1000 {13, 4, 1} < n/3 n = 40
  • 67. Shell Sort 3 3 2 5 4 6 7 1
  • 68. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, …
  • 69. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3
  • 70. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3
  • 71. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 4
  • 72. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3
  • 73. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3
  • 74. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3
  • 75. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3
  • 76. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1
  • 77. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1
  • 78. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1
  • 79. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1
  • 80. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1 3 5
  • 81. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1 3 5
  • 82. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1 3 5
  • 83. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1 3 5
  • 84. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1 3 5 4 7
  • 85. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1 3 5 4 74 6
  • 86. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1 3 5 4 74 64 5
  • 87. Shell Sort 3 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1 3 5 4 74 64 5
  • 88. ShellSort.java public abstract class ShellSort<T extends Comparable<T>> extends InsertionSort<T> { protected List<Integer> sequence; /** @param n the expected size of the list to be sorted. */ public ShellSort(Comparator<T> comparator, int n) { super(comparator); sequence = new ArrayList<>(); populateSequence(n); } /** * Populates the gap sequence with respect to the list size. * @param n the size of the list to be sorted. */ protected abstract void populateSequence(int n); /** * @param n the size of the list to be sorted. * @return the starting index of the sequence with respect to the list size. */ protected abstract int getSequenceStartIndex(int n); InsertionSort populateSequence()
  • 89. @Override public void sort(T[] array, int beginIndex, int endIndex) { int n = endIndex - beginIndex; populateSequence(n); for (int i = getSequenceStartIndex(n); i >= 0; i--) sort(array, beginIndex, endIndex, sequence.get(i)); } populateSequence() InsertionSort.java protected void sort(T[] array, int beginIndex, int endIndex, final int h) { int begin_h = beginIndex + h; for (int i = begin_h; i < endIndex; i++) for (int j = i; j >= begin_h && compareTo(array, j, j - h) < 0; j -= h) swap(array, j, j - h); }
  • 90. ShellSortKnuth.java public class ShellSortKnuth<T extends Comparable<T>> extends ShellSort<T> { public ShellSortKnuth(Comparator<T> comparator) { this(comparator, 1000); } public ShellSortKnuth(Comparator<T> comparator, int n) { super(comparator, n); } @Override protected void populateSequence(int n) { n /= 3; for (int t = sequence.size() + 1; ; t++) { int h = (int) ((Math.pow(3, t) - 1) / 2); if (h <= n) sequence.add(h); else break; } } n /= 3 populateSequence()
  • 91. @Override protected int getSequenceStartIndex(int n) { int index = Collections.binarySearch(sequence, n / 3); if (index < 0) index = -(index + 1); if (index == sequence.size()) index--; return index; } index == sequence.size()
  • 92. SortTest.java @Test public void testAccuracy() { final int iter = 100; final int size = 100; testAccuracy(iter, size, new SelectionSort<>()); testAccuracy(iter, size, new InsertionSort<>()); testAccuracy(iter, size, new HeapSort<>()); testAccuracy(iter, size, new ShellSortKnuth<>()); } private void testAccuracy(final int iter, final int size, AbstractSort<Integer> engine) { final Random rand = new Random(); Integer[] original, sorted; for (int i = 0; i < iter; i++) { original = Stream.generate(rand::nextInt).limit(size).toArray(Integer[]::new); sorted = Arrays.copyOf(original, size); engine.sort(original); Arrays.sort(sorted); assertArrayEquals(original, sorted); } } Arrays.copyOf() Arrays.sort()
  • 93. O(n )2 O(n log n) O(n) O(n) O(n )2 O(n log n) O(n )2 O(n )1.5 O(n )2 O(n log n) O(n )2 O(n )1.5