Selection sort
for(int i=0; i<n-1; i++)
{
int min = i;
for(int j=i+1; j<n; j++)
{
if(a[min]>a[j])
{
min = j;
}
}
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
Algorithm
• Start a for loop till reaches the ‘n’
• Select the min for the first for loop variable x
• Start one more for loop till it reaches ‘n’
• Compare the min value with all the elements in the
array
• If any other minimum element is found then the
value of min is altered
• After the end of second for loop swap the minimum
value of array with the current position of x.
• Repeat the above four steps till reaches the first for
loop fails
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
smallest
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 2 6
smallest
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Thank You