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?
EXAMPLES
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.
EXAMPLES
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

sorting and its types
sorting and its typessorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
Selection Sort - Vipin Ramola
Selection Sort - Vipin RamolaSelection Sort - Vipin Ramola
Selection Sort - Vipin Ramola
Dipayan Sarkar
 
Selection sort
Selection sortSelection sort
Selection sort
amna izzat
 
Sorting and Its Types
Sorting and Its TypesSorting and Its Types
Sorting and Its Types
amberkhan59
 
Selection sort and insertion sort
Selection sort and insertion sortSelection sort and insertion sort
Selection sort and insertion sort
May Ann Mendoza
 
Selection sort
Selection sortSelection sort
Selection sortasra khan
 
Calc03 8 (1)
Calc03 8 (1)Calc03 8 (1)
Calc03 8 (1)
AwesomeFace01
 
Sorting types and Algorithms
Sorting types and AlgorithmsSorting types and Algorithms
Sorting types and Algorithms
Ali Khan
 
relational algebra (joins)
relational algebra (joins)relational algebra (joins)
relational algebra (joins)
Nilt1234
 
relational algebra-(basics)
 relational algebra-(basics) relational algebra-(basics)
relational algebra-(basics)
Nilt1234
 
Chapter3 basic java data types and declarations
Chapter3 basic java data types and declarationsChapter3 basic java data types and declarations
Chapter3 basic java data types and declarationssshhzap
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
CHANDAN KUMAR
 
Sorting
SortingSorting
Sorting
Saharamily
 
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
 
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMARDOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
PrasunKumar38
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 

What's hot (18)

sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Selection Sort - Vipin Ramola
Selection Sort - Vipin RamolaSelection Sort - Vipin Ramola
Selection Sort - Vipin Ramola
 
Selection sort
Selection sortSelection sort
Selection sort
 
Sorting and Its Types
Sorting and Its TypesSorting and Its Types
Sorting and Its Types
 
Selection sort and insertion sort
Selection sort and insertion sortSelection sort and insertion sort
Selection sort and insertion sort
 
Data Structures and Algorithms
Data Structures and AlgorithmsData Structures and Algorithms
Data Structures and Algorithms
 
Selection sort
Selection sortSelection sort
Selection sort
 
Calc03 8 (1)
Calc03 8 (1)Calc03 8 (1)
Calc03 8 (1)
 
Sorting types and Algorithms
Sorting types and AlgorithmsSorting types and Algorithms
Sorting types and Algorithms
 
relational algebra (joins)
relational algebra (joins)relational algebra (joins)
relational algebra (joins)
 
Unit v data structure-converted
Unit  v data structure-convertedUnit  v data structure-converted
Unit v data structure-converted
 
relational algebra-(basics)
 relational algebra-(basics) relational algebra-(basics)
relational algebra-(basics)
 
Chapter3 basic java data types and declarations
Chapter3 basic java data types and declarationsChapter3 basic java data types and declarations
Chapter3 basic java data types and declarations
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting
SortingSorting
Sorting
 
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
 
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMARDOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
 

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 Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptxSorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptx
subhanalichand514
 
Searching and sorting
Searching  and sortingSearching  and sorting
Searching and sorting
PoojithaBollikonda
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
Rahul Jamwal
 
Sorting
SortingSorting
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 
Bubble sorting lab manual
Bubble sorting lab manualBubble sorting lab manual
Bubble sorting lab manual
maamir farooq
 
What is a Sorting Algorithm Ishank mini project.pptx
What is a Sorting Algorithm Ishank mini project.pptxWhat is a Sorting Algorithm Ishank mini project.pptx
What is a Sorting Algorithm Ishank mini project.pptx
SneakyGoblin1
 
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
 
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
 
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.pptx
Sorting Algorithms.pptxSorting Algorithms.pptx
Sorting Algorithms.pptx
MuhammadAli2977
 
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleDS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
Vivek487417
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 

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
 
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 Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptxSorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptx
 
Searching and sorting
Searching  and sortingSearching  and sorting
Searching and sorting
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Sorting
SortingSorting
Sorting
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Bubble sorting lab manual
Bubble sorting lab manualBubble sorting lab manual
Bubble sorting lab manual
 
What is a Sorting Algorithm Ishank mini project.pptx
What is a Sorting Algorithm Ishank mini project.pptxWhat is a Sorting Algorithm Ishank mini project.pptx
What is a Sorting Algorithm Ishank mini project.pptx
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
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
 
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.pptx
Sorting Algorithms.pptxSorting Algorithms.pptx
Sorting Algorithms.pptx
 
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleDS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 

Recently uploaded

Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
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
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
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
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
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
 
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
 
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
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
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
 
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
 
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)
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 

Recently uploaded (20)

Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
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
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
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
 
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...
 
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.
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
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
 
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
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 

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.
  • 7.
  • 8.
  • 9. HOW SELECTION SORT WORKS? Selection Sort C++
  • 10. HOW SELECTION SORT WORKS? Selection Sort C++
  • 11. HOW SELECTION SORT WORKS? Selection Sort C++ Result
  • 12.
  • 13.
  • 14.
  • 16. 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.
  • 17. 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.
  • 18. 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.
  • 19. 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.
  • 20. 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.
  • 22.
  • 23.
  • 24. HOW INSERTION SORT WORKS? Insertion Sort C++
  • 25. HOW INSERTION SORT WORKS? Insertion Sort C++
  • 26. HOW INSERTION SORT WORKS? Insertion Sort C++