SELECTION SORT
Vipin Ramola
PGT Comp. Sc.
KV Balasore
Bhubaneswar Region
1
2
OBJECTIVES
 To learn sorting
 To learn about Selection Sort – writing
program
 Application of Selection Sort
 Advantages & Disadvantages
3
PREVIOUS KNOWLEDGE
 Variables
 Loops
 Functions
4
Sorting
 Arranging elements/data/items in ascending
or descending order of values.
 Fundamental problems in computer science
and programming
 Sorting done to make searching easier
 Multiple different algorithms to solve the
same problem
5
Selection sort
 Algorithm
– Search through the list and find the smallest element
– swap the smallest element with the first element
– repeat starting at second element and find the second
smallest element and so on
Program
6
for(i=0; i< n-1; i++)
{
/* find the index of min element in the unsorted a[j ..
n-1] assume the min is the first element */
minIndex=i;
// test against elements after j to find the smallest
for(j=i+1; j< n; j++)
{
// if this element is less, then it is the new minimum
if(A[j] < A[minIndex])
minIndex = j;
}
/* iMin is the index of the minimum element. Swap it
with the current position */
if(minIndex!=i)
{
temp = A[i];
A[i]=A[minIndex];
A[minIndex] = temp;
}
}
7
ANIMATION
8
Applications of Selection Sort
 In embedded systems
 In basic mobiles
Advantages & Disadvantages
Advantages
Easy/simple implementation.
Useful when no. of elements are less.
Can be used when memory is less.
Disadvantages
It is slower when no of elements are more.
Performance varies with initial order of Input
9
Thank You
10

Selection Sort - Vipin Ramola

  • 1.
    SELECTION SORT Vipin Ramola PGTComp. Sc. KV Balasore Bhubaneswar Region 1
  • 2.
    2 OBJECTIVES  To learnsorting  To learn about Selection Sort – writing program  Application of Selection Sort  Advantages & Disadvantages
  • 3.
  • 4.
    4 Sorting  Arranging elements/data/itemsin ascending or descending order of values.  Fundamental problems in computer science and programming  Sorting done to make searching easier  Multiple different algorithms to solve the same problem
  • 5.
    5 Selection sort  Algorithm –Search through the list and find the smallest element – swap the smallest element with the first element – repeat starting at second element and find the second smallest element and so on
  • 6.
    Program 6 for(i=0; i< n-1;i++) { /* find the index of min element in the unsorted a[j .. n-1] assume the min is the first element */ minIndex=i; // test against elements after j to find the smallest for(j=i+1; j< n; j++) { // if this element is less, then it is the new minimum if(A[j] < A[minIndex]) minIndex = j; } /* iMin is the index of the minimum element. Swap it with the current position */ if(minIndex!=i) { temp = A[i]; A[i]=A[minIndex]; A[minIndex] = temp; } }
  • 7.
  • 8.
    8 Applications of SelectionSort  In embedded systems  In basic mobiles
  • 9.
    Advantages & Disadvantages Advantages Easy/simpleimplementation. Useful when no. of elements are less. Can be used when memory is less. Disadvantages It is slower when no of elements are more. Performance varies with initial order of Input 9
  • 10.