SlideShare a Scribd company logo
1 of 156
Download to read offline
Emory University Logo Guidelines
-
Sort:
Comparison-based
Data Structures and Algorithms
Emory University
Jinho D. Choi
Emory University Logo Guidelines
-
Sorting Algorithms
2
Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Comparison

based
Distribution

based
Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Comparison

based
Distribution

based
Selection Insertion Merge Quick
Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Comparison

based
Distribution

based
Selection
Heap
Insertion
Shell
Merge Quick
Tim Intro
Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Comparison

based
Distribution

based
Bucket
Radix
Selection
Heap
Insertion
Shell
Merge Quick
Tim Intro
Emory University Logo Guidelines
-
Sorting Algorithms
2
Sort
Comparison

based
Distribution

based
Bucket
Radix
Selection
Heap
Insertion
Shell
Merge Quick
vs.?
Tim Intro
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
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
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
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;
}
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;
}
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;
}
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
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
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;
}
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
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
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
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);
}
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);
}
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);
}
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);
}
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
Emory University Logo Guidelines
-
Selection-based Sort
7
Emory University Logo Guidelines
-
Selection-based Sort
• For each key ki in L[0, n-2]:
7
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
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
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)
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)
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)
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)
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)
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);
}
}
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)
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)
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
Heap Sort
9
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
6
2
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
6
2
6 2
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
6
2
6 2
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
6
2
7
5
6 2
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
6
2
7
5
6 27 5
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
6
2
7
5
6 27 5
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
3
6 27 5
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
3
6 27 57 3
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
3
6 27 57 3
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 3
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
Sink all non-leaf nodes to construct a heap.
6
2
7
5
7
35
3
6 27 57 35 3
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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.
Emory University Logo Guidelines
-
3 2 5 4 6 7 1
3
2 5
4 6 7 1
Heap Sort
9
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?
Emory University Logo Guidelines
-
private int getParentIndex(int beginIndex, int k)
{
return beginIndex + (k-beginIndex)/2 - 1;
}
private int getLeftChildIndex(int beginIndex, int k)
{
return beginIndex + 2*(k-beginIndex) + 1;
}
Heap Sort
10
public void sort(T[] array, int beginIndex, int endIndex)
{
for (int k=getParentIndex(beginIndex, endIndex); k>=beginIndex; k--)
sink(array, k, beginIndex, endIndex);
while (endIndex > beginIndex+1)
{
swap(array, beginIndex, --endIndex);
sink(array, beginIndex, beginIndex, endIndex);
}
}
Emory University Logo Guidelines
-
private int getParentIndex(int beginIndex, int k)
{
return beginIndex + (k-beginIndex)/2 - 1;
}
private int getLeftChildIndex(int beginIndex, int k)
{
return beginIndex + 2*(k-beginIndex) + 1;
}
Heap Sort
10
public void sort(T[] array, int beginIndex, int endIndex)
{
for (int k=getParentIndex(beginIndex, endIndex); k>=beginIndex; k--)
sink(array, k, beginIndex, endIndex);
while (endIndex > beginIndex+1)
{
swap(array, beginIndex, --endIndex);
sink(array, beginIndex, beginIndex, endIndex);
}
}
Emory University Logo Guidelines
-
private int getParentIndex(int beginIndex, int k)
{
return beginIndex + (k-beginIndex)/2 - 1;
}
private int getLeftChildIndex(int beginIndex, int k)
{
return beginIndex + 2*(k-beginIndex) + 1;
}
Heap Sort
10
public void sort(T[] array, int beginIndex, int endIndex)
{
for (int k=getParentIndex(beginIndex, endIndex); k>=beginIndex; k--)
sink(array, k, beginIndex, endIndex);
while (endIndex > beginIndex+1)
{
swap(array, beginIndex, --endIndex);
sink(array, beginIndex, beginIndex, endIndex);
}
}
Emory University Logo Guidelines
-
private int getParentIndex(int beginIndex, int k)
{
return beginIndex + (k-beginIndex)/2 - 1;
}
private int getLeftChildIndex(int beginIndex, int k)
{
return beginIndex + 2*(k-beginIndex) + 1;
}
Heap Sort
10
public void sort(T[] array, int beginIndex, int endIndex)
{
for (int k=getParentIndex(beginIndex, endIndex); k>=beginIndex; k--)
sink(array, k, beginIndex, endIndex);
while (endIndex > beginIndex+1)
{
swap(array, beginIndex, --endIndex);
sink(array, beginIndex, beginIndex, endIndex);
}
}
Emory University Logo Guidelines
-
Insertion-based Sort
11
Emory University Logo Guidelines
-
Insertion-based Sort
• For each key ki in L[1, n-1]:
11
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
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)
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)
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)
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);
}
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);
}
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?
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
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
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
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
Emory University Logo Guidelines
-
Shell Sort
13
41 1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728293031323334353637383940 0
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:
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:
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:
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
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
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, …
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 4
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
3 5
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
3 5
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
3 5
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
3 5
Emory University Logo Guidelines
-
Shell Sort
14
3 2 5 4 6 7 1
Hibbard Sequence: 1, 3, 7, 15, …
k1 = 3
1 41 3
k0 = 1
3 5 4 7
Emory University Logo Guidelines
-
Shell Sort
14
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
Emory University Logo Guidelines
-
Shell Sort
14
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
Emory University Logo Guidelines
-
Shell Sort
14
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
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);
...
}
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);
...
}
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
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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;
}
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
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
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
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
Emory University Logo Guidelines
-
Comparison - Speed (Random)
21
Σof1Kiterations(ms)
0
1500
3000
4500
6000
List sizes
100 200 300 400 500 600 700 800 900 1000
Heap Shell Selection Insertion
Emory University Logo Guidelines
-
Comparison - Speed (Ascending)
22
Σof1Kiterations(ms)
0
1000
2000
3000
4000
List sizes
100 200 300 400 500 600 700 800 900 1000
Heap Shell Selection Insertion
Emory University Logo Guidelines
-
Comparison - Speed (Descending)
23
Σof1Kiterations(ms)
0
3500
7000
10500
14000
List sizes
100 200 300 400 500 600 700 800 900 1000
Heap Shell Selection Insertion
Emory University Logo Guidelines
-
Agenda
• Exercise
- https://github.com/emory-courses/cs323/wiki/Divide-and-Conquer-Sort
• Reading
- https://en.wikipedia.org/wiki/Merge_sort
- https://en.wikipedia.org/wiki/Quicksort
- https://en.wikipedia.org/wiki/Introsort
- https://en.wikipedia.org/wiki/Timsort
24

More Related Content

What's hot

CS323: Longest Common Subsequences
CS323: Longest Common SubsequencesCS323: Longest Common Subsequences
CS323: Longest Common SubsequencesJinho Choi
 
09 logic programming
09 logic programming09 logic programming
09 logic programmingsaru40
 
Prolog programming
Prolog programmingProlog programming
Prolog programmingHarry Potter
 
Declare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term RewritingDeclare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term RewritingEelco Visser
 
Declare Your Language: Type Checking
Declare Your Language: Type CheckingDeclare Your Language: Type Checking
Declare Your Language: Type CheckingEelco Visser
 
Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name ResolutionEelco Visser
 
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6sumitbardhan
 
Numeric Range Queries in Lucene and Solr
Numeric Range Queries in Lucene and SolrNumeric Range Queries in Lucene and Solr
Numeric Range Queries in Lucene and SolrVadim Kirilchuk
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programaciónSoftware Guru
 
Declare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) ServicesDeclare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) ServicesEelco Visser
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionPython Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionNandan Sawant
 
Babar: Knowledge Recognition, Extraction and Representation
Babar: Knowledge Recognition, Extraction and RepresentationBabar: Knowledge Recognition, Extraction and Representation
Babar: Knowledge Recognition, Extraction and RepresentationPierre de Lacaze
 
A Preliminary survey of RDF/Neo4j as backends for KnetMiner
A Preliminary survey of RDF/Neo4j as backends for KnetMinerA Preliminary survey of RDF/Neo4j as backends for KnetMiner
A Preliminary survey of RDF/Neo4j as backends for KnetMinerRothamsted Research, UK
 
final report.doc
final report.docfinal report.doc
final report.docbutest
 
Arrays searching-sorting
Arrays searching-sortingArrays searching-sorting
Arrays searching-sortingAjharul Abedeen
 

What's hot (20)

CS323: Longest Common Subsequences
CS323: Longest Common SubsequencesCS323: Longest Common Subsequences
CS323: Longest Common Subsequences
 
09 logic programming
09 logic programming09 logic programming
09 logic programming
 
Prolog programming
Prolog programmingProlog programming
Prolog programming
 
Declare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term RewritingDeclare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term Rewriting
 
Declare Your Language: Type Checking
Declare Your Language: Type CheckingDeclare Your Language: Type Checking
Declare Your Language: Type Checking
 
Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name Resolution
 
C tutorial
C tutorialC tutorial
C tutorial
 
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6
 
Numeric Range Queries in Lucene and Solr
Numeric Range Queries in Lucene and SolrNumeric Range Queries in Lucene and Solr
Numeric Range Queries in Lucene and Solr
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
 
Declare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) ServicesDeclare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) Services
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionPython Puzzlers - 2016 Edition
Python Puzzlers - 2016 Edition
 
F# for C# Programmers
F# for C# ProgrammersF# for C# Programmers
F# for C# Programmers
 
Babar: Knowledge Recognition, Extraction and Representation
Babar: Knowledge Recognition, Extraction and RepresentationBabar: Knowledge Recognition, Extraction and Representation
Babar: Knowledge Recognition, Extraction and Representation
 
A Preliminary survey of RDF/Neo4j as backends for KnetMiner
A Preliminary survey of RDF/Neo4j as backends for KnetMinerA Preliminary survey of RDF/Neo4j as backends for KnetMiner
A Preliminary survey of RDF/Neo4j as backends for KnetMiner
 
Erlang session1
Erlang session1Erlang session1
Erlang session1
 
Plc part 4
Plc  part 4Plc  part 4
Plc part 4
 
final report.doc
final report.docfinal report.doc
final report.doc
 
Arrays searching-sorting
Arrays searching-sortingArrays searching-sorting
Arrays searching-sorting
 
R Intro Workshop
R Intro Workshop R Intro Workshop
R Intro Workshop
 

Similar to Emory University Logo Guidelines and Sorting Algorithms

Ctes percona live_2017
Ctes percona live_2017Ctes percona live_2017
Ctes percona live_2017Guilhem Bichot
 
Query-porcessing-& Query optimization
Query-porcessing-& Query optimizationQuery-porcessing-& Query optimization
Query-porcessing-& Query optimizationSaranya Natarajan
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
SQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQLSQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQLJerry Yang
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxmonicafrancis71118
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxcargillfilberto
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxdrandy1
 
Tag recommendation in social bookmarking sites like deli
Tag recommendation in social bookmarking sites like deliTag recommendation in social bookmarking sites like deli
Tag recommendation in social bookmarking sites like deliVinay Singri
 
Mining Source Code Improvement Patterns from Similar Code Review Works
Mining Source Code Improvement Patterns from Similar Code Review WorksMining Source Code Improvement Patterns from Similar Code Review Works
Mining Source Code Improvement Patterns from Similar Code Review WorksYuki Ueda
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer OverviewOlav Sandstå
 
Sql Patterns
Sql PatternsSql Patterns
Sql Patternsphanleson
 
Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03Avelin Huo
 
SQL Data Manipulation Language presentation
SQL Data Manipulation Language presentationSQL Data Manipulation Language presentation
SQL Data Manipulation Language presentationanandapriya
 
intro-slides.pdf very important for computer science students
intro-slides.pdf very important for computer science studentsintro-slides.pdf very important for computer science students
intro-slides.pdf very important for computer science studentssairevanth504
 

Similar to Emory University Logo Guidelines and Sorting Algorithms (20)

Ctes percona live_2017
Ctes percona live_2017Ctes percona live_2017
Ctes percona live_2017
 
Query-porcessing-& Query optimization
Query-porcessing-& Query optimizationQuery-porcessing-& Query optimization
Query-porcessing-& Query optimization
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
SQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQLSQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQL
 
Predicting the relevance of search results for e-commerce systems
Predicting the relevance of search results for e-commerce systemsPredicting the relevance of search results for e-commerce systems
Predicting the relevance of search results for e-commerce systems
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
R meetup talk
R meetup talkR meetup talk
R meetup talk
 
Tag recommendation in social bookmarking sites like deli
Tag recommendation in social bookmarking sites like deliTag recommendation in social bookmarking sites like deli
Tag recommendation in social bookmarking sites like deli
 
Mining Source Code Improvement Patterns from Similar Code Review Works
Mining Source Code Improvement Patterns from Similar Code Review WorksMining Source Code Improvement Patterns from Similar Code Review Works
Mining Source Code Improvement Patterns from Similar Code Review Works
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer Overview
 
Sql Patterns
Sql PatternsSql Patterns
Sql Patterns
 
Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03
 
sql-dml.ppt
sql-dml.pptsql-dml.ppt
sql-dml.ppt
 
SQL Data Manipulation Language presentation
SQL Data Manipulation Language presentationSQL Data Manipulation Language presentation
SQL Data Manipulation Language presentation
 
R seminar dplyr package
R seminar dplyr packageR seminar dplyr package
R seminar dplyr package
 
intro-slides.pdf very important for computer science students
intro-slides.pdf very important for computer science studentsintro-slides.pdf very important for computer science students
intro-slides.pdf very important for computer science students
 
Chapter15
Chapter15Chapter15
Chapter15
 

More from Jinho Choi

Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...Jinho Choi
 
Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...
Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...
Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...Jinho Choi
 
Competence-Level Prediction and Resume & Job Description Matching Using Conte...
Competence-Level Prediction and Resume & Job Description Matching Using Conte...Competence-Level Prediction and Resume & Job Description Matching Using Conte...
Competence-Level Prediction and Resume & Job Description Matching Using Conte...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 ResolutionJinho 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 RepresentationJinho Choi
 
Semantic Role Labeling
Semantic Role LabelingSemantic Role Labeling
Semantic Role LabelingJinho Choi
 
CS329 - WordNet Similarities
CS329 - WordNet SimilaritiesCS329 - WordNet Similarities
CS329 - WordNet SimilaritiesJinho Choi
 
CS329 - Lexical Relations
CS329 - Lexical RelationsCS329 - Lexical Relations
CS329 - Lexical RelationsJinho 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 ManagementJinho 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 ParsingJinho 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 DialogueJinho 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 UnderstandingJinho Choi
 
Topological Sort
Topological SortTopological Sort
Topological SortJinho 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 DiseaseJinho 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 ContextsJinho 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 IntelligentlyJinho Choi
 

More from Jinho Choi (20)

Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
 
Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...
Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...
Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...
 
Competence-Level Prediction and Resume & Job Description Matching Using Conte...
Competence-Level Prediction and Resume & Job Description Matching Using Conte...Competence-Level Prediction and Resume & Job Description Matching Using Conte...
Competence-Level Prediction and Resume & Job Description Matching Using Conte...
 
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
 

Recently uploaded

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 

Recently uploaded (20)

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 

Emory University Logo Guidelines and Sorting Algorithms

  • 1. Emory University Logo Guidelines - Sort: Comparison-based Data Structures and Algorithms Emory University Jinho D. Choi
  • 2. Emory University Logo Guidelines - Sorting Algorithms 2
  • 3. Emory University Logo Guidelines - Sorting Algorithms 2 Sort
  • 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
  • 26. Emory University Logo Guidelines - Selection-based Sort 7
  • 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)
  • 38. Emory University Logo Guidelines - 3 2 5 4 6 7 1 Heap Sort 9
  • 39. Emory University Logo Guidelines - 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap.
  • 40. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap.
  • 41. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap.
  • 42. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap. 6 2
  • 43. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap. 6 2 6 2
  • 44. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap. 6 2 6 2
  • 45. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap. 6 2 7 5 6 2
  • 46. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap. 6 2 7 5 6 27 5
  • 47. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap. 6 2 7 5 6 27 5
  • 48. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 3 6 27 5
  • 49. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 3 6 27 57 3
  • 50. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 3 6 27 57 3
  • 51. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 3
  • 52. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 Sink all non-leaf nodes to construct a heap. 6 2 7 5 7 35 3 6 27 57 35 3
  • 53. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 54. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 55. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 56. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 57. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 58. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 59. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 60. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 61. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 62. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 63. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 64. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 65. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 66. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 67. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 68. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 69. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 70. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 71. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 72. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 73. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 74. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 75. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 76. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 77. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 78. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 79. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 80. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 81. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 82. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 83. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 84. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 85. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 86. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 87. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 88. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 89. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 90. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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.
  • 91. Emory University Logo Guidelines - 3 2 5 4 6 7 1 3 2 5 4 6 7 1 Heap Sort 9 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?
  • 92. Emory University Logo Guidelines - private int getParentIndex(int beginIndex, int k) { return beginIndex + (k-beginIndex)/2 - 1; } private int getLeftChildIndex(int beginIndex, int k) { return beginIndex + 2*(k-beginIndex) + 1; } Heap Sort 10 public void sort(T[] array, int beginIndex, int endIndex) { for (int k=getParentIndex(beginIndex, endIndex); k>=beginIndex; k--) sink(array, k, beginIndex, endIndex); while (endIndex > beginIndex+1) { swap(array, beginIndex, --endIndex); sink(array, beginIndex, beginIndex, endIndex); } }
  • 93. Emory University Logo Guidelines - private int getParentIndex(int beginIndex, int k) { return beginIndex + (k-beginIndex)/2 - 1; } private int getLeftChildIndex(int beginIndex, int k) { return beginIndex + 2*(k-beginIndex) + 1; } Heap Sort 10 public void sort(T[] array, int beginIndex, int endIndex) { for (int k=getParentIndex(beginIndex, endIndex); k>=beginIndex; k--) sink(array, k, beginIndex, endIndex); while (endIndex > beginIndex+1) { swap(array, beginIndex, --endIndex); sink(array, beginIndex, beginIndex, endIndex); } }
  • 94. Emory University Logo Guidelines - private int getParentIndex(int beginIndex, int k) { return beginIndex + (k-beginIndex)/2 - 1; } private int getLeftChildIndex(int beginIndex, int k) { return beginIndex + 2*(k-beginIndex) + 1; } Heap Sort 10 public void sort(T[] array, int beginIndex, int endIndex) { for (int k=getParentIndex(beginIndex, endIndex); k>=beginIndex; k--) sink(array, k, beginIndex, endIndex); while (endIndex > beginIndex+1) { swap(array, beginIndex, --endIndex); sink(array, beginIndex, beginIndex, endIndex); } }
  • 95. Emory University Logo Guidelines - private int getParentIndex(int beginIndex, int k) { return beginIndex + (k-beginIndex)/2 - 1; } private int getLeftChildIndex(int beginIndex, int k) { return beginIndex + 2*(k-beginIndex) + 1; } Heap Sort 10 public void sort(T[] array, int beginIndex, int endIndex) { for (int k=getParentIndex(beginIndex, endIndex); k>=beginIndex; k--) sink(array, k, beginIndex, endIndex); while (endIndex > beginIndex+1) { swap(array, beginIndex, --endIndex); sink(array, beginIndex, beginIndex, endIndex); } }
  • 96. Emory University Logo Guidelines - Insertion-based Sort 11
  • 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
  • 109. Emory University Logo Guidelines - Shell Sort 13 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, …
  • 116. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1
  • 117. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, …
  • 118. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3
  • 119. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3
  • 120. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 4
  • 121. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3
  • 122. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3
  • 123. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3
  • 124. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3
  • 125. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1
  • 126. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1
  • 127. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1
  • 128. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1
  • 129. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1 3 5
  • 130. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1 3 5
  • 131. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1 3 5
  • 132. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1 3 5
  • 133. Emory University Logo Guidelines - Shell Sort 14 3 2 5 4 6 7 1 Hibbard Sequence: 1, 3, 7, 15, … k1 = 3 1 41 3 k0 = 1 3 5 4 7
  • 134. Emory University Logo Guidelines - Shell Sort 14 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
  • 135. Emory University Logo Guidelines - Shell Sort 14 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
  • 136. Emory University Logo Guidelines - Shell Sort 14 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
  • 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
  • 153. Emory University Logo Guidelines - Comparison - Speed (Random) 21 Σof1Kiterations(ms) 0 1500 3000 4500 6000 List sizes 100 200 300 400 500 600 700 800 900 1000 Heap Shell Selection Insertion
  • 154. Emory University Logo Guidelines - Comparison - Speed (Ascending) 22 Σof1Kiterations(ms) 0 1000 2000 3000 4000 List sizes 100 200 300 400 500 600 700 800 900 1000 Heap Shell Selection Insertion
  • 155. Emory University Logo Guidelines - Comparison - Speed (Descending) 23 Σof1Kiterations(ms) 0 3500 7000 10500 14000 List sizes 100 200 300 400 500 600 700 800 900 1000 Heap Shell Selection Insertion
  • 156. Emory University Logo Guidelines - Agenda • Exercise - https://github.com/emory-courses/cs323/wiki/Divide-and-Conquer-Sort • Reading - https://en.wikipedia.org/wiki/Merge_sort - https://en.wikipedia.org/wiki/Quicksort - https://en.wikipedia.org/wiki/Introsort - https://en.wikipedia.org/wiki/Timsort 24