SlideShare a Scribd company logo
Sorting
Arvind Devaraj
Sorting
• Given an array, put the elements in order
– Numerical or lexicographic
• Desirable characteristics
– Fast
– In place (don’t need a second array)
– Stability
Insertion Sort
• Simple, able to handle any data
• Grow a sorted array from the beginning
– Create an empty array of the proper size
– Pick the elements one at a time in any order
– Put them in the new array in sorted order
• If the element is not last, make room for it
– Repeat until done
• Can be done in place if well designed
Insertion Sort
90 11 27 37111631 4
Insertion Sort
90 11 27 37111631 4
90
Insertion Sort
90 11 27 37111631 4
9011
Insertion Sort
90 11 27
90
37111631 4
2711
Insertion Sort
90 11 27
31 90
37111631 4
2711
Insertion Sort
90 11 27
27 31 90
37111631 4
114
Insertion Sort
90 11 27
16 27 31 90
37111631 4
114
Insertion Sort
90 11 27
11 16 27 31 90
37111631 4
114
Insertion Sort
90 11 27
11 16 27 31 37 90
37111631 4
114
Merge Sort
• Fast, able to handle any data
– But can’t be done in place
• View the array as a set of small sorted arrays
– Initially only the 1-element “arrays” are sorted
• Merge pairs of sorted arrays
– Repeatedly choose the smallest element in each
– This produces sorted arrays that are twice as long
• Repeat until only one array remains
Merge Sort
90 11 27 37111631 4
9011
Merge Sort
90 11 27
27 31
37111631 4
9011
Merge Sort
90 11 27
27 31 4 16
37111631 4
9011
Merge Sort
90 11 27
27 31 4 16 11 37
37111631 4
9011
Merge Sort
11 27 31
27 31 4 16 11 37
90
9011
Merge Sort
11 27 31
27 31 4 16 11 37
37161190 4
9011
Merge Sort
11 27 31
11 16 27 31 37 90
37161190 4
114
Divide and Conquer
• Split a problem into simpler subproblems
– Keep doing that until trivial subproblems result
• Solve the trivial subproblems
• Combine the results to solve a larger problem
– Keep doing that until the full problem is solved
• Merge sort illustrates divide and conquer
– But it is a general strategy that is often helpful
Quick Sort
• For example, given
80 38 95 84 99 10 79 44 26 87 96 12 43 81 3
we can select the middle entry, 44, and sort
the remaining entries into two groups, those
less than 44 and those greater than 44:
38 10 26 12 43 3 44 80 95 84 99 79 87 96 81
• If we sort each sub-list, we will have sorted
the entire array
A sample heap
• Each node is larger than its children
19
1418
22
321
14
119
15
25
1722
Sorting using Heaps
• What do heaps have to do with sorting an array?
Because the binary tree is balanced and left justified, it can be
represented as an array
– All our operations on binary trees can be represented as
operations on arrays
– To sort:
heapify the array;
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}
Summary of Sorting Algorithms
• in-place, randomized
• fastest (good for large inputs)
O(n log n)
expected
quick-sort
• sequential data access
• fast (good for huge inputs)
O(n log n)merge-sort
• in-place
• fast (good for large inputs)
O(n log n)heap-sort
O(n2)
O(n2)
Time
insertion-sort
selection-sort
Algorithm Notes
• in-place
• slow (good for small inputs)
• in-place
• slow (good for small inputs)

More Related Content

What's hot

Implementing Merge Sort
Implementing Merge SortImplementing Merge Sort
Implementing Merge Sort
smita gupta
 
Search methods
Search methodsSearch methods
Search methods
zahraa F.Muhsen
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
Preetham Devisetty
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
Tribhuvan University
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
LJ Projects
 
Sorting (Bubble,Merge,Selection sort)
Sorting (Bubble,Merge,Selection sort)Sorting (Bubble,Merge,Selection sort)
Sorting (Bubble,Merge,Selection sort)
oDesk
 
Sorting and Its Types
Sorting and Its TypesSorting and Its Types
Sorting and Its Types
amberkhan59
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
Archana Burujwale
 
Types of algorithms
Types of algorithmsTypes of algorithms
Types of algorithms
Amelita Martinez
 
Matlab ch1 (4)
Matlab ch1 (4)Matlab ch1 (4)
Matlab ch1 (4)
mohsinggg
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
Aamir Sohail
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsAakash deep Singhal
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
Hanif Durad
 
Matlab ch1 (2)
Matlab ch1 (2)Matlab ch1 (2)
Matlab ch1 (2)
mohsinggg
 
Hashing
HashingHashing
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
mbadhi barnabas
 

What's hot (19)

Implementing Merge Sort
Implementing Merge SortImplementing Merge Sort
Implementing Merge Sort
 
Search methods
Search methodsSearch methods
Search methods
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Sorting (Bubble,Merge,Selection sort)
Sorting (Bubble,Merge,Selection sort)Sorting (Bubble,Merge,Selection sort)
Sorting (Bubble,Merge,Selection sort)
 
Sorting and Its Types
Sorting and Its TypesSorting and Its Types
Sorting and Its Types
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
Types of algorithms
Types of algorithmsTypes of algorithms
Types of algorithms
 
Matlab ch1 (4)
Matlab ch1 (4)Matlab ch1 (4)
Matlab ch1 (4)
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Matlab ch1 (2)
Matlab ch1 (2)Matlab ch1 (2)
Matlab ch1 (2)
 
Hashing
HashingHashing
Hashing
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
 

Viewers also liked

Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
Shantanu Mishra
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Kaushal Shah
 
Cleaning and sorting data
Cleaning and sorting dataCleaning and sorting data
Cleaning and sorting data
Nina Sandlin
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
aditya raj
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
Gail Carmichael
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
irdginfo
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
Brett Duncan
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
Madhu Bala
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Trupti Agrawal
 

Viewers also liked (19)

Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Cleaning and sorting data
Cleaning and sorting dataCleaning and sorting data
Cleaning and sorting data
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Quicksort
QuicksortQuicksort
Quicksort
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting
SortingSorting
Sorting
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 

Similar to Sorting (introduction)

Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexity
Motaleb Hossen Manik
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
Dabbal Singh Mahara
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
FarihaHabib123
 
L 14-ct1120
L 14-ct1120L 14-ct1120
L 14-ct1120
Zia Ush Shamszaman
 
1.4 Sorting.pptx
1.4 Sorting.pptx1.4 Sorting.pptx
1.4 Sorting.pptx
Sujan527908
 
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
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
VarchasvaTiwari2
 
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 Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
Abu Shaik saurab
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sortingFadhil Ismail
 
Bubble Sort presentation.pptx
Bubble Sort presentation.pptxBubble Sort presentation.pptx
Bubble Sort presentation.pptx
TusharTikia
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Manek Ar
 
Sorting
SortingSorting
Sorting
Amar Jukuntla
 
Sorting
SortingSorting
Sorting
Amar Jukuntla
 

Similar to Sorting (introduction) (15)

Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexity
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
L 14-ct1120
L 14-ct1120L 14-ct1120
L 14-ct1120
 
1.4 Sorting.pptx
1.4 Sorting.pptx1.4 Sorting.pptx
1.4 Sorting.pptx
 
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...
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
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 Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
 
Bubble Sort presentation.pptx
Bubble Sort presentation.pptxBubble Sort presentation.pptx
Bubble Sort presentation.pptx
 
16 mergesort
16 mergesort16 mergesort
16 mergesort
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 

More from Arvind Devaraj

Deep learning for NLP and Transformer
 Deep learning for NLP  and Transformer Deep learning for NLP  and Transformer
Deep learning for NLP and Transformer
Arvind Devaraj
 
NLP using transformers
NLP using transformers NLP using transformers
NLP using transformers
Arvind Devaraj
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
Arvind Devaraj
 
Career hunt pitch
Career hunt pitchCareer hunt pitch
Career hunt pitch
Arvind Devaraj
 
Career options for CS and IT students
Career options for CS and IT studentsCareer options for CS and IT students
Career options for CS and IT students
Arvind Devaraj
 
Careerhunt ebook
Careerhunt ebookCareerhunt ebook
Careerhunt ebook
Arvind Devaraj
 
Static Analysis of Computer programs
Static Analysis of Computer programs Static Analysis of Computer programs
Static Analysis of Computer programs
Arvind Devaraj
 
Hyperbook
HyperbookHyperbook
Hyperbook
Arvind Devaraj
 
Yourstory Android Workshop
Yourstory Android WorkshopYourstory Android Workshop
Yourstory Android WorkshopArvind Devaraj
 
Android High performance in GPU using opengles and renderscript
Android High performance in GPU using opengles and renderscriptAndroid High performance in GPU using opengles and renderscript
Android High performance in GPU using opengles and renderscriptArvind Devaraj
 
OpenGLES Android Graphics
OpenGLES Android GraphicsOpenGLES Android Graphics
OpenGLES Android Graphics
Arvind Devaraj
 
Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
Arvind Devaraj
 
AIDL - Android Interface Definition Language
AIDL  - Android Interface Definition LanguageAIDL  - Android Interface Definition Language
AIDL - Android Interface Definition Language
Arvind Devaraj
 
NDK Programming in Android
NDK Programming in AndroidNDK Programming in Android
NDK Programming in Android
Arvind Devaraj
 
Google Cloud Messaging
Google Cloud MessagingGoogle Cloud Messaging
Google Cloud Messaging
Arvind Devaraj
 
OpenGLES - Graphics Programming in Android
OpenGLES - Graphics Programming in Android OpenGLES - Graphics Programming in Android
OpenGLES - Graphics Programming in Android
Arvind Devaraj
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)Arvind Devaraj
 
Graphics programming in open gl
Graphics programming in open glGraphics programming in open gl
Graphics programming in open gl
Arvind Devaraj
 
Signal Processing Introduction using Fourier Transforms
Signal Processing Introduction using Fourier TransformsSignal Processing Introduction using Fourier Transforms
Signal Processing Introduction using Fourier TransformsArvind Devaraj
 

More from Arvind Devaraj (20)

Deep learning for NLP and Transformer
 Deep learning for NLP  and Transformer Deep learning for NLP  and Transformer
Deep learning for NLP and Transformer
 
NLP using transformers
NLP using transformers NLP using transformers
NLP using transformers
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
 
Career hunt pitch
Career hunt pitchCareer hunt pitch
Career hunt pitch
 
Career options for CS and IT students
Career options for CS and IT studentsCareer options for CS and IT students
Career options for CS and IT students
 
Careerhunt ebook
Careerhunt ebookCareerhunt ebook
Careerhunt ebook
 
Static Analysis of Computer programs
Static Analysis of Computer programs Static Analysis of Computer programs
Static Analysis of Computer programs
 
Hyperbook
HyperbookHyperbook
Hyperbook
 
Yourstory Android Workshop
Yourstory Android WorkshopYourstory Android Workshop
Yourstory Android Workshop
 
Android High performance in GPU using opengles and renderscript
Android High performance in GPU using opengles and renderscriptAndroid High performance in GPU using opengles and renderscript
Android High performance in GPU using opengles and renderscript
 
OpenGLES Android Graphics
OpenGLES Android GraphicsOpenGLES Android Graphics
OpenGLES Android Graphics
 
Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
 
AIDL - Android Interface Definition Language
AIDL  - Android Interface Definition LanguageAIDL  - Android Interface Definition Language
AIDL - Android Interface Definition Language
 
NDK Programming in Android
NDK Programming in AndroidNDK Programming in Android
NDK Programming in Android
 
Google Cloud Messaging
Google Cloud MessagingGoogle Cloud Messaging
Google Cloud Messaging
 
OpenGLES - Graphics Programming in Android
OpenGLES - Graphics Programming in Android OpenGLES - Graphics Programming in Android
OpenGLES - Graphics Programming in Android
 
Operating system
Operating systemOperating system
Operating system
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
 
Graphics programming in open gl
Graphics programming in open glGraphics programming in open gl
Graphics programming in open gl
 
Signal Processing Introduction using Fourier Transforms
Signal Processing Introduction using Fourier TransformsSignal Processing Introduction using Fourier Transforms
Signal Processing Introduction using Fourier Transforms
 

Recently uploaded

SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
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
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
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
 
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
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
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
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
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
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
ViralQR
 
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
 
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
 

Recently uploaded (20)

SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
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
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
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
 
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
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
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...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
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...
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
 
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...
 
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
 

Sorting (introduction)

  • 2. Sorting • Given an array, put the elements in order – Numerical or lexicographic • Desirable characteristics – Fast – In place (don’t need a second array) – Stability
  • 3. Insertion Sort • Simple, able to handle any data • Grow a sorted array from the beginning – Create an empty array of the proper size – Pick the elements one at a time in any order – Put them in the new array in sorted order • If the element is not last, make room for it – Repeat until done • Can be done in place if well designed
  • 4. Insertion Sort 90 11 27 37111631 4
  • 5. Insertion Sort 90 11 27 37111631 4 90
  • 6. Insertion Sort 90 11 27 37111631 4 9011
  • 7. Insertion Sort 90 11 27 90 37111631 4 2711
  • 8. Insertion Sort 90 11 27 31 90 37111631 4 2711
  • 9. Insertion Sort 90 11 27 27 31 90 37111631 4 114
  • 10. Insertion Sort 90 11 27 16 27 31 90 37111631 4 114
  • 11. Insertion Sort 90 11 27 11 16 27 31 90 37111631 4 114
  • 12. Insertion Sort 90 11 27 11 16 27 31 37 90 37111631 4 114
  • 13. Merge Sort • Fast, able to handle any data – But can’t be done in place • View the array as a set of small sorted arrays – Initially only the 1-element “arrays” are sorted • Merge pairs of sorted arrays – Repeatedly choose the smallest element in each – This produces sorted arrays that are twice as long • Repeat until only one array remains
  • 14. Merge Sort 90 11 27 37111631 4 9011
  • 15. Merge Sort 90 11 27 27 31 37111631 4 9011
  • 16. Merge Sort 90 11 27 27 31 4 16 37111631 4 9011
  • 17. Merge Sort 90 11 27 27 31 4 16 11 37 37111631 4 9011
  • 18. Merge Sort 11 27 31 27 31 4 16 11 37 90 9011
  • 19. Merge Sort 11 27 31 27 31 4 16 11 37 37161190 4 9011
  • 20. Merge Sort 11 27 31 11 16 27 31 37 90 37161190 4 114
  • 21. Divide and Conquer • Split a problem into simpler subproblems – Keep doing that until trivial subproblems result • Solve the trivial subproblems • Combine the results to solve a larger problem – Keep doing that until the full problem is solved • Merge sort illustrates divide and conquer – But it is a general strategy that is often helpful
  • 22. Quick Sort • For example, given 80 38 95 84 99 10 79 44 26 87 96 12 43 81 3 we can select the middle entry, 44, and sort the remaining entries into two groups, those less than 44 and those greater than 44: 38 10 26 12 43 3 44 80 95 84 99 79 87 96 81 • If we sort each sub-list, we will have sorted the entire array
  • 23. A sample heap • Each node is larger than its children 19 1418 22 321 14 119 15 25 1722
  • 24. Sorting using Heaps • What do heaps have to do with sorting an array? Because the binary tree is balanced and left justified, it can be represented as an array – All our operations on binary trees can be represented as operations on arrays – To sort: heapify the array; while the array isn’t empty { remove and replace the root; reheap the new root node; }
  • 25. Summary of Sorting Algorithms • in-place, randomized • fastest (good for large inputs) O(n log n) expected quick-sort • sequential data access • fast (good for huge inputs) O(n log n)merge-sort • in-place • fast (good for large inputs) O(n log n)heap-sort O(n2) O(n2) Time insertion-sort selection-sort Algorithm Notes • in-place • slow (good for small inputs) • in-place • slow (good for small inputs)