The document discusses sorting algorithms and heap sort. It begins with an overview of comparison-based and distribution-based sorting algorithms such as selection sort, heap sort, insertion sort, merge sort, and quicksort. It then covers the implementation of an abstract sorting class with methods for comparing elements, swapping elements, and tracking comparisons and assignments. The document concludes with an explanation of how heap sort works by sinking nodes to construct a max-heap and then repeatedly swapping the root element with the rightmost leaf and sinking it to extract elements in sorted order.
4. Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Comparison
based
Distribution
based
5. Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Comparison
based
Distribution
based
Selection Insertion Merge Quick
6. Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Comparison
based
Distribution
based
Selection
Heap
Insertion
Shell
Merge Quick
Tim Intro
7. Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Comparison
based
Distribution
based
Bucket
Radix
Selection
Heap
Insertion
Shell
Merge Quick
Tim Intro
8. Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Comparison
based
Distribution
based
Bucket
Radix
Selection
Heap
Insertion
Shell
Merge Quick
vs.?
Tim Intro
9. Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Comparison
based
Distribution
based
Bucket
Radix
Selection
Heap
Insertion
Shell
Merge Quick
vs.?
Best in general?
Tim Intro
10. Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Comparison
based
Distribution
based
Bucket
Radix
Selection
Heap
Insertion
Shell
Merge Quick
vs.?
Best in general?
When 1 < CPUs?
Tim Intro
11. Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Comparison
based
Distribution
based
Bucket
Radix
Selection
Heap
Insertion
Shell
Merge Quick
vs.?
Best in general?
When range is known?
When 1 < CPUs?
Tim Intro
12. Emory University Logo Guidelines
-
Abstract Sort
3
public abstract class AbstractSort<T extends Comparable<T>>
{
protected Comparator<T> comparator;
protected long comparisons;
protected long assignments;
public AbstractSort(Comparator<T> comparator)
{
setComparator(comparator);
resetCounts();
}
public Comparator<T> getComparator()
{
return comparator;
}
public void setComparator(Comparator<T> comparator)
{
this.comparator = comparator;
}
13. Emory University Logo Guidelines
-
Abstract Sort
3
public abstract class AbstractSort<T extends Comparable<T>>
{
protected Comparator<T> comparator;
protected long comparisons;
protected long assignments;
public AbstractSort(Comparator<T> comparator)
{
setComparator(comparator);
resetCounts();
}
public Comparator<T> getComparator()
{
return comparator;
}
public void setComparator(Comparator<T> comparator)
{
this.comparator = comparator;
}
14. Emory University Logo Guidelines
-
Abstract Sort
3
public abstract class AbstractSort<T extends Comparable<T>>
{
protected Comparator<T> comparator;
protected long comparisons;
protected long assignments;
public AbstractSort(Comparator<T> comparator)
{
setComparator(comparator);
resetCounts();
}
public Comparator<T> getComparator()
{
return comparator;
}
public void setComparator(Comparator<T> comparator)
{
this.comparator = comparator;
}
15. Emory University Logo Guidelines
-
Abstract Sort
3
public abstract class AbstractSort<T extends Comparable<T>>
{
protected Comparator<T> comparator;
protected long comparisons;
protected long assignments;
public AbstractSort(Comparator<T> comparator)
{
setComparator(comparator);
resetCounts();
}
public Comparator<T> getComparator()
{
return comparator;
}
public void setComparator(Comparator<T> comparator)
{
this.comparator = comparator;
}
getter
16. Emory University Logo Guidelines
-
Abstract Sort
3
public abstract class AbstractSort<T extends Comparable<T>>
{
protected Comparator<T> comparator;
protected long comparisons;
protected long assignments;
public AbstractSort(Comparator<T> comparator)
{
setComparator(comparator);
resetCounts();
}
public Comparator<T> getComparator()
{
return comparator;
}
public void setComparator(Comparator<T> comparator)
{
this.comparator = comparator;
}
getter
setter
17. Emory University Logo Guidelines
-
Abstract Sort
4
public abstract class AbstractSort<T extends Comparable<T>>
{
protected Comparator<T> comparator;
protected long comparisons;
protected long assignments;
public AbstractSort(Comparator<T> comparator)
{
setComparator(comparator);
resetCounts();
}
...
public long getComparisonCount() {return comparisons;}
public long getAssignmentCount() {return assignments;}
public void resetCounts()
{
comparisons = 0;
assignments = 0;
}
18. Emory University Logo Guidelines
-
Abstract Sort
4
public abstract class AbstractSort<T extends Comparable<T>>
{
protected Comparator<T> comparator;
protected long comparisons;
protected long assignments;
public AbstractSort(Comparator<T> comparator)
{
setComparator(comparator);
resetCounts();
}
...
public long getComparisonCount() {return comparisons;}
public long getAssignmentCount() {return assignments;}
public void resetCounts()
{
comparisons = 0;
assignments = 0;
}
for benchmarking
19. Emory University Logo Guidelines
-
Abstract Sort
4
public abstract class AbstractSort<T extends Comparable<T>>
{
protected Comparator<T> comparator;
protected long comparisons;
protected long assignments;
public AbstractSort(Comparator<T> comparator)
{
setComparator(comparator);
resetCounts();
}
...
public long getComparisonCount() {return comparisons;}
public long getAssignmentCount() {return assignments;}
public void resetCounts()
{
comparisons = 0;
assignments = 0;
}
for benchmarking
20. Emory University Logo Guidelines
-
Abstract Sort
4
public abstract class AbstractSort<T extends Comparable<T>>
{
protected Comparator<T> comparator;
protected long comparisons;
protected long assignments;
public AbstractSort(Comparator<T> comparator)
{
setComparator(comparator);
resetCounts();
}
...
public long getComparisonCount() {return comparisons;}
public long getAssignmentCount() {return assignments;}
public void resetCounts()
{
comparisons = 0;
assignments = 0;
}
for benchmarking
21. Emory University Logo Guidelines
-
Abstract Sort
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);
}
22. Emory University Logo Guidelines
-
Abstract Sort
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);
}
23. Emory University Logo Guidelines
-
Abstract Sort
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);
}
24. Emory University Logo Guidelines
-
Abstract Sort
6
/**
* Sorts the array[beginIndex:endIndex] in ascending order.
* @param array an array of comparable keys.
* @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);
public void sort(T[] array)
{
sort(array, 0, array.length);
}
25. Emory University Logo Guidelines
-
Abstract Sort
6
/**
* Sorts the array[beginIndex:endIndex] in ascending order.
* @param array an array of comparable keys.
* @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);
public void sort(T[] array)
{
sort(array, 0, array.length);
}
javadoc
27. Emory University Logo Guidelines
-
Selection-based Sort
• For each key ki in L[0, n-2]:
7
28. Emory University Logo Guidelines
-
Selection-based Sort
• For each key ki in L[0, n-2]:
- Search the minimum key kj in the L[i+1, n-1].
7
29. Emory University Logo Guidelines
-
Selection-based Sort
• For each key ki in L[0, n-2]:
- Search the minimum key kj in the L[i+1, n-1].
- Swap ki and kj.
7
30. Emory University Logo Guidelines
-
Selection-based Sort
• For each key ki in L[0, n-2]:
- Search the minimum key kj in the L[i+1, n-1].
- Swap ki and kj.
7
Search Compare Swap
Selection Linear O(n2) O(n)
Heap Heap O(n log n) O(n log n)
31. Emory University Logo Guidelines
-
Selection-based Sort
• For each key ki in L[0, n-2]:
- Search the minimum key kj in the L[i+1, n-1].
- Swap ki and kj.
7
Search Compare Swap
Selection Linear O(n2) O(n)
Heap Heap O(n log n) O(n log n)
32. Emory University Logo Guidelines
-
Selection-based Sort
• For each key ki in L[0, n-2]:
- Search the minimum key kj in the L[i+1, n-1].
- Swap ki and kj.
7
Search Compare Swap
Selection Linear O(n2) O(n)
Heap Heap O(n log n) O(n log n)
33. Emory University Logo Guidelines
-
Selection-based Sort
• For each key ki in L[0, n-2]:
- Search the minimum key kj in the L[i+1, n-1].
- Swap ki and kj.
7
Search Compare Swap
Selection Linear O(n2) O(n)
Heap Heap O(n log n) O(n log n)
34. Emory University Logo Guidelines
-
Selection-based Sort
• For each key ki in L[0, n-2]:
- Search the minimum key kj in the L[i+1, n-1].
- Swap ki and kj.
7
Search Compare Swap
Selection Linear O(n2) O(n)
Heap Heap O(n log n) O(n log n)
35. Emory University Logo Guidelines
-
Selection Sort
8
public void sort(T[] array, final int beginIndex, final int endIndex)
{
int min;
for (int i=beginIndex; i<endIndex-1; i++)
{
min = i;
for (int j=i+1; j<endIndex; j++)
{
if (compareTo(array, j, min) < 0)
min = j;
}
swap(array, i, min);
}
}
36. Emory University Logo Guidelines
-
Selection Sort
8
public void sort(T[] array, final int beginIndex, final int endIndex)
{
int min;
for (int i=beginIndex; i<endIndex-1; i++)
{
min = i;
for (int j=i+1; j<endIndex; j++)
{
if (compareTo(array, j, min) < 0)
min = j;
}
swap(array, i, min);
}
}
O(n2)
37. Emory University Logo Guidelines
-
Selection Sort
8
public void sort(T[] array, final int beginIndex, final int endIndex)
{
int min;
for (int i=beginIndex; i<endIndex-1; i++)
{
min = i;
for (int j=i+1; j<endIndex; j++)
{
if (compareTo(array, j, min) < 0)
min = j;
}
swap(array, i, min);
}
}
O(n2)
O(n)
97. Emory University Logo Guidelines
-
Insertion-based Sort
• For each key ki in L[1, n-1]:
11
98. Emory University Logo Guidelines
-
Insertion-based Sort
• For each key ki in L[1, n-1]:
- Keep swapping ki-1 and ki until ki-1 ≤ ki.
11
99. Emory University Logo Guidelines
-
Insertion-based Sort
• For each key ki in L[1, n-1]:
- Keep swapping ki-1 and ki until ki-1 ≤ ki.
11
Pair Compare Swap
Insertion Adjacent O(n2) O(n2)
Shell
By Knuth
Sequence
O(n1.5) O(n1.5)
100. Emory University Logo Guidelines
-
Insertion-based Sort
• For each key ki in L[1, n-1]:
- Keep swapping ki-1 and ki until ki-1 ≤ ki.
11
Pair Compare Swap
Insertion Adjacent O(n2) O(n2)
Shell
By Knuth
Sequence
O(n1.5) O(n1.5)
101. Emory University Logo Guidelines
-
Insertion-based Sort
• For each key ki in L[1, n-1]:
- Keep swapping ki-1 and ki until ki-1 ≤ ki.
11
Pair Compare Swap
Insertion Adjacent O(n2) O(n2)
Shell
By Knuth
Sequence
O(n1.5) O(n1.5)
102. Emory University Logo Guidelines
-
Insertion Sort
12
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 beginH = beginIndex + h;
for (int i=beginH; i<endIndex; i++)
for (int j=i; j>=beginH && compareTo(array, j, j-h) < 0; j-=h)
swap(array, j, j-h);
}
103. Emory University Logo Guidelines
-
Insertion Sort
12
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 beginH = beginIndex + h;
for (int i=beginH; i<endIndex; i++)
for (int j=i; j>=beginH && compareTo(array, j, j-h) < 0; j-=h)
swap(array, j, j-h);
}
104. Emory University Logo Guidelines
-
Insertion Sort
12
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 beginH = beginIndex + h;
for (int i=beginH; i<endIndex; i++)
for (int j=i; j>=beginH && compareTo(array, j, j-h) < 0; j-=h)
swap(array, j, j-h);
}
Advantage over selection sort?
105. Emory University Logo Guidelines
-
Insertion Sort
12
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 beginH = beginIndex + h;
for (int i=beginH; i<endIndex; i++)
for (int j=i; j>=beginH && compareTo(array, j, j-h) < 0; j-=h)
swap(array, j, j-h);
}
Advantage over selection sort?
41 1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728293031323334353637383940 0
106. Emory University Logo Guidelines
-
Insertion Sort
12
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 beginH = beginIndex + h;
for (int i=beginH; i<endIndex; i++)
for (int j=i; j>=beginH && compareTo(array, j, j-h) < 0; j-=h)
swap(array, j, j-h);
}
Advantage over selection sort?
41 1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728293031323334353637383940 0
107. Emory University Logo Guidelines
-
Insertion Sort
12
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 beginH = beginIndex + h;
for (int i=beginH; i<endIndex; i++)
for (int j=i; j>=beginH && compareTo(array, j, j-h) < 0; j-=h)
swap(array, j, j-h);
}
Advantage over selection sort?
How many swaps?
41 1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728293031323334353637383940 0
108. Emory University Logo Guidelines
-
Insertion Sort
12
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 beginH = beginIndex + h;
for (int i=beginH; i<endIndex; i++)
for (int j=i; j>=beginH && compareTo(array, j, j-h) < 0; j-=h)
swap(array, j, j-h);
}
Advantage over selection sort?
How many swaps?
Can we avoid unnecessary swaps?
41 1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728293031323334353637383940 0
110. Emory University Logo Guidelines
-
Shell Sort
13
41 1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728293031323334353637383940 0
Group keys whose distance is defined by a sequence:
111. Emory University Logo Guidelines
-
Shell Sort
13
41 1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728293031323334353637383940 0
14, 4, 1 < N / 3, where N = 42
Group keys whose distance is defined by a sequence:
112. Emory University Logo Guidelines
-
Shell Sort
13
41 1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728293031323334353637383940 0
14, 4, 1 < N / 3, where N = 42
Group keys whose distance is defined by a sequence:
113. Emory University Logo Guidelines
-
Shell Sort
13
41 1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728293031323334353637383940 0
14, 4, 1 < N / 3, where N = 42
Group keys whose distance is defined by a sequence:
Sort each group using insertion sort
114. Emory University Logo Guidelines
-
Shell Sort
13
41 1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728293031323334353637383940 0
14, 4, 1 < N / 3, where N = 42
Only 4 keys to swaps!
Group keys whose distance is defined by a sequence:
Sort each group using insertion sort
115. Emory University Logo Guidelines
-
Shell Sort
13
41 1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728293031323334353637383940 0
14, 4, 1 < N / 3, where N = 42
Only 4 keys to swaps!
Group keys whose distance is defined by a sequence:
Sort each group using insertion sort
Shell: n / 2k (n = 1000) ⇒ 500, 250, 125, …
Pratt: 2p ⋅ 3q ⇒ 1, 2, 3, 4, 6, 8, 9, 12, …
Hibbard: 2p ⋅ 3q ⇒ 1, 2, 3, 4, 6, 8, 9, 12, …
Knuth: (3t - 1) / 2 ⇒ 1, 4, 14, 40, 121, …
137. Emory University Logo Guidelines
-
Shell Sort
15
public abstract class ShellSort<T extends Comparable<T>> extends InsertionSort<T>
{
protected List<Integer> sequence;
public ShellSort(Comparator<T> comparator, int n)
{
super(comparator);
sequence = new ArrayList<>();
populateSequence(n);
}
protected abstract void populateSequence(int n);
...
}
138. Emory University Logo Guidelines
-
Shell Sort
15
public abstract class ShellSort<T extends Comparable<T>> extends InsertionSort<T>
{
protected List<Integer> sequence;
public ShellSort(Comparator<T> comparator, int n)
{
super(comparator);
sequence = new ArrayList<>();
populateSequence(n);
}
protected abstract void populateSequence(int n);
...
}
139. Emory University Logo Guidelines
-
Shell Sort
15
public abstract class ShellSort<T extends Comparable<T>> extends InsertionSort<T>
{
protected List<Integer> sequence;
public ShellSort(Comparator<T> comparator, int n)
{
super(comparator);
sequence = new ArrayList<>();
populateSequence(n);
}
protected abstract void populateSequence(int n);
...
}
pre-populate the sequence
140. Emory University Logo Guidelines
-
Shell Sort
16
public abstract class ShellSort<T extends Comparable<T>> extends InsertionSort<T>
{
...
@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));
}
protected abstract int getSequenceStartIndex(int n);
}
141. Emory University Logo Guidelines
-
Shell Sort
16
public abstract class ShellSort<T extends Comparable<T>> extends InsertionSort<T>
{
...
@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));
}
protected abstract int getSequenceStartIndex(int n);
}
142. Emory University Logo Guidelines
-
Shell Sort
16
public abstract class ShellSort<T extends Comparable<T>> extends InsertionSort<T>
{
...
@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));
}
protected abstract int getSequenceStartIndex(int n);
}
143. Emory University Logo Guidelines
-
Shell Sort
17
public abstract class ShellSort<T extends Comparable<T>> extends InsertionSort<T>
{
public ShellSort(Comparator<T> comparator)
{
super(comparator);
}
@Override
public void sort(T[] array, int beginIndex, int endIndex)
{
int n = endIndex - beginIndex;
for (int t=1, h=getH(n, t); h <= 1; h=getH(n, t), t++)
sort(array, beginIndex, endIndex, h);
}
protected abstract int getH(int n, int t);
}
144. Emory University Logo Guidelines
-
Shell Sort
17
public abstract class ShellSort<T extends Comparable<T>> extends InsertionSort<T>
{
public ShellSort(Comparator<T> comparator)
{
super(comparator);
}
@Override
public void sort(T[] array, int beginIndex, int endIndex)
{
int n = endIndex - beginIndex;
for (int t=1, h=getH(n, t); h <= 1; h=getH(n, t), t++)
sort(array, beginIndex, endIndex, h);
}
protected abstract int getH(int n, int t);
}
145. Emory University Logo Guidelines
-
Shell Sort
17
public abstract class ShellSort<T extends Comparable<T>> extends InsertionSort<T>
{
public ShellSort(Comparator<T> comparator)
{
super(comparator);
}
@Override
public void sort(T[] array, int beginIndex, int endIndex)
{
int n = endIndex - beginIndex;
for (int t=1, h=getH(n, t); h <= 1; h=getH(n, t), t++)
sort(array, beginIndex, endIndex, h);
}
protected abstract int getH(int n, int t);
}
146. Emory University Logo Guidelines
-
Shell Sort
17
public abstract class ShellSort<T extends Comparable<T>> extends InsertionSort<T>
{
public ShellSort(Comparator<T> comparator)
{
super(comparator);
}
@Override
public void sort(T[] array, int beginIndex, int endIndex)
{
int n = endIndex - beginIndex;
for (int t=1, h=getH(n, t); h <= 1; h=getH(n, t), t++)
sort(array, beginIndex, endIndex, h);
}
protected abstract int getH(int n, int t);
}
147. Emory University Logo Guidelines
-
Shell Sort
17
public abstract class ShellSort<T extends Comparable<T>> extends InsertionSort<T>
{
public ShellSort(Comparator<T> comparator)
{
super(comparator);
}
@Override
public void sort(T[] array, int beginIndex, int endIndex)
{
int n = endIndex - beginIndex;
for (int t=1, h=getH(n, t); h <= 1; h=getH(n, t), t++)
sort(array, beginIndex, endIndex, h);
}
protected abstract int getH(int n, int t);
}
148. Emory University Logo Guidelines
-
Shell Sort Knuth
18
@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;
}
}
@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;
}
149. Emory University Logo Guidelines
-
Shell Sort Knuth
18
@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;
}
}
@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;
}
upper bound
150. Emory University Logo Guidelines
-
Shell Sort Knuth
18
@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;
}
}
@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;
}
upper bound
151. Emory University Logo Guidelines
-
Comparison - Comparison (Random)
19
Σofcomparisons
List sizes
100 200 300 400 500 600 700 800 900 1000
Heap Shell Selection Insertion
152. Emory University Logo Guidelines
-
Comparison - Assignment (Random)
20
Σofassignments
List sizes
100 200 300 400 500 600 700 800 900 1000
Heap Shell Selection Insertion