int aux;
for(int i=0;i<array.Count()-1;i++)
{
for(int j=i+1;j<array.Count();j++)
{
if(array[i] > array[j])
{
aux = array[j开发者_运维百科];
array[j] = array[i];
array[i] = aux;
}
}
}
This is a dumbed down selection sort. Instead of swapping array[i]
with the minimum element after it, you just swap it with each smaller element. Eventually the correct element will obviously end up in the right position, and you do write less code.
This is a lot less efficient because more swaps are performed, but it's basically selection sort.
This is almost selection sort, except that you don't swap the minimum remaining element with the current element, but you swap every remaining element that's smaller than the current with the current element until the current element is the minimum.
精彩评论