SlideShare a Scribd company logo
NumPy sorting
(and dancing and memes):
a short review for students
Vladimir Fadeev
https://vovenur.medium.com/numpy-sorting-and-dancing-and-memes-bf3ce0fe7c3
Preface
I believe, one of the most interesting topics, which includes opportunities for both to learn
more about algorithms theory and see an example of the practical implementation, is the
topic of NumPy sorting.
This may sound a little nerdy, but let me provide a short survey of available options to
prove the suggestion above.
Structure: what can you find there?
• Short theoretical descriptions:
 The review should be meaningful.
• Dancing illustrations by AlgoRythmics project
 The survey should be demonstrative;
 and I’m crazy about things that include both art and education!
• Several memes:
 Just for fun!
numpy.sort()
So, what sorting algorithms are provided by NumPy? Of course, we are going to the
documentation page:
numpy.sort() method (version 1.19.0)
• heapsort
• mergesort
• stable (since version 1.15.0)
• quicksort (by default)
Hm, not so much. Let’s consider them step by step.
heapsort
The heapsort algorithm directly
relates to binary heaps (and this
defines the name of the algorithm).
Overall asymptotic complexity
is O(N*log2(N)).
An example of heapsort using binary
min-heap is described in
the heapq python module
documentation.
Let’s dance?
mergesort
At the first blush, this is
classical mergesort the main idea of which
can be described via the ancient idiom
“divide and conquer”:
o an array to be sorted should be split into
two parts of approximately the same size;
o each of the resulting parts is sorted
separately, for example, by the same
algorithm;
o two ordered arrays of half-size are
concatenated into one. Credits: ITMO University
Several important
facts about mergesort
• asymptotic complexity is the
same for the best, the worst and
average cases: O(N*log2(N));
• requires O(N) additional memory
for sorting of arrays (but this fact
doesn’t play a role in case of
linked lists);
• optimally works with data
structures which provide
sequential access to elements
(e.g. linked lists);
• stable.
However, what
do we read in the
NumPy
documentation:
New in version 1.17.0.
Timsort is added for better performance on already
or nearly sorted data. On random data timsort is
almost identical to mergesort. It is now used for
stable sort while quicksort is still the default sort if
none is chosen. For timsort details, refer to CPython
listsort.txt. ‘mergesort’ and ‘stable’ are mapped to
radix sort for integer data types. Radix sort is an O(n)
sort instead of O(n log n).
That is to say 1. mergesort and stable options
mean the same in the
numpy.sort();
2. a hybrid type of sorting
(Timsort algorithm) is used
instead of
“pure” mergesort (like in
method sort() and
function sorted() in CPython);
3. if elements of the array are
integers (int), O(N) radix sort
will be used.
So, we can make
intermediate conclusion
that the name mergesort in
NumPy is rather kind of
legacy.
quicksort
Let’s start from basics. In short, the
classical quicksort:
• selects a pivot element;
• devices the array into three groups:
“less”, “larger” and “equal” (than/to
pivot element);
• applies itself to the resulting groups
recursively.
Several facts about quicksort
• average asymptotic complexity is O(N*log2(N));
• the worst asymptotic complexity is O(N²);
• requires а relatively small amount of the additional memory (O(log2(N)));
• a part of the standard library of the C language (qsort());
• not stable.
But! In fact, now this
option means another
sorting algorithm too.
"New in version 1.12.0.
quicksort has been changed to introsort.
When sorting does not make enough
progress it switches to heapsort. This
implementation makes quicksort
O(n*log(n)) in the worst case."
In other words, the quicksort option means
a hybrid sorting algorithm which uses
either the quicksort or the heapsort
depending on the circumstances (usually,
it depends on the depth of the quicksort
recursion).
Yep, the second legacy name.
Table to
summarize
Name of the option Used algorithm
heapsort heapsort
mergesort timsort or radix sort
stable timsort or radix sort
quicksort introsort
Conclusion
1. Theory: reading of the documentation about
NumPy sorting inspires to learn more about
both classical and hybrid sorting algorithms.
2. Practice: reading of the documentation about
NumPy sorting allows tracing how simple
sorting methods become more complicated,
develop into hybrid ones and find their
application, including in the framework of
modern tools used by engineers and the
scientific community
(note, pandas.DataFrame.sort_values method
uses the same algorithms).
Have a nice day!
Literature
• Stephens, Rod. Essential Algorithms: A Practical Approach to Computer Algorithms. John
Wiley & Sons, 2013.
• Sedgwick, R., 1998. Algorithms in C++, Parts 1–4: Fundamentals, Data Structure, Sorting,
Searching.
Links for the visualization
• SORTING.at
• Sorting Algorithms Animations
• Min Heap sort
Do you know that in
Russian the second variant
of the heapsort’s name is
the “pyramid sorting”?

More Related Content

What's hot

Plotting data with python and pylab
Plotting data with python and pylabPlotting data with python and pylab
Plotting data with python and pylab
Giovanni Marco Dall'Olio
 
PyData NYC whatsnew NumPy-SciPy 2019
PyData NYC whatsnew NumPy-SciPy 2019PyData NYC whatsnew NumPy-SciPy 2019
PyData NYC whatsnew NumPy-SciPy 2019
Ralf Gommers
 
Pain points with M3, some things to address them and how replication works
Pain points with M3, some things to address them and how replication worksPain points with M3, some things to address them and how replication works
Pain points with M3, some things to address them and how replication works
Rob Skillington
 
Arm tools and roadmap for SVE compiler support
Arm tools and roadmap for SVE compiler supportArm tools and roadmap for SVE compiler support
Arm tools and roadmap for SVE compiler support
Linaro
 
High Performance Python - Marc Garcia
High Performance Python - Marc GarciaHigh Performance Python - Marc Garcia
High Performance Python - Marc Garcia
Marc Garcia
 
Mining top k frequent closed itemsets
Mining top k frequent closed itemsetsMining top k frequent closed itemsets
Mining top k frequent closed itemsets
yuanchung
 
Queue in C, Queue Real Life of Example
Queue in C, Queue Real Life of ExampleQueue in C, Queue Real Life of Example
Queue in C, Queue Real Life of Example
Hitesh Kumar
 
Re-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for XtextRe-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for Xtext
Edward Willink
 
Overview of ppOpen-AT/Static for ppOpen-APPL/FDM ver. 0.2.0
Overview of ppOpen-AT/Static for ppOpen-APPL/FDM ver. 0.2.0Overview of ppOpen-AT/Static for ppOpen-APPL/FDM ver. 0.2.0
Overview of ppOpen-AT/Static for ppOpen-APPL/FDM ver. 0.2.0
Takahiro Katagiri
 
8086
80868086
8086
kumaran15
 
Learning Erlang (from a Prolog dropout's perspective)
Learning Erlang (from a Prolog dropout's perspective)Learning Erlang (from a Prolog dropout's perspective)
Learning Erlang (from a Prolog dropout's perspective)
elliando dias
 
CS 542 -- Query Execution
CS 542 -- Query ExecutionCS 542 -- Query Execution
CS 542 -- Query Execution
J Singh
 
Standardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonStandardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for Python
Ralf Gommers
 
Object Detection with Tensorflow
Object Detection with TensorflowObject Detection with Tensorflow
Object Detection with Tensorflow
ElifTech
 
The Joy of SciPy
The Joy of SciPyThe Joy of SciPy
The Joy of SciPy
kammeyer
 
Assembly language part I
Assembly language part IAssembly language part I
Assembly language part I
Mohammed A. Imran
 
Sma
SmaSma
計算機結構 (習題:Nand2tetris硬體部分)
計算機結構  (習題:Nand2tetris硬體部分)計算機結構  (習題:Nand2tetris硬體部分)
計算機結構 (習題:Nand2tetris硬體部分)
鍾誠 陳鍾誠
 
Linuxconf 2011 parallel languages talk
Linuxconf 2011 parallel languages talkLinuxconf 2011 parallel languages talk
Linuxconf 2011 parallel languages talk
Lenz Gschwendtner
 
Minimum parallel binary adders with nor (nand) gates
Minimum parallel binary adders with nor (nand) gatesMinimum parallel binary adders with nor (nand) gates
Minimum parallel binary adders with nor (nand) gates
jpstudcorner
 

What's hot (20)

Plotting data with python and pylab
Plotting data with python and pylabPlotting data with python and pylab
Plotting data with python and pylab
 
PyData NYC whatsnew NumPy-SciPy 2019
PyData NYC whatsnew NumPy-SciPy 2019PyData NYC whatsnew NumPy-SciPy 2019
PyData NYC whatsnew NumPy-SciPy 2019
 
Pain points with M3, some things to address them and how replication works
Pain points with M3, some things to address them and how replication worksPain points with M3, some things to address them and how replication works
Pain points with M3, some things to address them and how replication works
 
Arm tools and roadmap for SVE compiler support
Arm tools and roadmap for SVE compiler supportArm tools and roadmap for SVE compiler support
Arm tools and roadmap for SVE compiler support
 
High Performance Python - Marc Garcia
High Performance Python - Marc GarciaHigh Performance Python - Marc Garcia
High Performance Python - Marc Garcia
 
Mining top k frequent closed itemsets
Mining top k frequent closed itemsetsMining top k frequent closed itemsets
Mining top k frequent closed itemsets
 
Queue in C, Queue Real Life of Example
Queue in C, Queue Real Life of ExampleQueue in C, Queue Real Life of Example
Queue in C, Queue Real Life of Example
 
Re-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for XtextRe-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for Xtext
 
Overview of ppOpen-AT/Static for ppOpen-APPL/FDM ver. 0.2.0
Overview of ppOpen-AT/Static for ppOpen-APPL/FDM ver. 0.2.0Overview of ppOpen-AT/Static for ppOpen-APPL/FDM ver. 0.2.0
Overview of ppOpen-AT/Static for ppOpen-APPL/FDM ver. 0.2.0
 
8086
80868086
8086
 
Learning Erlang (from a Prolog dropout's perspective)
Learning Erlang (from a Prolog dropout's perspective)Learning Erlang (from a Prolog dropout's perspective)
Learning Erlang (from a Prolog dropout's perspective)
 
CS 542 -- Query Execution
CS 542 -- Query ExecutionCS 542 -- Query Execution
CS 542 -- Query Execution
 
Standardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonStandardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for Python
 
Object Detection with Tensorflow
Object Detection with TensorflowObject Detection with Tensorflow
Object Detection with Tensorflow
 
The Joy of SciPy
The Joy of SciPyThe Joy of SciPy
The Joy of SciPy
 
Assembly language part I
Assembly language part IAssembly language part I
Assembly language part I
 
Sma
SmaSma
Sma
 
計算機結構 (習題:Nand2tetris硬體部分)
計算機結構  (習題:Nand2tetris硬體部分)計算機結構  (習題:Nand2tetris硬體部分)
計算機結構 (習題:Nand2tetris硬體部分)
 
Linuxconf 2011 parallel languages talk
Linuxconf 2011 parallel languages talkLinuxconf 2011 parallel languages talk
Linuxconf 2011 parallel languages talk
 
Minimum parallel binary adders with nor (nand) gates
Minimum parallel binary adders with nor (nand) gatesMinimum parallel binary adders with nor (nand) gates
Minimum parallel binary adders with nor (nand) gates
 

Similar to NumPy sorting (and dancing and memes): ​ a short review for students​

algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
kassahungebrie
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues lists
James Wong
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
Harry Potter
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
Luis Goldster
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
Stacksqueueslists
Fraboni Ec
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
Young Alista
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
Tony Nguyen
 
Parallel programming Comparisions
Parallel programming ComparisionsParallel programming Comparisions
Parallel programming Comparisions
Muhammad Bilal Khan
 
Big o
Big oBig o
Study on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining SortStudy on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining Sort
IRJET Journal
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
Mallikarjun Biradar
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
NANDINI SHARMA
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
ManishPrajapati78
 
Radix Sorting With No Extra Space
Radix Sorting With No Extra SpaceRadix Sorting With No Extra Space
Radix Sorting With No Extra Space
gueste5dc45
 
L1803016468
L1803016468L1803016468
L1803016468
IOSR Journals
 
Ln liers
Ln liersLn liers
Ln liers
Shodhan Kini
 
test1
test1test1
Introsort or introspective sort
Introsort or introspective sortIntrosort or introspective sort
Introsort or introspective sort
Eftykhar Mahmud
 
19 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp0219 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp02
Muhammad Aslam
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
Mary Margarat
 

Similar to NumPy sorting (and dancing and memes): ​ a short review for students​ (20)

algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
Stacksqueueslists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Parallel programming Comparisions
Parallel programming ComparisionsParallel programming Comparisions
Parallel programming Comparisions
 
Big o
Big oBig o
Big o
 
Study on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining SortStudy on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining Sort
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Radix Sorting With No Extra Space
Radix Sorting With No Extra SpaceRadix Sorting With No Extra Space
Radix Sorting With No Extra Space
 
L1803016468
L1803016468L1803016468
L1803016468
 
Ln liers
Ln liersLn liers
Ln liers
 
test1
test1test1
test1
 
Introsort or introspective sort
Introsort or introspective sortIntrosort or introspective sort
Introsort or introspective sort
 
19 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp0219 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp02
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 

Recently uploaded

Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
spdendr
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 

Recently uploaded (20)

Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 

NumPy sorting (and dancing and memes): ​ a short review for students​

  • 1. NumPy sorting (and dancing and memes): a short review for students Vladimir Fadeev https://vovenur.medium.com/numpy-sorting-and-dancing-and-memes-bf3ce0fe7c3
  • 2. Preface I believe, one of the most interesting topics, which includes opportunities for both to learn more about algorithms theory and see an example of the practical implementation, is the topic of NumPy sorting. This may sound a little nerdy, but let me provide a short survey of available options to prove the suggestion above.
  • 3. Structure: what can you find there? • Short theoretical descriptions:  The review should be meaningful. • Dancing illustrations by AlgoRythmics project  The survey should be demonstrative;  and I’m crazy about things that include both art and education! • Several memes:  Just for fun!
  • 4. numpy.sort() So, what sorting algorithms are provided by NumPy? Of course, we are going to the documentation page: numpy.sort() method (version 1.19.0) • heapsort • mergesort • stable (since version 1.15.0) • quicksort (by default) Hm, not so much. Let’s consider them step by step.
  • 5. heapsort The heapsort algorithm directly relates to binary heaps (and this defines the name of the algorithm). Overall asymptotic complexity is O(N*log2(N)). An example of heapsort using binary min-heap is described in the heapq python module documentation. Let’s dance?
  • 6. mergesort At the first blush, this is classical mergesort the main idea of which can be described via the ancient idiom “divide and conquer”: o an array to be sorted should be split into two parts of approximately the same size; o each of the resulting parts is sorted separately, for example, by the same algorithm; o two ordered arrays of half-size are concatenated into one. Credits: ITMO University
  • 7. Several important facts about mergesort • asymptotic complexity is the same for the best, the worst and average cases: O(N*log2(N)); • requires O(N) additional memory for sorting of arrays (but this fact doesn’t play a role in case of linked lists); • optimally works with data structures which provide sequential access to elements (e.g. linked lists); • stable.
  • 8. However, what do we read in the NumPy documentation: New in version 1.17.0. Timsort is added for better performance on already or nearly sorted data. On random data timsort is almost identical to mergesort. It is now used for stable sort while quicksort is still the default sort if none is chosen. For timsort details, refer to CPython listsort.txt. ‘mergesort’ and ‘stable’ are mapped to radix sort for integer data types. Radix sort is an O(n) sort instead of O(n log n).
  • 9. That is to say 1. mergesort and stable options mean the same in the numpy.sort(); 2. a hybrid type of sorting (Timsort algorithm) is used instead of “pure” mergesort (like in method sort() and function sorted() in CPython); 3. if elements of the array are integers (int), O(N) radix sort will be used.
  • 10. So, we can make intermediate conclusion that the name mergesort in NumPy is rather kind of legacy.
  • 11. quicksort Let’s start from basics. In short, the classical quicksort: • selects a pivot element; • devices the array into three groups: “less”, “larger” and “equal” (than/to pivot element); • applies itself to the resulting groups recursively.
  • 12. Several facts about quicksort • average asymptotic complexity is O(N*log2(N)); • the worst asymptotic complexity is O(N²); • requires а relatively small amount of the additional memory (O(log2(N))); • a part of the standard library of the C language (qsort()); • not stable.
  • 13. But! In fact, now this option means another sorting algorithm too. "New in version 1.12.0. quicksort has been changed to introsort. When sorting does not make enough progress it switches to heapsort. This implementation makes quicksort O(n*log(n)) in the worst case." In other words, the quicksort option means a hybrid sorting algorithm which uses either the quicksort or the heapsort depending on the circumstances (usually, it depends on the depth of the quicksort recursion). Yep, the second legacy name.
  • 14. Table to summarize Name of the option Used algorithm heapsort heapsort mergesort timsort or radix sort stable timsort or radix sort quicksort introsort
  • 15. Conclusion 1. Theory: reading of the documentation about NumPy sorting inspires to learn more about both classical and hybrid sorting algorithms. 2. Practice: reading of the documentation about NumPy sorting allows tracing how simple sorting methods become more complicated, develop into hybrid ones and find their application, including in the framework of modern tools used by engineers and the scientific community (note, pandas.DataFrame.sort_values method uses the same algorithms). Have a nice day!
  • 16. Literature • Stephens, Rod. Essential Algorithms: A Practical Approach to Computer Algorithms. John Wiley & Sons, 2013. • Sedgwick, R., 1998. Algorithms in C++, Parts 1–4: Fundamentals, Data Structure, Sorting, Searching.
  • 17. Links for the visualization • SORTING.at • Sorting Algorithms Animations • Min Heap sort
  • 18. Do you know that in Russian the second variant of the heapsort’s name is the “pyramid sorting”?