我有这段代码,需要一点帮助来计算掉期的数量。我想我需要一个标记为 for 的循环,但我不明白如何提取交换计数。提前致谢。
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);
}
}