Uploaded by wegepi9592

Selection Sort

advertisement
Selection Sort:
============
The selection sort algorithm 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
sub arrays in a given array
1) The subarray which is already sorted
2) Remaining subarray which is unsorted
In every iteration of selection sort, the minimum element (considering ascending order) from the
unsorted subarray is picked and moved to the sorted subarray.
Arr ➔ N=6
0
7
1
4
2
10
3
8
4
3
5
1
0
1
1
4
2
10
3
8
4
3
5
7
0
1
1
3
2
10
3
8
4
4
5
7
0
1
1
3
2
4
3
8
4
10
5
7
0
1
1
3
2
4
3
7
4
10
5
8
Pass 1 :
Pass 2:
Pass 3 :
Pass 4:
Pass 5:
0
1
1
3
2
4
Selection Sort Algorithm pseudocode
===============================
For(i=0;I<n-1;i++)
{
Int min =I;
For(j=i+1; j<n;j++)
{
If (arr[j]<arr[min])
{
Min=j;
}
If(min!=i)
{
Swap(arr[i],arr[min]);
}
}
}
Trace:
I=0;
Min=0;
J=1;
Arr[1]<arr[0] ? min=1
Arr[2]<arr[1] ?
Arr[3] <arr[1] ?
Arr[4]<arr[1] ? min=4
3
7
4
8
5
10
Arr[5]<arr[4]? Min=5
O(n2)
Download