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
Selection and insertion sort
Selection and insertion sort
HOW SELECTION SORT WORKS?
Selection Sort C++
HOW SELECTION SORT WORKS?
Selection Sort C++
HOW SELECTION SORT WORKS?
Selection Sort C++ Result
Selection and insertion sort
Selection and insertion sort
Selection and insertion sort
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
Selection and insertion sort
Selection and insertion sort
HOW INSERTION SORT WORKS?
Insertion Sort C++
HOW INSERTION SORT WORKS?
Insertion Sort C++
HOW INSERTION SORT WORKS?
Insertion Sort C++

More Related Content

PPT
Selection sort
PPTX
Selection sort
PPTX
Sorting method data structure
PPTX
Selection sort 1
PPTX
Selection sorting
PPTX
The selection sort algorithm
PPTX
Selection sort algorithm presentation, selection sort example using power point
PPTX
Presentation on the topic selection sort
Selection sort
Selection sort
Sorting method data structure
Selection sort 1
Selection sorting
The selection sort algorithm
Selection sort algorithm presentation, selection sort example using power point
Presentation on the topic selection sort

What's hot (18)

PPTX
sorting and its types
PPT
Selection Sort - Vipin Ramola
PPT
Selection sort
PPTX
Sorting and Its Types
PPTX
Selection sort and insertion sort
PPTX
Data Structures and Algorithms
DOC
Selection sort
PPT
Calc03 8 (1)
PPTX
Sorting types and Algorithms
PPTX
relational algebra (joins)
PDF
Unit v data structure-converted
PPTX
relational algebra-(basics)
PPT
Chapter3 basic java data types and declarations
PPT
Sorting algorithms
PPTX
Sorting
PPTX
Insertion Sort, Quick Sort And Their complexity
PPTX
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
PPT
Searching in c language
sorting and its types
Selection Sort - Vipin Ramola
Selection sort
Sorting and Its Types
Selection sort and insertion sort
Data Structures and Algorithms
Selection sort
Calc03 8 (1)
Sorting types and Algorithms
relational algebra (joins)
Unit v data structure-converted
relational algebra-(basics)
Chapter3 basic java data types and declarations
Sorting algorithms
Sorting
Insertion Sort, Quick Sort And Their complexity
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
Searching in c language
Ad

Similar to Selection and insertion sort (20)

PPTX
Selection and insertion sort
PPTX
Selection Sort and Insertion Sort
PPTX
Selection-sort-in-algorithm and complexity.pptx
PPTX
Selection Sort & Insertion Sorts Algorithms
PPTX
Selection Sort & Insertion Sorts Algorithms
PPTX
Sorting Data structure And Algorithm.pptx
PPTX
Searching and sorting
PPTX
Searching & Sorting Algorithms
PPTX
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
PDF
PPT
Searching Sorting
DOCX
Bubble sorting lab manual
PPTX
sortting 2 sderftgyurvtynurftgyhujse.pptx
PPTX
What is a Sorting Algorithm Ishank mini project.pptx
PPTX
Data structure using c module 3
PPTX
sorting and searching.pptx
PDF
advanced searching and sorting.pdf
PPTX
Searching and Sorting algorithms and working
PPTX
DS - Unit 2 FINAL (2).pptx
PPTX
Data Structures_ Sorting & Searching
Selection and insertion sort
Selection Sort and Insertion Sort
Selection-sort-in-algorithm and complexity.pptx
Selection Sort & Insertion Sorts Algorithms
Selection Sort & Insertion Sorts Algorithms
Sorting Data structure And Algorithm.pptx
Searching and sorting
Searching & Sorting Algorithms
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
Searching Sorting
Bubble sorting lab manual
sortting 2 sderftgyurvtynurftgyhujse.pptx
What is a Sorting Algorithm Ishank mini project.pptx
Data structure using c module 3
sorting and searching.pptx
advanced searching and sorting.pdf
Searching and Sorting algorithms and working
DS - Unit 2 FINAL (2).pptx
Data Structures_ Sorting & Searching
Ad

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Institutional Correction lecture only . . .
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Cell Structure & Organelles in detailed.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
master seminar digital applications in india
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Pre independence Education in Inndia.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Complications of Minimal Access Surgery at WLH
Computing-Curriculum for Schools in Ghana
PPH.pptx obstetrics and gynecology in nursing
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Anesthesia in Laparoscopic Surgery in India
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Institutional Correction lecture only . . .
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Final Presentation General Medicine 03-08-2024.pptx
GDM (1) (1).pptx small presentation for students
Abdominal Access Techniques with Prof. Dr. R K Mishra
Cell Structure & Organelles in detailed.
Microbial diseases, their pathogenesis and prophylaxis
Module 4: Burden of Disease Tutorial Slides S2 2025
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
master seminar digital applications in india
Supply Chain Operations Speaking Notes -ICLT Program
Pre independence Education in Inndia.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Complications of Minimal Access Surgery at WLH

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.
  • 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
  • 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.
  • 24. HOW INSERTION SORT WORKS? Insertion Sort C++
  • 25. HOW INSERTION SORT WORKS? Insertion Sort C++
  • 26. HOW INSERTION SORT WORKS? Insertion Sort C++