SlideShare a Scribd company logo
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
03_sorting123456789454545454545444543.ppt
PPT
03_sorting and it's types with example .ppt
PPT
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
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
searching in data structure.pptx
PPTX
PPT
Searching Sorting-SELECTION ,BUBBBLE.ppt
PPTX
2.Problem Solving Techniques and Data Structures.pptx
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
03_sorting123456789454545454545444543.ppt
03_sorting and it's types with example .ppt
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
sorting-160810203705.pptx
Selection sort lab mannual
L 14-ct1120
sorting1.pptx
465659705-15-DSA-PPT-Sorting-Techniques-I.pdf
searching in data structure.pptx
Searching Sorting-SELECTION ,BUBBBLE.ppt
2.Problem Solving Techniques and Data Structures.pptx

Recently uploaded (20)

PDF
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
PPT
LEC Synthetic Biology and its application.ppt
PDF
Warm, water-depleted rocky exoplanets with surfaceionic liquids: A proposed c...
PDF
Unit 5 Preparations, Reactions, Properties and Isomersim of Organic Compounds...
PDF
GROUP 2 ORIGINAL PPT. pdf Hhfiwhwifhww0ojuwoadwsfjofjwsofjw
PDF
lecture 2026 of Sjogren's syndrome l .pdf
PPTX
Microbes in human welfare class 12 .pptx
PPT
1. INTRODUCTION TO EPIDEMIOLOGY.pptx for community medicine
PPTX
gene cloning powerpoint for general biology 2
PDF
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
PPTX
A powerpoint on colorectal cancer with brief background
PPTX
Fluid dynamics vivavoce presentation of prakash
PPTX
Welcome-grrewfefweg-students-of-2024.pptx
PDF
Looking into the jet cone of the neutrino-associated very high-energy blazar ...
PPTX
PMR- PPT.pptx for students and doctors tt
PPTX
endocrine - management of adrenal incidentaloma.pptx
PPTX
Biomechanics of the Hip - Basic Science.pptx
PDF
The Land of Punt — A research by Dhani Irwanto
PPTX
ap-psych-ch-1-introduction-to-psychology-presentation.pptx
PPTX
Lesson-1-Introduction-to-the-Study-of-Chemistry.pptx
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
LEC Synthetic Biology and its application.ppt
Warm, water-depleted rocky exoplanets with surfaceionic liquids: A proposed c...
Unit 5 Preparations, Reactions, Properties and Isomersim of Organic Compounds...
GROUP 2 ORIGINAL PPT. pdf Hhfiwhwifhww0ojuwoadwsfjofjwsofjw
lecture 2026 of Sjogren's syndrome l .pdf
Microbes in human welfare class 12 .pptx
1. INTRODUCTION TO EPIDEMIOLOGY.pptx for community medicine
gene cloning powerpoint for general biology 2
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
A powerpoint on colorectal cancer with brief background
Fluid dynamics vivavoce presentation of prakash
Welcome-grrewfefweg-students-of-2024.pptx
Looking into the jet cone of the neutrino-associated very high-energy blazar ...
PMR- PPT.pptx for students and doctors tt
endocrine - management of adrenal incidentaloma.pptx
Biomechanics of the Hip - Basic Science.pptx
The Land of Punt — A research by Dhani Irwanto
ap-psych-ch-1-introduction-to-psychology-presentation.pptx
Lesson-1-Introduction-to-the-Study-of-Chemistry.pptx

selection