Java 中的递归冒泡排序

发布于 2024-12-10 06:50:50 字数 517 浏览 1 评论 0原文

我正在尝试用 Java 编写递归冒泡排序,但出现索引越界异常。我做错了什么以及为什么会收到此错误? 这是我的代码:

public static  <T extends Comparable< ? super T>>
    void sort(T [] a){
    T tmp;
    for(int i=0;i<a.length;i++){
        if(a[i].compareTo(a[i+1])>0){
            tmp = a[i];
            a[i]=a[i+1];
            a[i+1]=tmp;
            sort(a);
        }
        System.out.println("i:"+i+" "+a[i]);

    }

另外,即使它对数组进行排序并且我在最后收到错误,它正在打印所有步骤,如何让它打印最后一个最终排序的数组? 这可能是一个简单的答案,但我的大脑现在很混乱,无法正常思考。 提前致谢。

I am trying to write a Recursive Bubble sort in Java and I'm getting an Index Out of Bounds Exception. What am I doing wrong and why am I getting this error?
Here is my code:

public static  <T extends Comparable< ? super T>>
    void sort(T [] a){
    T tmp;
    for(int i=0;i<a.length;i++){
        if(a[i].compareTo(a[i+1])>0){
            tmp = a[i];
            a[i]=a[i+1];
            a[i+1]=tmp;
            sort(a);
        }
        System.out.println("i:"+i+" "+a[i]);

    }

Also, even tho it sorts the array and I get the error at the end, it is printing all of the steps, how do I make it print the last final sorted array?
Is probably a simple answer but my brain is fried right now and can't think straight.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

断桥再见 2024-12-17 06:50:50

当 i i i i i i i i i i i a.length-1 ,因为在您的代码中您访问位置 i+1 ,并且当 i == a.length - 1i+1 将尝试访问数组末尾不存在的元素 - 因此,超出范围。

The loop should stop when i < a.length-1 , because in your code you access the position i+1, and when i == a.length - 1 then i+1 will be trying to access an element off the end of the array that doesn't exist - hence, out of bounds.

命比纸薄 2024-12-17 06:50:50

您已经编写了循环,以便 i 得到与 a.length - 1 一样的位。然后,您尝试访问a[i+1],当i == a.length - 1时,这是一个越界索引。将循环限制降低 1。

You've written your loop so that i gets as bit as a.length - 1. Then you are trying to access a[i+1], which is an out-of-bounds index when i == a.length - 1. Lower your loop's limit by 1.

平定天下 2024-12-17 06:50:50

上限应该是length-1

for(int i=0;i<a.length-1;i++)

The upper limit should be length-1

for(int i=0;i<a.length-1;i++)
撩人痒 2024-12-17 06:50:50

发生 IndexOutOfBoundsException 是因为当 i 位于最后一个索引 (a.length 时,循环尝试访问 a[i+1] - 1),超出范围。

要修复此问题,请调整循环运行直到 i i i i i i i i i i i i i a.length - 1,因此a[i+1]始终有效:

public static <T extends Comparable<? super T>> void sort(T[] a) {
    T tmp;
    boolean swapped = false;

    for (int i = 0; i < a.length - 1; i++) {  
        if (a[i].compareTo(a[i + 1]) > 0) {
            tmp = a[i];
            a[i] = a[i + 1];
            a[i + 1] = tmp;
            swapped = true;
        }
    }

    if (swapped) {
        sort(a); 
    } else {
        // Print the final sorted array
        for (T element : a) {
            System.out.print(element + " ");
        }
        System.out.println();
    }

发生错误是因为循环试图访问不存在的索引(a[ i+1]i 位于 a.length - 1) 时。该修复确保有效的索引访问并让数组正确排序,最后仅打印一次最终排序的数组。

The IndexOutOfBoundsException happens because your loop tries to access a[i+1] when i is at the last index (a.length - 1), which goes out of bounds.

To fix it, adjust the loop to run until i < a.length - 1, so a[i+1] is always valid:

public static <T extends Comparable<? super T>> void sort(T[] a) {
    T tmp;
    boolean swapped = false;

    for (int i = 0; i < a.length - 1; i++) {  
        if (a[i].compareTo(a[i + 1]) > 0) {
            tmp = a[i];
            a[i] = a[i + 1];
            a[i + 1] = tmp;
            swapped = true;
        }
    }

    if (swapped) {
        sort(a); 
    } else {
        // Print the final sorted array
        for (T element : a) {
            System.out.print(element + " ");
        }
        System.out.println();
    }

The error happened because the loop tried to access an index that doesn’t exist (a[i+1] when i is at a.length - 1). The fix ensures valid index access and lets the array sort correctly, printing the final sorted array only once at the end.

谁人与我共长歌 2024-12-17 06:50:50

试试这个。

 public static void bubbleSort(Object[] source, int fromIndex, int endIndex){
            if(fromIndex==endIndex){
                return;
            }
            else{
                if(((Comparable) source[fromIndex]).compareTo(source [fromIndex+1])>0){
                    Object temp=source[fromIndex];
                    source[fromIndex]=source[fromIndex+1];
                    source[fromIndex+1]=temp;
                }
                bubbleSort(source,fromIndex+1,endIndex);
            }
            bubbleSort(source,fromIndex,endIndex-1);
        }

Try this.

 public static void bubbleSort(Object[] source, int fromIndex, int endIndex){
            if(fromIndex==endIndex){
                return;
            }
            else{
                if(((Comparable) source[fromIndex]).compareTo(source [fromIndex+1])>0){
                    Object temp=source[fromIndex];
                    source[fromIndex]=source[fromIndex+1];
                    source[fromIndex+1]=temp;
                }
                bubbleSort(source,fromIndex+1,endIndex);
            }
            bubbleSort(source,fromIndex,endIndex-1);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文