SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
Algorithms
Selection Sort
Abdelrahman M. Saleh
List of contents
● Introduction
● Example w/ illustrating figures
● Algorithm
● implementation (Java, C++, Python)
● Performance Runtime
○ Best, Average and worst cases.
● Execution
● Other Notes
Introduction
● sorts an array by repeatedly finding the minimum element (considering ascending
order) from unsorted part and putting it at the beginning .
● The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
● In every iteration ,the minimum element (considering ascending order) from the
unsorted subarray is picked and moved to the sorted subarray .
Example
arr[] = 19, 5, 7, 12
// Find the minimum element in arr[0...3]
// and place it at beginning
5, 19, 7, 12
// Find the minimum element in arr[1...3]
// and place it at beginning of arr[1...3]
5, 7, 19, 12
// Find the minimum element in arr[2...3]
// and place it at beginning of arr[2...3]
5, 7, 12, 19
Algorithm .
● Let the min : 0
● Search the minimum
● Swap with value at location min
● Increment min
● Repeat until is sorted
Implementation .
C++ .
void selectionSort(int a[], int len)
{
int min, i, j ;
for(i=0; i<len-1; i++)
{
min = i ;
for(j=i+1; j<len; j++)
{
if(a[j] < a[min])
min = j ;
}
swap(&a[i], &a[min]) ;
}
}
JAVA .
void sort(int a[])
{
int len = a.length, min;
for (int i = 0; i < len-1; i++)
{
min = i;
for (int j=i+1; j<len; j++)
if (a[j] < a[min])
min = j;
int temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
Python .
def selectionSort(a):
for i in range(len(a)):
min = i
for j in range(i+1, len(a)):
if a[min] > a[j]:
min = j
a[i], a[min] = a[min], a[i]
Performance Runtime
● Time complexity Θ(n2
) .
● it never makes more than O(n) swaps =>
can be useful when memory write is a costly operation .
Execution .
Input array : [4, 6, 3, 2, 1, 9, 7]
Swap => 4, 1
First Action : [1, 6, 3, 2, 4, 9, 7]
Swap => 6, 2
Second Action : [1, 2, 3, 6, 4, 9, 7]
Swap => 6, 4
Fourth Action : [1, 2, 3, 4, 6, 9, 7]
Swap => 9, 7
Sixth Action : [1, 2, 3, 4, 6, 7, 9]
Output Array: [1, 2, 3, 4, 6, 7, 9]
Other Notes .
● Algorithmic Paradigm: Incremental Approach
● Sorting In Place: Yes
● Stable: Yes
● Online: Yes

More Related Content

PPTX
Sorting Algorithms
Pranay Neema
 
PPTX
Presentation on the topic selection sort
District Administration
 
PPT
Selection sort
stella D
 
PDF
Sorting
Zaid Shabbir
 
PPTX
Divide and conquer - Quick sort
Madhu Bala
 
PPTX
Insertion sort
almaqboli
 
PPTX
Insertion sort algorithm power point presentation
University of Science and Technology Chitttagong
 
PPTX
Quick sort
amar kakde
 
Sorting Algorithms
Pranay Neema
 
Presentation on the topic selection sort
District Administration
 
Selection sort
stella D
 
Sorting
Zaid Shabbir
 
Divide and conquer - Quick sort
Madhu Bala
 
Insertion sort
almaqboli
 
Insertion sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Quick sort
amar kakde
 

What's hot (20)

PPTX
Selection sorting
Himanshu Kesharwani
 
PPTX
Insertion sort
Monalisa Patel
 
PPTX
Selection sort 1
asmhemu
 
PDF
Linear search algorithm
NeoClassical
 
PPTX
Sorting algorithms
Maher Alshammari
 
PPTX
Insertion Sort
Brett Duncan
 
PPTX
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
PPTX
Quick sort-Data Structure
Jeanie Arnoco
 
PPT
Data Structures - Searching & sorting
Kaushal Shah
 
PPTX
Selection sort
Jay Patel
 
PPT
Algorithm: Quick-Sort
Tareq Hasan
 
PPTX
Binary search
AparnaKumari31
 
PPT
Selection sort
amna izzat
 
PPT
Deletion operation in array(ds)
chauhankapil
 
PPT
3.2 insertion sort
Krish_ver2
 
PPTX
Selection sort
smlagustin
 
PPTX
Ppt on Linked list,stack,queue
Srajan Shukla
 
PPT
Hash table
Rajendran
 
PPTX
Quick sort
Jehat Hassan
 
Selection sorting
Himanshu Kesharwani
 
Insertion sort
Monalisa Patel
 
Selection sort 1
asmhemu
 
Linear search algorithm
NeoClassical
 
Sorting algorithms
Maher Alshammari
 
Insertion Sort
Brett Duncan
 
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
Quick sort-Data Structure
Jeanie Arnoco
 
Data Structures - Searching & sorting
Kaushal Shah
 
Selection sort
Jay Patel
 
Algorithm: Quick-Sort
Tareq Hasan
 
Binary search
AparnaKumari31
 
Selection sort
amna izzat
 
Deletion operation in array(ds)
chauhankapil
 
3.2 insertion sort
Krish_ver2
 
Selection sort
smlagustin
 
Ppt on Linked list,stack,queue
Srajan Shukla
 
Hash table
Rajendran
 
Quick sort
Jehat Hassan
 
Ad

Similar to Selection sort (20)

PPT
Sorting algorithms
CHANDAN KUMAR
 
PDF
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
fickolatigo
 
PPTX
Sorting techniques
JayeshGadhave1
 
PPTX
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Pratik Parmar
 
PPT
Data Structure (MC501)
Kamal Singh Lodhi
 
PPTX
sorting1.pptx
AJAYVISHALRP
 
PPTX
Data structure presentation sorting
Pranjali Rawat
 
PDF
Selection sort
Raghu nath
 
PPTX
sorting-160810203705.pptx
VarchasvaTiwari2
 
PPTX
Bubble sort
Abdelrahman Saleh
 
PDF
Insertion sort
Abdelrahman Saleh
 
PPT
Sorting techniques
Lovely Professional University
 
PPTX
Dsa – data structure and algorithms sorting
sajinis3
 
PPTX
Sorting method data structure
sunilchute1
 
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
PPTX
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
PPTX
Searching and sorting Techniques in Data structures
PRIANKA R
 
PPTX
Unit vii sorting
Tribhuvan University
 
PPT
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Sorting algorithms
CHANDAN KUMAR
 
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
fickolatigo
 
Sorting techniques
JayeshGadhave1
 
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Pratik Parmar
 
Data Structure (MC501)
Kamal Singh Lodhi
 
sorting1.pptx
AJAYVISHALRP
 
Data structure presentation sorting
Pranjali Rawat
 
Selection sort
Raghu nath
 
sorting-160810203705.pptx
VarchasvaTiwari2
 
Bubble sort
Abdelrahman Saleh
 
Insertion sort
Abdelrahman Saleh
 
Sorting techniques
Lovely Professional University
 
Dsa – data structure and algorithms sorting
sajinis3
 
Sorting method data structure
sunilchute1
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
Searching and sorting Techniques in Data structures
PRIANKA R
 
Unit vii sorting
Tribhuvan University
 
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Ad

Recently uploaded (20)

PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 

Selection sort

  • 2. List of contents ● Introduction ● Example w/ illustrating figures ● Algorithm ● implementation (Java, C++, Python) ● Performance Runtime ○ Best, Average and worst cases. ● Execution ● Other Notes
  • 3. Introduction ● sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning . ● The algorithm maintains two subarrays in a given array. 1) The subarray which is already sorted. 2) Remaining subarray which is unsorted. ● In every iteration ,the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray .
  • 4. Example arr[] = 19, 5, 7, 12 // Find the minimum element in arr[0...3] // and place it at beginning 5, 19, 7, 12 // Find the minimum element in arr[1...3] // and place it at beginning of arr[1...3] 5, 7, 19, 12 // Find the minimum element in arr[2...3] // and place it at beginning of arr[2...3] 5, 7, 12, 19
  • 5. Algorithm . ● Let the min : 0 ● Search the minimum ● Swap with value at location min ● Increment min ● Repeat until is sorted
  • 7. C++ . void selectionSort(int a[], int len) { int min, i, j ; for(i=0; i<len-1; i++) { min = i ; for(j=i+1; j<len; j++) { if(a[j] < a[min]) min = j ; } swap(&a[i], &a[min]) ; } }
  • 8. JAVA . void sort(int a[]) { int len = a.length, min; for (int i = 0; i < len-1; i++) { min = i; for (int j=i+1; j<len; j++) if (a[j] < a[min]) min = j; int temp = a[min]; a[min] = a[i]; a[i] = temp; } }
  • 9. Python . def selectionSort(a): for i in range(len(a)): min = i for j in range(i+1, len(a)): if a[min] > a[j]: min = j a[i], a[min] = a[min], a[i]
  • 10. Performance Runtime ● Time complexity Θ(n2 ) . ● it never makes more than O(n) swaps => can be useful when memory write is a costly operation .
  • 11. Execution . Input array : [4, 6, 3, 2, 1, 9, 7] Swap => 4, 1 First Action : [1, 6, 3, 2, 4, 9, 7] Swap => 6, 2 Second Action : [1, 2, 3, 6, 4, 9, 7] Swap => 6, 4 Fourth Action : [1, 2, 3, 4, 6, 9, 7] Swap => 9, 7 Sixth Action : [1, 2, 3, 4, 6, 7, 9] Output Array: [1, 2, 3, 4, 6, 7, 9]
  • 12. Other Notes . ● Algorithmic Paradigm: Incremental Approach ● Sorting In Place: Yes ● Stable: Yes ● Online: Yes