我有这段代码,需要一些帮助来计算交换次数。我想我需要一个for循环,它是标记,但我不退出了解如何提取交换计数。提前致谢。Java选择排序交换计数
public class Selection
{
public static void SelectionSort (int [ ] num, int howmany)
{
int i, j, first, temp;
int comparecount = 0;
int swapcount = 0;
for (i = num.length - 1; i > 0; i--)
{
first = 0;
for(j = 1; j <= i; j ++)
{
comparecount++;
if(num[ j ] < num[ first ])
first = j;
}
temp = num[ first ]; //need to count swaps ???
num[ first ] = num[ i ];
num[ i ] = temp;
}
System.out.print(comparecount);
System.out.print(swapcount);
}
}
你能不能只是增加'在您的评论swapcount'? – Breavyn 2013-05-07 00:53:41
另外'int howmany'用在哪里?为什么在那里? – Breavyn 2013-05-07 00:58:06
我是通过它的东西,并没有删除它 – 2013-05-07 01:07:38