0% found this document useful (0 votes)
7 views30 pages

Selection Sort Algorithm Explained

The document outlines the selection sort algorithm, which sorts an array by repeatedly finding the minimum element and swapping it with the current position. It describes the process through nested loops that compare elements and update the minimum value until the array is sorted. The document includes visual representations of the sorting process with comparisons and data movements.

Uploaded by

shivendra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views30 pages

Selection Sort Algorithm Explained

The document outlines the selection sort algorithm, which sorts an array by repeatedly finding the minimum element and swapping it with the current position. It describes the process through nested loops that compare elements and update the minimum value until the array is sorted. The document includes visual representations of the sorting process with comparisons and data movements.

Uploaded by

shivendra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

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

You might also like