简体   繁体   English

Java:选择排序停止,没有任何错误消息

[英]Java: selection sort stops without any error message

I'm making a basic program for my computer science class, which is supposed to perform a selection sort on an array.我正在为我的计算机科学课制作一个基本程序,它应该对数组执行选择排序。
Problem is, it stops in the middle of the process and I can't figure out why.问题是,它在过程中间停止,我不知道为什么。
It used to give me an out of bound exception, which it for some reason doesn't do anymore.它曾经给我一个越界异常,它出于某种原因不再这样做了。
I'd appreciate if someone could help me fix it.如果有人能帮我修复它,我将不胜感激。

import java.util.Random;
public class main {

    public static void main(String[] args) {
        int i;
        int j;
        int k;
        int tausch;
        //int size = 10;
        Random rand = new Random();
        int z[] = new int[10];
        for(i=0;i<10;i++) {
            z[i] = rand.nextInt(10) + 1;
        }
        for(i=0;i<z.length;i++) {
            System.out.print(z[i]+", ");
        }
        
        long startTime = System.nanoTime();
        for(i=0;i<z.length;i++) {
            k = i;
            for(j=i+1;j<10;i++) {
                if(z[k] > z[j]) {
                    k = j;
                }
            }
            tausch = z[i];
            z [i] = z[k];
            z[k] = tausch;
        }
        long endTime = System.nanoTime();
        long totalTime = endTime - startTime;
        
        System.out.println();
        for(i=0;i<z.length;i++) {
            System.out.print(z[i]+", ");
        }
        System.out.println();
        System.out.print(totalTime);
        
    }

}

for(j=i+1;j<10;i++)

应该是for(j=i+1;j<10;j++) - 你增加了错误的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM