Java 选择排序交换计数

Java 选择排序交换计数,java,arrays,sorting,Java,Arrays,Sorting,我有一个包含用户输入的数组。该程序是关于冒泡排序,选择排序和插入排序。我无法解决选择部分的问题。交换计数器总是给我数组的长度-1。我知道这是因为第一个for循环,但我找不到任何解决方案 int SComparison = 0; int SSwaps = 0; for (int i = 0; i < SorBag.length - 1; i++) { min = i; for (int j = i + 1; j < SorBag.length; j+

我有一个包含用户输入的数组。该程序是关于冒泡排序,选择排序和插入排序。我无法解决选择部分的问题。交换计数器总是给我数组的长度-1。我知道这是因为第一个for循环,但我找不到任何解决方案

int SComparison = 0;
int SSwaps = 0;

for (int i = 0; i < SorBag.length - 1; i++) {
        min = i;

        for (int j = i + 1; j < SorBag.length; j++) {
            SComparison++;

            if (SorBag[j] < SorBag[min]) {
                min = j;

            }
        }

        SSwaps++;
        temp2 = SorBag[i];
        SorBag[i] = SorBag[min];
        SorBag[min] = temp2;
    }
int SComparison=0;
int-SSwaps=0;
对于(int i=0;i
我试着将SSWAP放入“如果”,它总是给我0。我试着做一个“如果”来找出有多少数组元素比第一个元素小。还是一样。SSwaps++应该放在哪里?谢谢大家!

检查此代码:

public class Test {


    public static void main(String[] args) {

        int SComparison = 0;
         int SSwaps = 0;
         int totalSwap=0;
        int min = 0;
        int temp = 0;
        int SorBag[] = { 1, 4, 3 };

        for (int i = 0; i < SorBag.length - 1; i++) {
            min = i;

            for (int j = i + 1; j < SorBag.length; j++) {
                SComparison++;

                if (SorBag[j] < SorBag[min]) {
                    min = j;
                    temp = SorBag[i];
                    SorBag[i] = SorBag[min];
                    SorBag[min] = temp;
                    SSwaps++;
                    totalSwap+=SSwaps;
                    System.out.println("swap count at "+j+" pass : "+SSwaps);
                }
            }
            SSwaps=0;


        }
        for (int i = 0; i < SorBag.length; i++) 
            System.out.print(SorBag[i]+" ");

        System.out.println("\nTotal swap count :"+totalSwap);
    }


}

选择排序通过比较(您的
如果…
)更新最小索引,并在需要时执行元素交换

您将
交换+++
放在正确的位置。但是如果
min==i
,您应该检查。您只想在
min!时进行交换=我
。相反,你没有检查,只是交换了。这就是为什么你得到了固定的
长度-1

另外,请尝试遵循java命名约定。

有什么问题? 对于您的代码,这将给出4次正确的交换

    int[] SorBag = new int[]{5 ,4 ,3 ,2 ,1};
    int min = 0;
    int SComparison = 0;
    int SSwaps = 0;

    for (int i = 0; i < SorBag.length - 1; i++) {
            min = i;

            for (int j = i + 1; j < SorBag.length; j++) {
                SComparison++;

                if (SorBag[j] < SorBag[min]) {
                    min = j;
                    SSwaps++;
                }
            }


            int temp2 = SorBag[i];
            SorBag[i] = SorBag[min];
            SorBag[min] = temp2;
        }
    System.out.println("sswaps : "+SSwaps);

    for(int i = 0; i<5;i++){
        System.out.println("SorBag["+i+"] = "+SorBag[i]);
    }
}  
int[]SorBag=newint[]{5,4,3,2,1};
int min=0;
int SComparison=0;
int-SSwaps=0;
对于(int i=0;i对于(int i=0;iStill same,当我将数组的元素设为1,2,3,4时,它将为我提供3。此代码始终为我提供0。冒泡排序工作正常,但这里确实存在问题…我将SSwaps++移至if(min!=i)。但现在输出总是0。我也尝试了这个BTW,但效果不太好。@HalilM然后输入已经被排序。假设排序逻辑工作正常。@HalilM和
的第一个
应该是:
I
当我尝试33 21 7 25时,它给出了0。我还删除了-1。但是,如果(min!=I),同样是0://{SSwaps++;}temp2=SorBag[i];SorBag[i]=SorBag[min];SorBag[min]=temp2;我只将SSwaps++移动到if中。尝试将其设为1、2、3、4、5。它也会给出4。
    int[] SorBag = new int[]{5 ,4 ,3 ,2 ,1};
    int min = 0;
    int SComparison = 0;
    int SSwaps = 0;

    for (int i = 0; i < SorBag.length - 1; i++) {
            min = i;

            for (int j = i + 1; j < SorBag.length; j++) {
                SComparison++;

                if (SorBag[j] < SorBag[min]) {
                    min = j;
                    SSwaps++;
                }
            }


            int temp2 = SorBag[i];
            SorBag[i] = SorBag[min];
            SorBag[min] = temp2;
        }
    System.out.println("sswaps : "+SSwaps);

    for(int i = 0; i<5;i++){
        System.out.println("SorBag["+i+"] = "+SorBag[i]);
    }
}