SlideShare a Scribd company logo
Sort:
Divide & Conquer
Data Structures and Algorithms
Emory University
Jinho D. Choi
QuickSort
O(n log n) O(n) O(n log n) O(n log n)
O(n log n) O(n log n) O(n log n) O(n log n)
O(n log n) O(n log n) O(n )2 O(n log n)
2
3 2 7 4 6 8 1 5Merge
Sort
2
3 2 7 4 6 8 1 5
Merge
Sort
2
3 2 7 4 6 8 1 5
Merge
Sort
2
3 2 7 4 6 8 1 5
Merge
Sort
2
32
7 4 6 8 1 5
Merge
Sort
2
32 74
6 8 1 5
Merge
Sort
2
32 74 6 8
1 5
Merge
Sort
2
32 74 6 8 1 5
Merge
Sort
2
32 74 6 8 1 5
Merge
Sort
2
3
2
74 6 8 1 5
Merge
Sort
2
32
74 6 8 1 5
Merge
Sort
2
32 74
6 8 1 5
Merge
Sort
2
32 74
6 8 1 5
Merge
Sort
2
32 74
6 8
1
5
Merge
Sort
2
32 74
6 8
1 5
Merge
Sort
2
32 74 6 81 5
Merge
Sort
2
32 74 6 81 5
Merge
Sort
2
32 74 6 8
1
5
Merge
Sort
2
3
2
74 6 8
1
5
Merge
Sort
2
32
74 6 8
1
5
Merge
Sort
2
32
7
4
6 8
1
5
Merge
Sort
2
32
7
4
6 8
1 5
Merge
Sort
2
32
7
4 6
8
1 5
Merge
Sort
2
32 74 6
8
1 5
Merge
Sort
2
32 74 6 81 5
Merge
Sort
MergeSort.java
public class MergeSort<T extends Comparable<T>> extends AbstractSort<T> {
private T[] temp;
@Override
public void sort(T[] array, int beginIndex, int endIndex) {
if (beginIndex + 1 >= endIndex) return;
int middleIndex = Utils.getMiddleIndex(beginIndex, endIndex);
// sort left partition
sort(array, beginIndex, middleIndex);
// sort Right partition
sort(array, middleIndex, endIndex);
// merge partitions
merge(array, beginIndex, middleIndex, endIndex);
}
T[] temp
Utils.getMiddleIndex()
private void copy(T[] array, int beginIndex, int endIndex) {
int N = array.length;
if (temp == null || temp.length < N)
temp = Arrays.copyOf(array, N);
else {
N = endIndex - beginIndex;
System.arraycopy(array, beginIndex, temp, beginIndex, N);
}
assignments += N;
}
Arrays.copyOf() System.arraycopy()
temp
temp
/**
* @param beginIndex the beginning index of the 1st half (inclusive).
* @param middleIndex the ending index of the 1st half (exclusive).
* @param endIndex the ending index of the 2nd half (exclusive).
*/
private void merge(T[] array, int beginIndex, int middleIndex, int endIndex) {
int fst = beginIndex, snd = middleIndex;
copy(array, beginIndex, endIndex);
for (int k = beginIndex; k < endIndex; k++) {
if (fst >= middleIndex) // no key left in the 1st half
assign(array, k, temp[snd++]);
else if (snd >= endIndex) // no key left in the 2nd half
assign(array, k, temp[fst++]);
else if (compareTo(temp, fst, snd) < 0) // 1st key < 2nd key
assign(array, k, temp[fst++]);
else
assign(array, k, temp[snd++]);
}
}
Quick Sort
3
3 2 7 4 6 8 1 5
Quick Sort
3
3 2 7 4 6 8 1 5
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 5
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 5
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 5
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 7
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 7
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 7
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 71 3
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 71
1 2 3 6 8 7 5
3
4
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 71
1 2 3 6 8 7 5
3
4
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 71
1 2 3 6 8 7 5
3
4
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 71
1 2 3 6 8 7 5
3
4
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 71
1 2 3 6 8 7 5
3
4
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 71
1 2 3 6 8 7 5
3
4
1 2 3 6 8 7 54
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 71
1 2 3 6 8 7 5
3
4
1 2 3 6 8 7 54
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 71
1 2 3 6 8 7 5
3
4
1 2 3 6 8 7 54
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 71
1 2 3 6 8 7 5
3
4
1 2 3 6 8 7 54 5 8
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 71
1 2 3 6 8 7 5
3
4
1 2 3 6 8 7 54 5 8
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 71
1 2 3 6 8 7 5
3
4
1 2 3 6 8 7 54 5 8
Pick a pivot and swap between left and right partitions.
Quick Sort
3
3 2 7 4 6 8 1 51 71
1 2 3 6 8 7 5
3
4
1 2 3 6 8 7 54 5 85 6
Pick a pivot and swap between left and right partitions.
QuickSort.java
@Override
public void sort(T[] array, int beginIndex, int endIndex) {
// at least one key in the range
if (beginIndex >= endIndex) return;
int pivotIndex = partition(array, beginIndex, endIndex);
// sort left partition
sort(array, beginIndex, pivotIndex);
// sort right partition
sort(array, pivotIndex + 1, endIndex);
}
protected int partition(T[] array, int beginIndex, int endIndex) {
int fst = beginIndex, snd = endIndex;
while (true) {
// Find where endIndex > fst > pivot
while (++fst < endIndex && compareTo(array, beginIndex, fst) >= 0);
// Find where beginIndex < snd < pivot
while (--snd > beginIndex && compareTo(array, beginIndex, snd) <= 0);
// pointers crossed
if (fst >= snd) break;
// exchange
swap(array, fst, snd);
}
// set pivot
swap(array, beginIndex, snd);
return snd;
}
O(n )2
∃
IntroSort.java
public class IntroSort<T extends Comparable<T>> extends QuickSort<T> {
private AbstractSort<T> engine;
public IntroSort(AbstractSort<T> engine, Comparator<T> comparator) {
super(comparator);
this.engine = engine;
}
@Override
public void resetCounts() {
super.resetCounts();
if (engine != null) engine.resetCounts();
}
engine
resetCount()
O(n log n)
@Override
public void sort(T[] array, int beginIndex, int endIndex) {
final int maxdepth = getMaxDepth(beginIndex, endIndex);
introsort(array, beginIndex, endIndex, maxdepth);
comparisons += engine.getComparisonCount();
assignments += engine.getAssignmentCount();
}
protected int getMaxDepth(int beginIndex, int endIndex) {
return 2 * (int) Utils.log2(endIndex - beginIndex);
}
private void introsort(T[] array, int beginIndex, int endIndex, int maxdepth) {
if (beginIndex >= endIndex) return;
if (maxdepth == 0) // encounter the worst case
engine.sort(array, beginIndex, endIndex);
else {
int pivotIndex = partition(array, beginIndex, endIndex);
introsort(array, beginIndex, pivotIndex, maxdepth - 1);
introsort(array, pivotIndex + 1, endIndex, maxdepth - 1);
}
}
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)

More Related Content

Similar to CS253: Divide & Conquer Sort (2019)

Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
BhumikaBiyani1
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 
Computer notes - Mergesort
Computer notes - MergesortComputer notes - Mergesort
Computer notes - Mergesort
ecomputernotes
 
Sorting
SortingSorting
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
MuhammadSheraz836877
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39ecomputernotes
 
08 - 25 Jan - Divide and Conquer
08 - 25 Jan - Divide and Conquer08 - 25 Jan - Divide and Conquer
08 - 25 Jan - Divide and Conquer
Neeldhara Misra
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
18Gunaalanpg
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
Kamal Singh Lodhi
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
Sorting algos > Data Structures & Algorithums
Sorting algos  > Data Structures & AlgorithumsSorting algos  > Data Structures & Algorithums
Sorting algos > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
chapter7.ppt
chapter7.pptchapter7.ppt
chapter7.ppt
Preetiverma538521
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
WrushabhShirsat3
 
chapter7 Sorting.ppt
chapter7 Sorting.pptchapter7 Sorting.ppt
chapter7 Sorting.ppt
Nielmagahod
 
A Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort AlgorithmsA Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort Algorithms
CSCJournals
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Radhika Talaviya
 
Sorting Algorithms.
Sorting Algorithms.Sorting Algorithms.
Sorting Algorithms.
Saket Kumar
 
Quick sort
Quick sortQuick sort
Quick sort
Afaq Mansoor Khan
 

Similar to CS253: Divide & Conquer Sort (2019) (20)

Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
Computer notes - Mergesort
Computer notes - MergesortComputer notes - Mergesort
Computer notes - Mergesort
 
Sorting
SortingSorting
Sorting
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39
 
08 - 25 Jan - Divide and Conquer
08 - 25 Jan - Divide and Conquer08 - 25 Jan - Divide and Conquer
08 - 25 Jan - Divide and Conquer
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Sorting algos > Data Structures & Algorithums
Sorting algos  > Data Structures & AlgorithumsSorting algos  > Data Structures & Algorithums
Sorting algos > Data Structures & Algorithums
 
chapter7.ppt
chapter7.pptchapter7.ppt
chapter7.ppt
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
 
chapter7 Sorting.ppt
chapter7 Sorting.pptchapter7 Sorting.ppt
chapter7 Sorting.ppt
 
A Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort AlgorithmsA Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort Algorithms
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
 
Sorting Algorithms.
Sorting Algorithms.Sorting Algorithms.
Sorting Algorithms.
 
Quick sort
Quick sortQuick sort
Quick sort
 

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 Resolution
Jinho Choi
 
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
Jinho Choi
 
Abstract Meaning Representation
Abstract Meaning RepresentationAbstract Meaning Representation
Abstract Meaning Representation
Jinho Choi
 
Semantic Role Labeling
Semantic Role LabelingSemantic Role Labeling
Semantic Role Labeling
Jinho Choi
 
CKY Parsing
CKY ParsingCKY Parsing
CKY Parsing
Jinho Choi
 
CS329 - WordNet Similarities
CS329 - WordNet SimilaritiesCS329 - WordNet Similarities
CS329 - WordNet Similarities
Jinho Choi
 
CS329 - Lexical Relations
CS329 - Lexical RelationsCS329 - Lexical Relations
CS329 - Lexical Relations
Jinho Choi
 
Automatic Knowledge Base Expansion for Dialogue Management
Automatic Knowledge Base Expansion for Dialogue ManagementAutomatic Knowledge Base Expansion for Dialogue Management
Automatic Knowledge Base Expansion for Dialogue Management
Jinho Choi
 
Attention is All You Need for AMR Parsing
Attention is All You Need for AMR ParsingAttention is All You Need for AMR Parsing
Attention is All You Need for AMR Parsing
Jinho Choi
 
Graph-to-Text Generation and its Applications to Dialogue
Graph-to-Text Generation and its Applications to DialogueGraph-to-Text Generation and its Applications to Dialogue
Graph-to-Text Generation and its Applications to Dialogue
Jinho Choi
 
Real-time Coreference Resolution for Dialogue Understanding
Real-time Coreference Resolution for Dialogue UnderstandingReal-time Coreference Resolution for Dialogue Understanding
Real-time Coreference Resolution for Dialogue Understanding
Jinho Choi
 
Topological Sort
Topological SortTopological Sort
Topological Sort
Jinho Choi
 
Tries - Put
Tries - PutTries - Put
Tries - Put
Jinho Choi
 
Multi-modal Embedding Learning for Early Detection of Alzheimer's Disease
Multi-modal Embedding Learning for Early Detection of Alzheimer's DiseaseMulti-modal Embedding Learning for Early Detection of Alzheimer's Disease
Multi-modal Embedding Learning for Early Detection of Alzheimer's Disease
Jinho Choi
 
Building Widely-Interpretable Semantic Networks for Dialogue Contexts
Building Widely-Interpretable Semantic Networks for Dialogue ContextsBuilding Widely-Interpretable Semantic Networks for Dialogue Contexts
Building Widely-Interpretable Semantic Networks for Dialogue Contexts
Jinho Choi
 
How to make Emora talk about Sports Intelligently
How to make Emora talk about Sports IntelligentlyHow to make Emora talk about Sports Intelligently
How to make Emora talk about Sports Intelligently
Jinho Choi
 

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

A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 

Recently uploaded (20)

A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 

CS253: Divide & Conquer Sort (2019)

  • 1. Sort: Divide & Conquer Data Structures and Algorithms Emory University Jinho D. Choi
  • 2.
  • 3. QuickSort O(n log n) O(n) O(n log n) O(n log n) O(n log n) O(n log n) O(n log n) O(n log n) O(n log n) O(n log n) O(n )2 O(n log n)
  • 4. 2 3 2 7 4 6 8 1 5Merge Sort
  • 5. 2 3 2 7 4 6 8 1 5 Merge Sort
  • 6. 2 3 2 7 4 6 8 1 5 Merge Sort
  • 7. 2 3 2 7 4 6 8 1 5 Merge Sort
  • 8. 2 32 7 4 6 8 1 5 Merge Sort
  • 9. 2 32 74 6 8 1 5 Merge Sort
  • 10. 2 32 74 6 8 1 5 Merge Sort
  • 11. 2 32 74 6 8 1 5 Merge Sort
  • 12. 2 32 74 6 8 1 5 Merge Sort
  • 13. 2 3 2 74 6 8 1 5 Merge Sort
  • 14. 2 32 74 6 8 1 5 Merge Sort
  • 15. 2 32 74 6 8 1 5 Merge Sort
  • 16. 2 32 74 6 8 1 5 Merge Sort
  • 18. 2 32 74 6 8 1 5 Merge Sort
  • 19. 2 32 74 6 81 5 Merge Sort
  • 20. 2 32 74 6 81 5 Merge Sort
  • 21. 2 32 74 6 8 1 5 Merge Sort
  • 27. 2 32 74 6 8 1 5 Merge Sort
  • 28. 2 32 74 6 81 5 Merge Sort
  • 29. MergeSort.java public class MergeSort<T extends Comparable<T>> extends AbstractSort<T> { private T[] temp; @Override public void sort(T[] array, int beginIndex, int endIndex) { if (beginIndex + 1 >= endIndex) return; int middleIndex = Utils.getMiddleIndex(beginIndex, endIndex); // sort left partition sort(array, beginIndex, middleIndex); // sort Right partition sort(array, middleIndex, endIndex); // merge partitions merge(array, beginIndex, middleIndex, endIndex); } T[] temp Utils.getMiddleIndex()
  • 30. private void copy(T[] array, int beginIndex, int endIndex) { int N = array.length; if (temp == null || temp.length < N) temp = Arrays.copyOf(array, N); else { N = endIndex - beginIndex; System.arraycopy(array, beginIndex, temp, beginIndex, N); } assignments += N; } Arrays.copyOf() System.arraycopy() temp temp
  • 31. /** * @param beginIndex the beginning index of the 1st half (inclusive). * @param middleIndex the ending index of the 1st half (exclusive). * @param endIndex the ending index of the 2nd half (exclusive). */ private void merge(T[] array, int beginIndex, int middleIndex, int endIndex) { int fst = beginIndex, snd = middleIndex; copy(array, beginIndex, endIndex); for (int k = beginIndex; k < endIndex; k++) { if (fst >= middleIndex) // no key left in the 1st half assign(array, k, temp[snd++]); else if (snd >= endIndex) // no key left in the 2nd half assign(array, k, temp[fst++]); else if (compareTo(temp, fst, snd) < 0) // 1st key < 2nd key assign(array, k, temp[fst++]); else assign(array, k, temp[snd++]); } }
  • 32. Quick Sort 3 3 2 7 4 6 8 1 5
  • 33. Quick Sort 3 3 2 7 4 6 8 1 5 Pick a pivot and swap between left and right partitions.
  • 34. Quick Sort 3 3 2 7 4 6 8 1 5 Pick a pivot and swap between left and right partitions.
  • 35. Quick Sort 3 3 2 7 4 6 8 1 5 Pick a pivot and swap between left and right partitions.
  • 36. Quick Sort 3 3 2 7 4 6 8 1 5 Pick a pivot and swap between left and right partitions.
  • 37. Quick Sort 3 3 2 7 4 6 8 1 51 7 Pick a pivot and swap between left and right partitions.
  • 38. Quick Sort 3 3 2 7 4 6 8 1 51 7 Pick a pivot and swap between left and right partitions.
  • 39. Quick Sort 3 3 2 7 4 6 8 1 51 7 Pick a pivot and swap between left and right partitions.
  • 40. Quick Sort 3 3 2 7 4 6 8 1 51 71 3 Pick a pivot and swap between left and right partitions.
  • 41. Quick Sort 3 3 2 7 4 6 8 1 51 71 1 2 3 6 8 7 5 3 4 Pick a pivot and swap between left and right partitions.
  • 42. Quick Sort 3 3 2 7 4 6 8 1 51 71 1 2 3 6 8 7 5 3 4 Pick a pivot and swap between left and right partitions.
  • 43. Quick Sort 3 3 2 7 4 6 8 1 51 71 1 2 3 6 8 7 5 3 4 Pick a pivot and swap between left and right partitions.
  • 44. Quick Sort 3 3 2 7 4 6 8 1 51 71 1 2 3 6 8 7 5 3 4 Pick a pivot and swap between left and right partitions.
  • 45. Quick Sort 3 3 2 7 4 6 8 1 51 71 1 2 3 6 8 7 5 3 4 Pick a pivot and swap between left and right partitions.
  • 46. Quick Sort 3 3 2 7 4 6 8 1 51 71 1 2 3 6 8 7 5 3 4 1 2 3 6 8 7 54 Pick a pivot and swap between left and right partitions.
  • 47. Quick Sort 3 3 2 7 4 6 8 1 51 71 1 2 3 6 8 7 5 3 4 1 2 3 6 8 7 54 Pick a pivot and swap between left and right partitions.
  • 48. Quick Sort 3 3 2 7 4 6 8 1 51 71 1 2 3 6 8 7 5 3 4 1 2 3 6 8 7 54 Pick a pivot and swap between left and right partitions.
  • 49. Quick Sort 3 3 2 7 4 6 8 1 51 71 1 2 3 6 8 7 5 3 4 1 2 3 6 8 7 54 5 8 Pick a pivot and swap between left and right partitions.
  • 50. Quick Sort 3 3 2 7 4 6 8 1 51 71 1 2 3 6 8 7 5 3 4 1 2 3 6 8 7 54 5 8 Pick a pivot and swap between left and right partitions.
  • 51. Quick Sort 3 3 2 7 4 6 8 1 51 71 1 2 3 6 8 7 5 3 4 1 2 3 6 8 7 54 5 8 Pick a pivot and swap between left and right partitions.
  • 52. Quick Sort 3 3 2 7 4 6 8 1 51 71 1 2 3 6 8 7 5 3 4 1 2 3 6 8 7 54 5 85 6 Pick a pivot and swap between left and right partitions.
  • 53. QuickSort.java @Override public void sort(T[] array, int beginIndex, int endIndex) { // at least one key in the range if (beginIndex >= endIndex) return; int pivotIndex = partition(array, beginIndex, endIndex); // sort left partition sort(array, beginIndex, pivotIndex); // sort right partition sort(array, pivotIndex + 1, endIndex); }
  • 54. protected int partition(T[] array, int beginIndex, int endIndex) { int fst = beginIndex, snd = endIndex; while (true) { // Find where endIndex > fst > pivot while (++fst < endIndex && compareTo(array, beginIndex, fst) >= 0); // Find where beginIndex < snd < pivot while (--snd > beginIndex && compareTo(array, beginIndex, snd) <= 0); // pointers crossed if (fst >= snd) break; // exchange swap(array, fst, snd); } // set pivot swap(array, beginIndex, snd); return snd; }
  • 56. IntroSort.java public class IntroSort<T extends Comparable<T>> extends QuickSort<T> { private AbstractSort<T> engine; public IntroSort(AbstractSort<T> engine, Comparator<T> comparator) { super(comparator); this.engine = engine; } @Override public void resetCounts() { super.resetCounts(); if (engine != null) engine.resetCounts(); } engine resetCount() O(n log n)
  • 57. @Override public void sort(T[] array, int beginIndex, int endIndex) { final int maxdepth = getMaxDepth(beginIndex, endIndex); introsort(array, beginIndex, endIndex, maxdepth); comparisons += engine.getComparisonCount(); assignments += engine.getAssignmentCount(); } protected int getMaxDepth(int beginIndex, int endIndex) { return 2 * (int) Utils.log2(endIndex - beginIndex); } private void introsort(T[] array, int beginIndex, int endIndex, int maxdepth) { if (beginIndex >= endIndex) return; if (maxdepth == 0) // encounter the worst case engine.sort(array, beginIndex, endIndex); else { int pivotIndex = partition(array, beginIndex, endIndex); introsort(array, beginIndex, pivotIndex, maxdepth - 1); introsort(array, pivotIndex + 1, endIndex, maxdepth - 1); } }