DESIGN AND ANALYSIS
OF ALGORITHMS
Selection Sort
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 2, Module 3
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 1
Scan the entire stack and find the paper with
minimum marks
Move this paper to a new stack
Repeat with remaining papers
Each time, add next minimum mark paper on
top of new stack
Eventually, new stack is sorted in descending
order
Strategy 1 …
74 32 89 55 21 64
Strategy 1 …
21
74 32 89 55 21 64
Strategy 1 …
21 32
74 32 89 55 21 64
Strategy 1 …
21 32 55
74 32 89 55 21 64
Strategy 1 …
21 32 55 64
74 32 89 55 21 64
Strategy 1 …
21 32 55 64 74
74 32 89 55 21 64
Strategy 1 …
21 32 55 64 74 89
74 32 89 55 21 64
Strategy 1 …
Selection Sort
Select the next element in sorted order
Move it into its correct place in the final sorted list
Selection Sort
Avoid using a second list
Swap minimum element with value in first
position
Swap second minimum element to second
position
…
Selection Sort
74 32 89 55 21 64
Selection Sort
74 32 89 55 21 64
Selection Sort
21 32 89 55 74 64
Selection Sort
21 32 89 55 74 64
Selection Sort
21 32 89 55 74 64
Selection Sort
21 32 89 55 74 64
Selection Sort
21 32 55 89 74 64
Selection Sort
21 32 55 89 74 64
Selection Sort
21 32 55 64 74 89
Selection Sort
21 32 55 64 74 89
Selection Sort
21 32 55 64 74 89
Selection Sort
21 32 55 64 74 89
Selection Sort
SelectionSort(A,n) // Sort A of size n
for (startpos = 0; startpos < n; startpos++)
// Scan segments A[0]..A[n-1], A[1]..A[n-1], …
// Locate position of minimum element in current segment
minpos = startpos;
for (i = minpos+1; i < n; i++)
if (A[i] < A[minpos])
minpos = i;
// Move minimum element to start of current segment
swap(A,startpos,minpos)
Analysis of Selection Sort
Finding minimum in unsorted segment of length k
requires one scan, k steps
In each iteration, segment to be scanned reduces
by 1
t(n) = n + (n-1) + (n-2) + … + 1 = n(n+1)/2 = O(n2)
Recursive formulation
To sort A[i .. n-1]
Find minimum value in segment and move to A[i]
Apply Selection Sort to A[i+1..n-1]
Base case
Do nothing if i = n-1
Selection Sort, recursive
SelectionSort(A,start,n) // Sort A from start to n-1
if (start >= n-1)
return;
// Locate minimum element and move to start of segment
minpos = start;
for (i = start+1; i < n; i++)
if (A[i] < A[minpos])
minpos = i;
swap(A,start,minpos)
// Recursively sort the rest
SelectionSort(A,start+1,n)
Alternative calculation
t(n), time to run selection sort on length n
n steps to find minimum and move to position 0
t(n-1) time to run selection sort on A[1] to A[n-1]
Recurrence
t(n) = n + t(n-1)
t(1) = 1
t(n) = n + t(n-1) = n + ((n-1) + t(n-2)) = … =
n + (n-1) + (n-2) + … + 1 = n(n+1)/2 = O(n2)

More Related Content

PDF
insertion sort-new.pdf
PPTX
Data Structure and algorithms for software
PPTX
Searching and Sorting algorithms and working
PPTX
Different Searching and Sorting Methods.pptx
PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PPT
Data Structure (MC501)
PDF
Sorting
PDF
Sorting
insertion sort-new.pdf
Data Structure and algorithms for software
Searching and Sorting algorithms and working
Different Searching and Sorting Methods.pptx
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
Data Structure (MC501)
Sorting
Sorting

Similar to selection (20)

PPT
Unit 7 sorting
PPTX
Data structure using c module 3
PPTX
sorting and searching.pptx
PPTX
All Searching and Sorting Techniques in Data Structures
PPTX
ppt2- sorting data in design and anlysis.pptx
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PDF
Sorting algorithms bubble sort to merge sort.pdf
PPT
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
PPT
03_sorting and it's types with example .ppt
PPT
03_sorting123456789454545454545444543.ppt
PPTX
sorting-160810203705.pptx
DOCX
Selection sort lab mannual
PDF
L 14-ct1120
PPTX
sorting1.pptx
PDF
465659705-15-DSA-PPT-Sorting-Techniques-I.pdf
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PPTX
searching in data structure.pptx
PPTX
PPT
Searching Sorting-SELECTION ,BUBBBLE.ppt
Unit 7 sorting
Data structure using c module 3
sorting and searching.pptx
All Searching and Sorting Techniques in Data Structures
ppt2- sorting data in design and anlysis.pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
Sorting algorithms bubble sort to merge sort.pdf
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
03_sorting and it's types with example .ppt
03_sorting123456789454545454545444543.ppt
sorting-160810203705.pptx
Selection sort lab mannual
L 14-ct1120
sorting1.pptx
465659705-15-DSA-PPT-Sorting-Techniques-I.pdf
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
searching in data structure.pptx
Searching Sorting-SELECTION ,BUBBBLE.ppt

Recently uploaded (20)

PDF
Geothermal Energy: Unlocking the Earth’s Heat for Power (www.kiu.ac.ug)
PDF
CuO Nps photocatalysts 15156456551564161
PPT
ecg for noob ecg interpretation ecg recall
PDF
Micro 4 New.ppt.pdf thesis main microbio
PDF
Cosmology using numerical relativity - what hapenned before big bang?
PPTX
Preformulation.pptx Preformulation studies-Including all parameter
PPTX
The Female Reproductive System - Grade 10 ppt
PPTX
Cutaneous tuberculosis Dermatology
PPTX
Arterial Blood Pressure_Blood Flow_Hemodynamics.pptx
PDF
Glycolysis by Rishikanta Usham, Dhanamanjuri University
PPTX
Cells and Organs of the Immune System (Unit-2) - Majesh Sir.pptx
PDF
No dilute core produced in simulations of giant impacts on to Jupiter
PDF
Traditional Healing Practices: A Model for Integrative Care in Diabetes Mana...
PPTX
Neuro Ophthalmic diseases and their lesions
PPTX
Spectroscopy techniques in forensic science _ppt.pptx
PDF
Chapter 3 - Human Development Poweroint presentation
PDF
Sustainable Biology- Scopes, Principles of sustainiability, Sustainable Resou...
PDF
final prehhhejjehehhehehehebesentation.pdf
PPTX
CELL DIVISION Biology meiosis and mitosis
PDF
Sujay Rao Mandavilli IJISRT25AUG764 context based approaches to population ma...
Geothermal Energy: Unlocking the Earth’s Heat for Power (www.kiu.ac.ug)
CuO Nps photocatalysts 15156456551564161
ecg for noob ecg interpretation ecg recall
Micro 4 New.ppt.pdf thesis main microbio
Cosmology using numerical relativity - what hapenned before big bang?
Preformulation.pptx Preformulation studies-Including all parameter
The Female Reproductive System - Grade 10 ppt
Cutaneous tuberculosis Dermatology
Arterial Blood Pressure_Blood Flow_Hemodynamics.pptx
Glycolysis by Rishikanta Usham, Dhanamanjuri University
Cells and Organs of the Immune System (Unit-2) - Majesh Sir.pptx
No dilute core produced in simulations of giant impacts on to Jupiter
Traditional Healing Practices: A Model for Integrative Care in Diabetes Mana...
Neuro Ophthalmic diseases and their lesions
Spectroscopy techniques in forensic science _ppt.pptx
Chapter 3 - Human Development Poweroint presentation
Sustainable Biology- Scopes, Principles of sustainiability, Sustainable Resou...
final prehhhejjehehhehehehebesentation.pdf
CELL DIVISION Biology meiosis and mitosis
Sujay Rao Mandavilli IJISRT25AUG764 context based approaches to population ma...

selection