SlideShare a Scribd company logo
DESIGN AND ANALYSIS
OF ALGORITHMS
Insertion Sort
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 2, Module 4
Sorting
Searching for a value
Unsorted array — linear scan, O(n)
Sorted array — binary search, O(log n)
Other advantages of sorting
Finding median value: midpoint of sorted list
Checking for duplicates
Building a frequency table of values
How to sort?
You are a Teaching Assistant for a course
The instructor gives you a stack of exam answer
papers with marks, ordered randomly
Your task is to arrange them in descending order
Strategy 2
First paper: put in a new stack
Second paper:
Lower marks than first? Place below first paper
Higher marks than first? Place above first paper
Third paper
Insert into the correct position with respect to first
two papers
Do this for each subsequent paper:
insert into correct position in new sorted stack
Strategy 2 …
74 32 89 55 21 64
Strategy 2 …
74 32 89 55 21 64
74
Strategy 2 …
74 32 89 55 21 64
32 74
Strategy 2 …
74 32 89 55 21 64
32 74 89
Strategy 2 …
74 32 89 55 21 64
32 55 74 89
Strategy 2 …
74 32 89 55 21 64
21 32 55 74 89
Strategy 2 …
74 32 89 55 21 64
21 32 55 64 74 89
Strategy 2 …
Insertion Sort
Start building a sorted sequence with one element
Pick up next unsorted element and insert it into its
correct place in the already sorted sequence
Insertion Sort
InsertionSort(A,n) // Sort A of size n
for (pos = 1; pos < n; pos++)
// Build longer and longer sorted segments
// In each iteration A[0]..A[pos-1] is already sorted
// Move first element after sorted segment left
// till it is in the correct place
nextpos = pos
while (nextpos > 0 &&
A[nextpos] < A[nextpos-1])
swap(A,nextpos,nextpos-1)
nextpos = nextpos-1
Insertion Sort
74 32 89 55 21 64
Insertion Sort
74 32 89 55 21 64
Insertion Sort
32 74 89 55 21 64
Insertion Sort
32 74 89 55 21 64
Insertion Sort
32 74 55 89 21 64
Insertion Sort
32 55 74 89 21 64
Insertion Sort
32 55 74 21 89 64
Insertion Sort
32 55 21 74 89 64
Insertion Sort
32 21 55 74 89 64
Insertion Sort
21 32 55 74 89 64
Insertion Sort
21 32 55 74 64 89
Insertion Sort
21 32 55 64 74 89
Analysis of Insertion Sort
Inserting a new value in sorted segment of length
k requires upto k steps in the worst case
In each iteration, sorted segment in which to insert
increased by 1
t(n) = 1 + 2 + … + n-1 = n(n-1)/2 = O(n2)
Recursive formulation
To sort A[0..n-1]
Recursively sort A[0..n-2]
Insert A[n-1] into A[0..n-2]
Base case: n = 1
Insertion Sort, recursive
InsertionSort(A,k) // Sort A[0..k-1]
if (k == 1)
return;
InsertionSort(A,k-1);
Insert(A,k-1);
return;
Insert(A,j) // Insert A[j] into A[0..j-1]
pos = j;
while (pos > 0 && A[pos] < A[pos-1])
swap(A,pos,pos-1);
pos = pos-1;
Recurrence
t(n), time to run insertion sort on length n
Time t(n-1) to sort segment A[0] to A[n-2]
n-1 steps to insert A[n-1] in sorted segment
Recurrence
t(n) = n-1 + t(n-1)
t(1) = 1
t(n) = n-1 + t(n-1) = n-1 + ((n-2) + t(n-2)) = … =
(n-1) + (n-2) + … + 1 = n(n-1)/2 = O(n2)
O(n2) sorting algorithms
Selection sort and insertion sort are both O(n2)
So is bubble sort, which we will not discuss here
O(n2) sorting is infeasible for n over 10000
Among O(n2) sorts, insertion sort is usually better
than selection sort and both are better than
bubble sort
What happens when we apply insertion sort to
an already sorted list?

More Related Content

Similar to insertion sort-new.pdf

module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
Shiwani Gupta
 
presentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.pptpresentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
MergesortQuickSort.ppt
MergesortQuickSort.pptMergesortQuickSort.ppt
MergesortQuickSort.ppt
AliAhmad38278
 
220exercises2
220exercises2220exercises2
220exercises2
sadhanakumble
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
DrRanjeetKumar51721
 
Swaati algorithm of alignment ppt
Swaati algorithm of alignment pptSwaati algorithm of alignment ppt
Swaati algorithm of alignment ppt
Swati Kumari
 
Lecture 7_introduction_algorithms.pptx
Lecture 7_introduction_algorithms.pptxLecture 7_introduction_algorithms.pptx
Lecture 7_introduction_algorithms.pptx
ngBinh3
 
Module 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsModule 2 Design Analysis and Algorithms
Module 2 Design Analysis and Algorithms
Cool Guy
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbound
despicable me
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
Quartile
QuartileQuartile
Quartile
bilal samad
 
lecture 10
lecture 10lecture 10
lecture 10
sajinsc
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
ssuserd602fd
 
Analyzing and using test item data
Analyzing and using test item dataAnalyzing and using test item data
Analyzing and using test item data
Richelle Lontoc
 
Merge sort
Merge sortMerge sort
Sorting and searching
Sorting and searchingSorting and searching
Sorting and searching
kalyanineve
 
K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)
Abdullah al Mamun
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sort
Vasim Pathan
 
merge sort
merge sortmerge sort
merge sort
ObedurRahman1
 
K- Nearest Neighbor Approach
K- Nearest Neighbor ApproachK- Nearest Neighbor Approach
K- Nearest Neighbor Approach
Kumud Arora
 

Similar to insertion sort-new.pdf (20)

module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
presentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.pptpresentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.ppt
 
MergesortQuickSort.ppt
MergesortQuickSort.pptMergesortQuickSort.ppt
MergesortQuickSort.ppt
 
220exercises2
220exercises2220exercises2
220exercises2
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
 
Swaati algorithm of alignment ppt
Swaati algorithm of alignment pptSwaati algorithm of alignment ppt
Swaati algorithm of alignment ppt
 
Lecture 7_introduction_algorithms.pptx
Lecture 7_introduction_algorithms.pptxLecture 7_introduction_algorithms.pptx
Lecture 7_introduction_algorithms.pptx
 
Module 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsModule 2 Design Analysis and Algorithms
Module 2 Design Analysis and Algorithms
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbound
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Quartile
QuartileQuartile
Quartile
 
lecture 10
lecture 10lecture 10
lecture 10
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
Analyzing and using test item data
Analyzing and using test item dataAnalyzing and using test item data
Analyzing and using test item data
 
Merge sort
Merge sortMerge sort
Merge sort
 
Sorting and searching
Sorting and searchingSorting and searching
Sorting and searching
 
K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sort
 
merge sort
merge sortmerge sort
merge sort
 
K- Nearest Neighbor Approach
K- Nearest Neighbor ApproachK- Nearest Neighbor Approach
K- Nearest Neighbor Approach
 

Recently uploaded

J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
Maitrey Patel
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
The Third Creative Media
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Peter Caitens
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
Envertis Software Solutions
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
Yara Milbes
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
kalichargn70th171
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLESINTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
anfaltahir1010
 

Recently uploaded (20)

J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLESINTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
 

insertion sort-new.pdf

  • 1. DESIGN AND ANALYSIS OF ALGORITHMS Insertion Sort MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE http://www.cmi.ac.in/~madhavan NPTEL MOOC,JAN-FEB 2015 Week 2, Module 4
  • 2. Sorting Searching for a value Unsorted array — linear scan, O(n) Sorted array — binary search, O(log n) Other advantages of sorting Finding median value: midpoint of sorted list Checking for duplicates Building a frequency table of values
  • 3. How to sort? You are a Teaching Assistant for a course The instructor gives you a stack of exam answer papers with marks, ordered randomly Your task is to arrange them in descending order
  • 4. Strategy 2 First paper: put in a new stack Second paper: Lower marks than first? Place below first paper Higher marks than first? Place above first paper Third paper Insert into the correct position with respect to first two papers Do this for each subsequent paper: insert into correct position in new sorted stack
  • 5. Strategy 2 … 74 32 89 55 21 64
  • 6. Strategy 2 … 74 32 89 55 21 64 74
  • 7. Strategy 2 … 74 32 89 55 21 64 32 74
  • 8. Strategy 2 … 74 32 89 55 21 64 32 74 89
  • 9. Strategy 2 … 74 32 89 55 21 64 32 55 74 89
  • 10. Strategy 2 … 74 32 89 55 21 64 21 32 55 74 89
  • 11. Strategy 2 … 74 32 89 55 21 64 21 32 55 64 74 89
  • 12. Strategy 2 … Insertion Sort Start building a sorted sequence with one element Pick up next unsorted element and insert it into its correct place in the already sorted sequence
  • 13. Insertion Sort InsertionSort(A,n) // Sort A of size n for (pos = 1; pos < n; pos++) // Build longer and longer sorted segments // In each iteration A[0]..A[pos-1] is already sorted // Move first element after sorted segment left // till it is in the correct place nextpos = pos while (nextpos > 0 && A[nextpos] < A[nextpos-1]) swap(A,nextpos,nextpos-1) nextpos = nextpos-1
  • 14. Insertion Sort 74 32 89 55 21 64
  • 15. Insertion Sort 74 32 89 55 21 64
  • 16. Insertion Sort 32 74 89 55 21 64
  • 17. Insertion Sort 32 74 89 55 21 64
  • 18. Insertion Sort 32 74 55 89 21 64
  • 19. Insertion Sort 32 55 74 89 21 64
  • 20. Insertion Sort 32 55 74 21 89 64
  • 21. Insertion Sort 32 55 21 74 89 64
  • 22. Insertion Sort 32 21 55 74 89 64
  • 23. Insertion Sort 21 32 55 74 89 64
  • 24. Insertion Sort 21 32 55 74 64 89
  • 25. Insertion Sort 21 32 55 64 74 89
  • 26. Analysis of Insertion Sort Inserting a new value in sorted segment of length k requires upto k steps in the worst case In each iteration, sorted segment in which to insert increased by 1 t(n) = 1 + 2 + … + n-1 = n(n-1)/2 = O(n2)
  • 27. Recursive formulation To sort A[0..n-1] Recursively sort A[0..n-2] Insert A[n-1] into A[0..n-2] Base case: n = 1
  • 28. Insertion Sort, recursive InsertionSort(A,k) // Sort A[0..k-1] if (k == 1) return; InsertionSort(A,k-1); Insert(A,k-1); return; Insert(A,j) // Insert A[j] into A[0..j-1] pos = j; while (pos > 0 && A[pos] < A[pos-1]) swap(A,pos,pos-1); pos = pos-1;
  • 29. Recurrence t(n), time to run insertion sort on length n Time t(n-1) to sort segment A[0] to A[n-2] n-1 steps to insert A[n-1] in sorted segment Recurrence t(n) = n-1 + t(n-1) t(1) = 1 t(n) = n-1 + t(n-1) = n-1 + ((n-2) + t(n-2)) = … = (n-1) + (n-2) + … + 1 = n(n-1)/2 = O(n2)
  • 30. O(n2) sorting algorithms Selection sort and insertion sort are both O(n2) So is bubble sort, which we will not discuss here O(n2) sorting is infeasible for n over 10000 Among O(n2) sorts, insertion sort is usually better than selection sort and both are better than bubble sort What happens when we apply insertion sort to an already sorted list?