SlideShare a Scribd company logo
Session 4Ver. 1.0
Selection Sort
Selection sort algorithm:
Has a quadratic order of growth and is therefore suitable for
sorting small lists only
Scans through the list iteratively, selects one item in each
scan, and moves the item to its correct position in the list
Sorting Data by Using Selection Sort
Session 4Ver. 1.0
Selection Sort
To understand the implementation of selection sort
algorithm, consider an unsorted list of numbers stored in an
array.
Implementing Selection Sort Algorithm
arr
210 43
120 10 200105 20
Session 4Ver. 1.0
Selection Sort
Let us sort this unsorted list.
Implementing Selection Sort Algorithm (Contd.)
arr
210 43
120 10 200105 20
Session 4Ver. 1.0
Selection Sort
Pass 1
n = 5
Search the minimum value in the array, arr[0] to arr[n – 1].
Implementing Selection Sort Algorithm (Contd.)
arr
210 43
120 10 200105 20
min
Session 4Ver. 1.0
Selection Sort
Pass 1
n = 5
Search the minimum value in the array, arr[0] to arr[n – 1].
Swap the minimum value with the value at index 0.
Implementing Selection Sort Algorithm (Contd.)
arr
210 43
120 10 200105 20
min
Swap
10510
Session 4Ver. 1.0
Selection Sort
Pass 1
n = 5
Search the minimum value in the array, arr[0] to arr[n – 1].
Swap the minimum value with the value at index 0.
Implementing Selection Sort Algorithm (Contd.)
arr
210 43
120 105 20010 20
The smallest value is placed at its correct location after Pass 1
Session 4Ver. 1.0
Selection Sort
Pass 2
n = 5
Search the minimum value in the array, arr[1] to arr[n – 1].
Swap the minimum value with the value at index 1.
Implementing Selection Sort Algorithm (Contd.)
arr
210 43
120 105 20010 20
min
20 120
Swap
Session 4Ver. 1.0
Selection Sort
Pass 2
n = 5
Search the minimum value in the array, arr[1] to arr[n – 1].
Swap the minimum value with the value at index 1.
Implementing Selection Sort Algorithm (Contd.)
arr
210 43
120 200 2010510arr
210 43
20 200 120
The second smallest value is placed at its correct location after
Pass 2
10510
Session 4Ver. 1.0
Selection Sort
Pass 3
n = 5
Search the minimum value in the array, arr[2] to arr[n – 1].
Swap the minimum value with the value at index 2.
Implementing Selection Sort Algorithm (Contd.)
arr
210 43
120 200 2010510arr
210 43
20 200
min
10 120105
Session 4Ver. 1.0
Selection Sort
Pass 3
n = 5
Search the minimum value in the array, arr[2] to arr[n – 1].
Swap the minimum value with the value at index 2.
Implementing Selection Sort Algorithm (Contd.)
arr
210 43
120 200 2010510arr
210 43
20 200
minThe third smallest value is placed at its correct location after Pass 3
12010510
Session 4Ver. 1.0
Selection Sort
Pass 4
n = 5
Search the minimum value in the array, arr[3] to arr[n – 1].
Swap the minimum value with the value at index 3.
Implementing Selection Sort Algorithm (Contd.)
arr
210 43
120 200 2010510arr
210 43
20 200 12010 105
min
120 200
Swap
Session 4Ver. 1.0
Selection Sort
Pass 4
n = 5
Search the minimum value in the array, arr[3] to arr[n – 1].
Swap the minimum value with the value at index 3.
Implementing Selection Sort Algorithm (Contd.)
arr
210 43
120 200 2010510arr
210 43
20
The fourth smallest value is placed at its correct location after Pass 4
120 20010 105
Session 4Ver. 1.0
Selection Sort
Pass 4
n = 5
The list is now sorted.
Implementing Selection Sort Algorithm (Contd.)
arr
210 43
120 200 2010510arr
210 43
20 20010 105 120
Session 4Ver. 1.0
Selection Sort
Write an algorithm to implement selection sort.
Algorithm for selection sort:
1. Repeat steps 2 and 3 varying j from 0 to n – 2
2. Find the minimum value in arr[j] to arr[n – 1]:
a. Set min_index = j
b. Repeat step c varying i from j + 1 to n – 1
c. If arr[i] < arr[min_index]:
i. min_index = i
3. Swap arr[j] with arr[min_index]
Implementing Selection Sort Algorithm (Contd.)
Session 4Ver. 1.0
Selection Sort
public void selectionSort()
{
int out,in,min;
for(out=0;out<nElems;out++)
{
min=out;
for(in=out+1;in<nElems-1;in++)
{
if(A[in]<a[min])
min=in;
}
swap(out,min);
}
}
Selection Sort Method
Session 4Ver. 1.0
Selection Sort
In selection sort, there are n – 1 comparisons during Pass 1
to find the smallest element, n – 2 comparisons during Pass
2 to find the second smallest element, and so on.
Total number of comparisons = (n – 1) + (n – 2) + (n – 3) +
… + 3 + 2 + 1 = n(n – 1)/2
n(n – 1)/2 is of O(n2
) order. Therefore, the selection sort
algorithm is of the order O(n2
).
Determining the Efficiency of Selection Sort Algorithm
Session 4Ver. 1.0
Selection Sort
Read the following statement and specify whether it is true
or false:
The efficiency of selection sort algorithm is same as that of the
bubble sort algorithm.
Just a minute
Answer:
True
Session 4Ver. 1.0
Selection Sort
How many comparisons are performed in the second pass
of the selection sort algorithm?
Just a minute
Answer:
n – 2

More Related Content

PPTX
Topological Sorting
PPTX
Quick sort
PPTX
Abstract Data Types
PPT
Unit 4 external sorting
PPTX
Bubble Sort Algorithm Presentation
PPTX
sorting and searching.pptx
PPTX
Ppt on Linked list,stack,queue
PPTX
Greedy algorithms
Topological Sorting
Quick sort
Abstract Data Types
Unit 4 external sorting
Bubble Sort Algorithm Presentation
sorting and searching.pptx
Ppt on Linked list,stack,queue
Greedy algorithms

What's hot (20)

PPTX
Analysis of Algorithm (Bubblesort and Quicksort)
PPTX
Analysis of algorithm
PPTX
Array operations
PPTX
Binary Heap Tree, Data Structure
PPTX
Shortest path algorithm
PPTX
Insertion sort algorithm power point presentation
PPTX
Stack and Queue
PDF
Binary Search - Design & Analysis of Algorithms
PPTX
sorting and its types
PPTX
Insertion sort
PPT
BINARY TREE REPRESENTATION.ppt
PPTX
Binary Search Tree in Data Structure
PPTX
PPTX
Ambiguous & Unambiguous Grammar
PPT
B trees in Data Structure
PPTX
Insertion sort
PPTX
SORTING techniques.pptx
PPT
PPTX
B and B+ tree
PDF
Sorting Algorithms
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of algorithm
Array operations
Binary Heap Tree, Data Structure
Shortest path algorithm
Insertion sort algorithm power point presentation
Stack and Queue
Binary Search - Design & Analysis of Algorithms
sorting and its types
Insertion sort
BINARY TREE REPRESENTATION.ppt
Binary Search Tree in Data Structure
Ambiguous & Unambiguous Grammar
B trees in Data Structure
Insertion sort
SORTING techniques.pptx
B and B+ tree
Sorting Algorithms
Ad

Similar to Selection sort (20)

PPTX
Selection sorting
PDF
Sorting algorithms
PPT
Selection sort
PPTX
Sorting Algorithms
PPTX
Different Searching and Sorting Methods.pptx
PPTX
Selection-sort-in-algorithm and complexity.pptx
DOCX
Selection sort lab mannual
PPTX
Selectionsort
PPTX
Sorting Algorithms to arrange data in particular format
PPT
Data Structure (MC501)
DOCX
Sorting
PPTX
Selection_Sort-CSI (For Sharing and General )
PPTX
Lecture 13 data structures and algorithms
PPTX
Sorting method data structure
PPTX
Data structure using c module 3
PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PPT
Sorting of linked list data through python.ppt
PPTX
Data structure.pptx
PPTX
Chapter 8 Sorting in the context of DSA.pptx
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
Selection sorting
Sorting algorithms
Selection sort
Sorting Algorithms
Different Searching and Sorting Methods.pptx
Selection-sort-in-algorithm and complexity.pptx
Selection sort lab mannual
Selectionsort
Sorting Algorithms to arrange data in particular format
Data Structure (MC501)
Sorting
Selection_Sort-CSI (For Sharing and General )
Lecture 13 data structures and algorithms
Sorting method data structure
Data structure using c module 3
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
Sorting of linked list data through python.ppt
Data structure.pptx
Chapter 8 Sorting in the context of DSA.pptx
358 33 powerpoint-slides_14-sorting_chapter-14
Ad

More from M Vishnuvardhan Reddy (20)

PPTX
Python Sets_Dictionary.pptx
PPTX
Lists_tuples.pptx
PPTX
Python Control Structures.pptx
PPTX
Python Strings.pptx
PPTX
Python Basics.pptx
PPTX
Python Operators.pptx
PPTX
Python Datatypes.pptx
PPTX
DataScience.pptx
PPT
PPT
Cascading Style Sheets
PPT
Java Threads
PPT
Java Streams
PPT
Scanner class
PPT
Polymorphism
PPT
PPT
Java applets
PPT
Exception handling
PPT
Control structures
PPT
Constructors
PPT
Classes&amp;objects
Python Sets_Dictionary.pptx
Lists_tuples.pptx
Python Control Structures.pptx
Python Strings.pptx
Python Basics.pptx
Python Operators.pptx
Python Datatypes.pptx
DataScience.pptx
Cascading Style Sheets
Java Threads
Java Streams
Scanner class
Polymorphism
Java applets
Exception handling
Control structures
Constructors
Classes&amp;objects

Recently uploaded (20)

PPTX
Introduction to Artificial Intelligence
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
top salesforce developer skills in 2025.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Digital Strategies for Manufacturing Companies
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Transform Your Business with a Software ERP System
Introduction to Artificial Intelligence
Reimagine Home Health with the Power of Agentic AI​
Operating system designcfffgfgggggggvggggggggg
Odoo POS Development Services by CandidRoot Solutions
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
top salesforce developer skills in 2025.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Navsoft: AI-Powered Business Solutions & Custom Software Development
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
How to Migrate SBCGlobal Email to Yahoo Easily
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
CHAPTER 2 - PM Management and IT Context
Digital Strategies for Manufacturing Companies
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Odoo Companies in India – Driving Business Transformation.pdf
PTS Company Brochure 2025 (1).pdf.......
Transform Your Business with a Software ERP System

Selection sort

  • 1. Session 4Ver. 1.0 Selection Sort Selection sort algorithm: Has a quadratic order of growth and is therefore suitable for sorting small lists only Scans through the list iteratively, selects one item in each scan, and moves the item to its correct position in the list Sorting Data by Using Selection Sort
  • 2. Session 4Ver. 1.0 Selection Sort To understand the implementation of selection sort algorithm, consider an unsorted list of numbers stored in an array. Implementing Selection Sort Algorithm arr 210 43 120 10 200105 20
  • 3. Session 4Ver. 1.0 Selection Sort Let us sort this unsorted list. Implementing Selection Sort Algorithm (Contd.) arr 210 43 120 10 200105 20
  • 4. Session 4Ver. 1.0 Selection Sort Pass 1 n = 5 Search the minimum value in the array, arr[0] to arr[n – 1]. Implementing Selection Sort Algorithm (Contd.) arr 210 43 120 10 200105 20 min
  • 5. Session 4Ver. 1.0 Selection Sort Pass 1 n = 5 Search the minimum value in the array, arr[0] to arr[n – 1]. Swap the minimum value with the value at index 0. Implementing Selection Sort Algorithm (Contd.) arr 210 43 120 10 200105 20 min Swap 10510
  • 6. Session 4Ver. 1.0 Selection Sort Pass 1 n = 5 Search the minimum value in the array, arr[0] to arr[n – 1]. Swap the minimum value with the value at index 0. Implementing Selection Sort Algorithm (Contd.) arr 210 43 120 105 20010 20 The smallest value is placed at its correct location after Pass 1
  • 7. Session 4Ver. 1.0 Selection Sort Pass 2 n = 5 Search the minimum value in the array, arr[1] to arr[n – 1]. Swap the minimum value with the value at index 1. Implementing Selection Sort Algorithm (Contd.) arr 210 43 120 105 20010 20 min 20 120 Swap
  • 8. Session 4Ver. 1.0 Selection Sort Pass 2 n = 5 Search the minimum value in the array, arr[1] to arr[n – 1]. Swap the minimum value with the value at index 1. Implementing Selection Sort Algorithm (Contd.) arr 210 43 120 200 2010510arr 210 43 20 200 120 The second smallest value is placed at its correct location after Pass 2 10510
  • 9. Session 4Ver. 1.0 Selection Sort Pass 3 n = 5 Search the minimum value in the array, arr[2] to arr[n – 1]. Swap the minimum value with the value at index 2. Implementing Selection Sort Algorithm (Contd.) arr 210 43 120 200 2010510arr 210 43 20 200 min 10 120105
  • 10. Session 4Ver. 1.0 Selection Sort Pass 3 n = 5 Search the minimum value in the array, arr[2] to arr[n – 1]. Swap the minimum value with the value at index 2. Implementing Selection Sort Algorithm (Contd.) arr 210 43 120 200 2010510arr 210 43 20 200 minThe third smallest value is placed at its correct location after Pass 3 12010510
  • 11. Session 4Ver. 1.0 Selection Sort Pass 4 n = 5 Search the minimum value in the array, arr[3] to arr[n – 1]. Swap the minimum value with the value at index 3. Implementing Selection Sort Algorithm (Contd.) arr 210 43 120 200 2010510arr 210 43 20 200 12010 105 min 120 200 Swap
  • 12. Session 4Ver. 1.0 Selection Sort Pass 4 n = 5 Search the minimum value in the array, arr[3] to arr[n – 1]. Swap the minimum value with the value at index 3. Implementing Selection Sort Algorithm (Contd.) arr 210 43 120 200 2010510arr 210 43 20 The fourth smallest value is placed at its correct location after Pass 4 120 20010 105
  • 13. Session 4Ver. 1.0 Selection Sort Pass 4 n = 5 The list is now sorted. Implementing Selection Sort Algorithm (Contd.) arr 210 43 120 200 2010510arr 210 43 20 20010 105 120
  • 14. Session 4Ver. 1.0 Selection Sort Write an algorithm to implement selection sort. Algorithm for selection sort: 1. Repeat steps 2 and 3 varying j from 0 to n – 2 2. Find the minimum value in arr[j] to arr[n – 1]: a. Set min_index = j b. Repeat step c varying i from j + 1 to n – 1 c. If arr[i] < arr[min_index]: i. min_index = i 3. Swap arr[j] with arr[min_index] Implementing Selection Sort Algorithm (Contd.)
  • 15. Session 4Ver. 1.0 Selection Sort public void selectionSort() { int out,in,min; for(out=0;out<nElems;out++) { min=out; for(in=out+1;in<nElems-1;in++) { if(A[in]<a[min]) min=in; } swap(out,min); } } Selection Sort Method
  • 16. Session 4Ver. 1.0 Selection Sort In selection sort, there are n – 1 comparisons during Pass 1 to find the smallest element, n – 2 comparisons during Pass 2 to find the second smallest element, and so on. Total number of comparisons = (n – 1) + (n – 2) + (n – 3) + … + 3 + 2 + 1 = n(n – 1)/2 n(n – 1)/2 is of O(n2 ) order. Therefore, the selection sort algorithm is of the order O(n2 ). Determining the Efficiency of Selection Sort Algorithm
  • 17. Session 4Ver. 1.0 Selection Sort Read the following statement and specify whether it is true or false: The efficiency of selection sort algorithm is same as that of the bubble sort algorithm. Just a minute Answer: True
  • 18. Session 4Ver. 1.0 Selection Sort How many comparisons are performed in the second pass of the selection sort algorithm? Just a minute Answer: n – 2

Editor's Notes

  • #2: Ask students to answer this question and then come to the given example.
  • #3: Ask students to answer this question and then come to the given example.
  • #4: Ask students to answer this question and then come to the given example.
  • #5: Ask students to answer this question and then come to the given example.
  • #6: Ask students to answer this question and then come to the given example.
  • #7: Ask students to answer this question and then come to the given example.
  • #8: Ask students to answer this question and then come to the given example.
  • #9: Ask students to answer this question and then come to the given example.
  • #10: Ask students to answer this question and then come to the given example.
  • #11: Ask students to answer this question and then come to the given example.
  • #12: Ask students to answer this question and then come to the given example.
  • #13: Ask students to answer this question and then come to the given example.
  • #14: Ask students to answer this question and then come to the given example.
  • #15: Ask students to write an algorithm to implement bubble sorting. Given them 5-10 minutes to write the algorithm. Then show them the answer.
  • #16: Ask students to write an algorithm to implement bubble sorting. Given them 5-10 minutes to write the algorithm. Then show them the answer.
  • #17: In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  • #18: In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  • #19: In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.