SlideShare a Scribd company logo
Sort:
Divide & Conquer
Data Structures and Algorithms
Emory University
Jinho D. Choi
CS253: Divide & Conquer Sort (2019)
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

PPT
Data Structures- Part4 basic sorting algorithms
PPT
Chapter 11 - Sorting and Searching
PDF
12X1 T01 01 log laws
PDF
11 x1 t01 01 algebra & indices (2014)
PPT
Shell sort
PDF
Chapter 8 advanced sorting and hashing for print
PPT
Shell sorting
 
PPTX
Insertion Sorting
Data Structures- Part4 basic sorting algorithms
Chapter 11 - Sorting and Searching
12X1 T01 01 log laws
11 x1 t01 01 algebra & indices (2014)
Shell sort
Chapter 8 advanced sorting and hashing for print
Shell sorting
 
Insertion Sorting

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

PPTX
Unit 2 - Quick Sort.pptx
PPT
Data Structure Sorting
PPT
quicksort (1).ppt
PPTX
Unit 5 internal sorting &amp; files
PPT
quick_sort_with_explanationandImplmentation.ppt
PPTX
Quick sort
PPT
Sorting algos > Data Structures & Algorithums
PPTX
Sortings .pptx
PPTX
Lecture 3.3.4 Quick sort.pptxIIIIIIIIIII
PDF
Quick sort
PPTX
Divide-and-conquer
DOCX
Assignment 2 data structures National University Of Modern Languages
PPTX
Quick sort by Sania Nisar
PPT
quicksort.ppthhhhhhhhhhhhhhhhhhhhhhhhhhh
PPT
Quicksort and MergeSort Algorithm Analysis
PDF
Quick sort
PPT
quicksort.ppt
PPT
quicksort (1).ppt
PPT
Quick & Merge Sort.ppt
PPT
quicksort.ppt
Unit 2 - Quick Sort.pptx
Data Structure Sorting
quicksort (1).ppt
Unit 5 internal sorting &amp; files
quick_sort_with_explanationandImplmentation.ppt
Quick sort
Sorting algos > Data Structures & Algorithums
Sortings .pptx
Lecture 3.3.4 Quick sort.pptxIIIIIIIIIII
Quick sort
Divide-and-conquer
Assignment 2 data structures National University Of Modern Languages
Quick sort by Sania Nisar
quicksort.ppthhhhhhhhhhhhhhhhhhhhhhhhhhh
Quicksort and MergeSort Algorithm Analysis
Quick sort
quicksort.ppt
quicksort (1).ppt
Quick & Merge Sort.ppt
quicksort.ppt
Ad

More from Jinho Choi (20)

PDF
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
PDF
Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...
PDF
Competence-Level Prediction and Resume & Job Description Matching Using Conte...
PDF
Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...
PDF
The Myth of Higher-Order Inference in Coreference Resolution
PDF
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
PDF
Abstract Meaning Representation
PDF
Semantic Role Labeling
PDF
CKY Parsing
PDF
CS329 - WordNet Similarities
PDF
CS329 - Lexical Relations
PDF
Automatic Knowledge Base Expansion for Dialogue Management
PDF
Attention is All You Need for AMR Parsing
PDF
Graph-to-Text Generation and its Applications to Dialogue
PDF
Real-time Coreference Resolution for Dialogue Understanding
PDF
Topological Sort
PDF
Tries - Put
PDF
Multi-modal Embedding Learning for Early Detection of Alzheimer's Disease
PDF
Building Widely-Interpretable Semantic Networks for Dialogue Contexts
PDF
How to make Emora talk about Sports Intelligently
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...
Competence-Level Prediction and Resume & Job Description Matching Using Conte...
Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...
The Myth of Higher-Order Inference in Coreference Resolution
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
Abstract Meaning Representation
Semantic Role Labeling
CKY Parsing
CS329 - WordNet Similarities
CS329 - Lexical Relations
Automatic Knowledge Base Expansion for Dialogue Management
Attention is All You Need for AMR Parsing
Graph-to-Text Generation and its Applications to Dialogue
Real-time Coreference Resolution for Dialogue Understanding
Topological Sort
Tries - Put
Multi-modal Embedding Learning for Early Detection of Alzheimer's Disease
Building Widely-Interpretable Semantic Networks for Dialogue Contexts
How to make Emora talk about Sports Intelligently
Ad

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Spectroscopy.pptx food analysis technology
PDF
cuic standard and advanced reporting.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Encapsulation_ Review paper, used for researhc scholars
Network Security Unit 5.pdf for BCA BBA.
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
sap open course for s4hana steps from ECC to s4
Reach Out and Touch Someone: Haptics and Empathic Computing
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Unlocking AI with Model Context Protocol (MCP)
MIND Revenue Release Quarter 2 2025 Press Release
Chapter 3 Spatial Domain Image Processing.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Dropbox Q2 2025 Financial Results & Investor Presentation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Spectroscopy.pptx food analysis technology
cuic standard and advanced reporting.pdf
Programs and apps: productivity, graphics, security and other tools
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11

CS253: Divide & Conquer Sort (2019)

  • 1. Sort: Divide & Conquer Data Structures and Algorithms Emory University Jinho D. Choi
  • 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); } }