#define SELECTIONSORT
namespace mystd
{
template
void SelectionSort(T * sortee, int & size)
{
int min_index = 0;
for (int loop=0;loop
min_index = loop;
for (int innerLoop=loop+1;innerLoop
{
if ( *(sortee + innerLoop)
< *(sortee + min_index))
{
min_index = innerLoop; //O(1)
}
}
T temp = *(sortee + loop);
*(sortee + loop) = *(sortee + min_index);
*(sortee + min_index) = temp;
}
/* Therefore, 1 + O(n) + O(n ^ 2)
SelectionSort = O(N ^ 2)
*/
}
/*
loop 0 - Select the minimum first put it index 0
- 1, 4, 7, 3, 2, 9, 10,
6, 8, 5
loop 1 - Select minimum put it index 1 - start innerLoop = 1
- 1, 2, 7, 3, 4, 9, 10,
6, 8, 5
loop 2 - Select minimum put it index 2 - start innerLoop = 2
- 1, 2, 3, 7, 4, 9, 10,
6, 8, 5
loop 3 - Select minimum put it index 3 - start innerLoop = 3
- 1, 2, 3, 4, 7, 9, 10,
6, 8, 5
loop 4 - Select minimum put it index 4 - start innerLoop = 4
- 1, 2, 3, 4, 5, 9, 10,
6, 8, 7
loop 5 - Select minimum put it index 5 - start innerLoop = 5
- 1, 2, 3, 4, 5, 6, 10,
9, 8, 7
loop 6 - Select minimum put it index 6 - start innerLoop = 6
- 1, 2, 3, 4, 5, 6, 7,
9, 8, 10
loop 7 - Select minimum put it index 7 - start innerLoop = 7
- 1, 2, 3, 4, 5, 6, 7,
8, 9, 10
*/
}
#endif
No comments:
Post a Comment