SlideShare a Scribd company logo
SELECTION SORT
SELECTION SORT
 Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-
based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted
part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.
 The smallest element is selected from the unsorted array and swapped with the leftmost element, and
that element becomes a part of the sorted array. This process continues moving
unsorted array boundary by one element to the right.
 This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n2),
where n is the number of items.
HOW SELECTION SORT WORKS?
 Consider the following depicted array as an example.
 For the first position in the sorted list, the whole list is scanned sequentially. The first position where 14
is stored presently, we search the whole list and find that 10 is the lowest value.
 So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the list,
appears in the first position of the sorted list.
HOW SELECTION SORT WORKS?
 For the second position, where 33 is residing, we start scanning the rest of the list in a linear manner.
 We find that 14 is the second lowest value in the list and it should appear at the second place. We swap
these values.
 After two iterations, two least values are positioned at the beginning in a sorted manner.
HOW SELECTION SORT WORKS?
HOW SELECTION SORT WORKS?
Selection Sort C++
HOW SELECTION SORT WORKS?
Selection Sort C++
HOW SELECTION SORT WORKS?
Selection Sort C++ Result
INSERTION SORT
INSERTION SORT
 This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always
sorted. For example, the lower part of an array is maintained to be sorted. An element which is to be
'insert'ed in this sorted sub-list, has to find its appropriate place and then it has to be inserted there.
Hence the name, insertion sort.
 The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list
(in the same array). This algorithm is not suitable for large data sets as its average and worst case
complexity are of Ο(n2), where n is the number of items.
HOW INSERTION SORT WORKS?
 We take an unsorted array for our example.
 Insertion sort compares the first two elements.
 It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.
HOW INSERTION SORT WORKS?
 Insertion sort moves ahead and compares 33 with 27.
 And finds that 33 is not in the correct position.
 It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that the sorted
sub-list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list remains sorted
after swapping.
HOW INSERTION SORT WORKS?
 By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10.
 These values are not in a sorted order.
 So we swap them.
HOW INSERTION SORT WORKS?
 However, swapping makes 27 and 10 unsorted.
 Hence, we swap them too.
 Again we find 14 and 10 in an unsorted order.
 We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.
 This process goes on until all the unsorted values are covered in a sorted sub-list.
HOW INSERTION SORT WORKS?
Insertion Sort C++
HOW INSERTION SORT WORKS?
Insertion Sort C++
HOW INSERTION SORT WORKS?
Insertion Sort C++

More Related Content

What's hot

Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptx
Dr.Shweta
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
chauhankapil
 
Selection sort
Selection sortSelection sort
Selection sortJay Patel
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
Kamran Zafar
 
Splay tree
Splay treeSplay tree
Splay tree
hina firdaus
 
Merge sort
Merge sortMerge sort
Merge sort
Md. Rakib Trofder
 
Applications of queues ii
Applications of queues   iiApplications of queues   ii
Applications of queues iiTech_MX
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
District Administration
 
Ppt bubble sort
Ppt bubble sortPpt bubble sort
Ppt bubble sort
prabhakar jalasutram
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
Dr. Jasmine Beulah Gnanadurai
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P
 
Quick sort
Quick sortQuick sort
Quick sort
Jehat Hassan
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
Md. Israil Fakir
 
Array data structure
Array data structureArray data structure
Array data structure
maamir farooq
 
Sorting method data structure
Sorting method data structureSorting method data structure
Sorting method data structure
sunilchute1
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListPTCL
 
Linked list
Linked listLinked list
Linked list
FURQAN M LODHI
 

What's hot (20)

Insertion sort
Insertion sortInsertion sort
Insertion sort
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptx
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
Selection sort
Selection sortSelection sort
Selection sort
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Splay tree
Splay treeSplay tree
Splay tree
 
Merge sort
Merge sortMerge sort
Merge sort
 
Applications of queues ii
Applications of queues   iiApplications of queues   ii
Applications of queues ii
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
Ppt bubble sort
Ppt bubble sortPpt bubble sort
Ppt bubble sort
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Quick sort
Quick sortQuick sort
Quick sort
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Array data structure
Array data structureArray data structure
Array data structure
 
Sorting method data structure
Sorting method data structureSorting method data structure
Sorting method data structure
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Linked list
Linked listLinked list
Linked list
 

Similar to Selection and insertion sort

Selection and insertion sort
Selection and insertion sortSelection and insertion sort
Selection and insertion sort
Ann Tugade
 
Selection Sort and Insertion Sort
Selection Sort and Insertion SortSelection Sort and Insertion Sort
Selection Sort and Insertion Sort
tamayaoraquel
 
Selection-sort-in-algorithm and complexity.pptx
Selection-sort-in-algorithm and complexity.pptxSelection-sort-in-algorithm and complexity.pptx
Selection-sort-in-algorithm and complexity.pptx
ArjayBalberan1
 
Sorting
SortingSorting
Sorting
Saharamily
 
Sorting types and Algorithms
Sorting types and AlgorithmsSorting types and Algorithms
Sorting types and Algorithms
Ali Khan
 
Sorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptxSorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptx
subhanalichand514
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
Rahul Jamwal
 
Sorting
SortingSorting
Sorting and Its Types
Sorting and Its TypesSorting and Its Types
Sorting and Its Types
amberkhan59
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
smruti sarangi
 
Bubble sorting lab manual
Bubble sorting lab manualBubble sorting lab manual
Bubble sorting lab manual
maamir farooq
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
ParagAhir1
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Sorting Algorithms.pptx
Sorting Algorithms.pptxSorting Algorithms.pptx
Sorting Algorithms.pptx
MuhammadAli2977
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
CHANDAN KUMAR
 

Similar to Selection and insertion sort (20)

Selection and insertion sort
Selection and insertion sortSelection and insertion sort
Selection and insertion sort
 
Selection Sort and Insertion Sort
Selection Sort and Insertion SortSelection Sort and Insertion Sort
Selection Sort and Insertion Sort
 
Unit v data structure-converted
Unit  v data structure-convertedUnit  v data structure-converted
Unit v data structure-converted
 
Selection-sort-in-algorithm and complexity.pptx
Selection-sort-in-algorithm and complexity.pptxSelection-sort-in-algorithm and complexity.pptx
Selection-sort-in-algorithm and complexity.pptx
 
Sorting
SortingSorting
Sorting
 
Data Structures and Algorithms
Data Structures and AlgorithmsData Structures and Algorithms
Data Structures and Algorithms
 
Sorting types and Algorithms
Sorting types and AlgorithmsSorting types and Algorithms
Sorting types and Algorithms
 
Sorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptxSorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptx
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Sorting
SortingSorting
Sorting
 
Sorting and Its Types
Sorting and Its TypesSorting and Its Types
Sorting and Its Types
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
Bubble sorting lab manual
Bubble sorting lab manualBubble sorting lab manual
Bubble sorting lab manual
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Sorting Algorithms.pptx
Sorting Algorithms.pptxSorting Algorithms.pptx
Sorting Algorithms.pptx
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 

Recently uploaded

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 

Recently uploaded (20)

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 

Selection and insertion sort

  • 2. SELECTION SORT  Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison- based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.  The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary by one element to the right.  This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n2), where n is the number of items.
  • 3. HOW SELECTION SORT WORKS?  Consider the following depicted array as an example.  For the first position in the sorted list, the whole list is scanned sequentially. The first position where 14 is stored presently, we search the whole list and find that 10 is the lowest value.  So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the list, appears in the first position of the sorted list.
  • 4. HOW SELECTION SORT WORKS?  For the second position, where 33 is residing, we start scanning the rest of the list in a linear manner.  We find that 14 is the second lowest value in the list and it should appear at the second place. We swap these values.  After two iterations, two least values are positioned at the beginning in a sorted manner.
  • 6. HOW SELECTION SORT WORKS? Selection Sort C++
  • 7. HOW SELECTION SORT WORKS? Selection Sort C++
  • 8. HOW SELECTION SORT WORKS? Selection Sort C++ Result
  • 10. INSERTION SORT  This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. An element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and then it has to be inserted there. Hence the name, insertion sort.  The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array). This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2), where n is the number of items.
  • 11. HOW INSERTION SORT WORKS?  We take an unsorted array for our example.  Insertion sort compares the first two elements.  It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.
  • 12. HOW INSERTION SORT WORKS?  Insertion sort moves ahead and compares 33 with 27.  And finds that 33 is not in the correct position.  It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that the sorted sub-list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list remains sorted after swapping.
  • 13. HOW INSERTION SORT WORKS?  By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10.  These values are not in a sorted order.  So we swap them.
  • 14. HOW INSERTION SORT WORKS?  However, swapping makes 27 and 10 unsorted.  Hence, we swap them too.  Again we find 14 and 10 in an unsorted order.  We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.  This process goes on until all the unsorted values are covered in a sorted sub-list.
  • 15. HOW INSERTION SORT WORKS? Insertion Sort C++
  • 16. HOW INSERTION SORT WORKS? Insertion Sort C++
  • 17. HOW INSERTION SORT WORKS? Insertion Sort C++