Java 在选择排序中计数交换计数和比较
我对Java有点陌生,在理解一个概念时遇到了困难。在这段代码中,我需要添加交换计数和比较计数,这就是我目前得到的结果Java 在选择排序中计数交换计数和比较,java,selection-sort,Java,Selection Sort,我对Java有点陌生,在理解一个概念时遇到了困难。在这段代码中,我需要添加交换计数和比较计数,这就是我目前得到的结果 public static void SelectionSort ( int [ ] num, int howmany ) { int i, j, first, temp; int compCount; int swapCount; for ( i = num.length - 1; i > 0; i-- ) {
public static void SelectionSort ( int [ ] num, int howmany )
{
int i, j, first, temp;
int compCount;
int swapCount;
for ( i = num.length - 1; i > 0; i-- )
{
compCount = 0;
swapCount = 0;
first = 0; //initialize to subscript of first element
for(j = 1; j <= i; j ++) //locate smallest element between positions 1 and i.
{
compCount++;
if( num[ j ] < num[ first ] )
first = j;
}
temp = num[ first ]; //swap smallest found with element in position i.
num[ first ] = num[ i ];
num[ i ] = temp;
swapCount++;
}
System.out.println("selection sort " + compCount + swapCount );
publicstaticvoidselectionsort(int[]num,int howmount)
{
int i,j,第一,温度;
国际货币基金组织;
国际交换计数;
对于(i=num.length-1;i>0;i--)
{
compCount=0;
swapCount=0;
first=0;//初始化为第一个元素的下标
对于(j=1;j它将不起作用,因为每次迭代都会清除计数器
计数器应在开始时初始化,而不仅仅是递增的
此代码应该更好,但未经测试
public static void SelectionSort ( int [ ] num, int howmany )
{
int i, j, first, temp;
int compCount=0;
int swapCount=0;
for ( i = num.length - 1; i > 0; i-- )
{
first = 0; //initialize to subscript of first element
for(j = 1; j <= i; j ++) //locate smallest element between positions 1 and i.
{
compCount++;
if( num[ j ] < num[ first ] )
first = j;
}
temp = num[ first ]; //swap smallest found with element in position i.
num[ first ] = num[ i ];
num[ i ] = temp;
swapCount++;
}
System.out.println("selection sort " + compCount + swapCount );
}
publicstaticvoidselectionsort(int[]num,int howmount)
{
int i,j,第一,温度;
int compCount=0;
int swapCount=0;
对于(i=num.length-1;i>0;i--)
{
first=0;//初始化为第一个元素的下标
对于(j=1;jpublicstaticvoidselectionsort(int[]num,int howmount)
{
int i,j,第一,温度;
int compCount=0;
int swapCount=0;
对于(i=num.length-1;i>0;i--)
{
/*您不应该在for循环中重新初始化swap count和compCount。这样做的目的是在每次迭代后将其设为0,这不是您想要的*/
first=0;//初始化为第一个元素的下标
对于(j=1;jssort类{
静态无效选择_排序(int arr[],int n)
{
整数交换=0;
对于(int i=0;iSame idea作为我的答案,这是一个解决方案(+1)没有问题。请您将其中一个答案标记为正确,以便它可以帮助其他人。使用选择排序计算最小交换数,这是一个有效的编码。请将您的注释添加到您的答案中!
public static void SelectionSort ( int [ ] num, int howmany )
{
int i, j, first, temp;
int compCount = 0;
int swapCount = 0;
for ( i = num.length - 1; i > 0; i-- )
{
/* you should not be reinitializing swap count and compCount inside the for loop. What this does is make it 0 after each iteration which is not what you want*/
first = 0; //initialize to subscript of first element
for(j = 1; j <= i; j ++) //locate smallest element between positions 1 and i.
{
compCount++;
if( num[ j ] < num[ first ] )
first = j;
}
temp = num[ first ]; //swap smallest found with element in position i.
num[ first ] = num[ i ];
num[ i ] = temp;
swapCount++;
}
System.out.println("selection sort " + compCount + swapCount );
class ssort {
static void selection_sort(int arr[], int n)
{
int swap=0;
for(int i=0;i<n;i++)
{
//find min
int min_indx=i;
int old_min=min_indx;
for(int j=i+1;j<n;j++)
{
if(arr[j]< arr[min_indx]){
min_indx=j;
}
}
if(old_min != min_indx)
{
int temp= arr[min_indx];//swap`enter code here`
arr[min_indx]=arr[i];
arr[i]=temp;
swap++;
}
}
System.out.println("Total Swaps "+swap);
}
public static void main(String[] args) {
int arr[]=new int[]{7, 1, 3, 2, 4, 5, 6};
int n=arr.length;
selection_sort(arr, n);
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
}
}