Sunteți pe pagina 1din 7

1|Page

Name :
No NAme
Section :

Roll No :

Subject :
Data Structure & Algorithem
Submitted To :
2|Page

Selection Sort
selection sort ( int array[] , int size )
int temp;
for( int i=0 ; i< n-1 ; i++) ( n+1)
Int min = I n
for ( int j = i+1 : j<n : j++) { n (n+1)
If ( array [j] < array [min] ) { n
min=j;
}
}

temp = array [ j ] n

array[ j ]= array [min] n

array [min] = temp n

0 1 2 3 4 5

7 8 5 2 4 6

Oth itration of i

I = i+1 1st itration of j

0 2 3 4 5 6

7 8 5 2 4 6

I , min j

8<7 x

2nd itration of j

7 8 5 2 4 6

I min,j
3|Page

3rd itration of j

2<5

0 1 2 3 4 5

7 8 5 2 4 6

I min ,j

4th itraion of j

4<2 x

0 1 2 3 4 5

7 8 5 2 4 6

I min j

5th itration of j

6<2 x

0 1 2 3 4 5

7 8 5 2 4 6

I min j

Loop terminates

2 8 5 7 4 6

1 st itration of i

0 1 2 3 4 5

2 8 5 7 4 6

I ,min j

1st itration of j

5<8 true
4|Page

0 1 2 3 4 5

2 8 5 7 4 6

I min , j

2nd itration of j

7<5 false

0 1 2 3 4 5

2 8 5 7 4 6

I min j

3rd itration of j

4<5 true

0 1 2 3 4 5

2 8 5 7 4 6

I min, j

4th itration of j

7<5 false

0 1 2 3 4 5

2 8 5 7 4 6

I min j

0 1 2 3 4 5

2 4 5 7 8 6

2nd itration of i
5|Page

0 1 2 3 4 5

2 4 5 7 8 6

I , min

1st itration of j

7<5 false

0 1 2 3 4 5

2 4 5 7 8 6

I ,min j

2nd itration of j

8<5 false

0 1 2 3 4 5

2 4 5 7 8 6

I ,min j

3rd itration of j

6<5 false

0 1 2 3 4 5

2 4 5 7 8 6
2 4 5 7 8 6

I ,min j

Swap with itself

2 4 5 7 8 6

3rd itration of i
2 4 5 7 8 6

I , min
6|Page

1st itration of j

8<7 false

0 1 2 3 4 5

2 4 5 7 8 6

I , min j

2nd itration of j

6<7 false

0 1 2 3 4 5

2 4 5 7 8 6

I j,mim

Swap itself

2 4 5 6 8 7

4th itration of i
0 1 2 3 4 5
2 4 5 6 8 7

I,min

1st itration of j

7< 8 true

0 1 2 3 4 5
2 4 5 6 8 7

I j,min

Assending order

2 4 5 6 7 8
7|Page

S-ar putea să vă placă și