java 递归插入排序

发布于 2024-12-07 04:22:50 字数 798 浏览 2 评论 0原文

我有一个数组,必须使用插入排序对它们进行排序。我尝试使用 CompareTo 方法来遍历数组并查看更大的值。我遇到了一个问题,因为我试图用一个显然不起作用的字符串引用数组索引(即在compareTo(a[key]))。

任何有关如何执行此操作的建议或提示将不胜感激。

这就是我到目前为止所拥有的。这是一个好的开始吗?还是朝着正确的方向开始?

 public void insertionSort() 
    { 
        insertionSort(a.length-1);
    } 



    private void insertionSort(int n)
    {
        String temp; 
        if(n <= 1)
        {
        //Do nothing, easiest case
        }

        else
        {
        for(int i = 1; i < a.length; i++)
        {
        int j;
        String key = a[i];

            while((j >= 0) && (a[i].compareTo(a[key]) > 0))
            {
            a[i+1] = a[i];
            j--;
            }
            a[i+1] = key;
        }   

        insertionSort(n-1);

        }
    } 

i have an array and have to sort them using insertion sort. i tried to use the compareTo method to run through the array and see whats bigger. I ran into a problem because i was trying to reference an array index with a string which obviously didntwork (thats at compareTo(a[key])).

any suggestions or hints as to how to do this would be appreciated.

this is what i have so far. is it a good start? or a start in the right direction?

 public void insertionSort() 
    { 
        insertionSort(a.length-1);
    } 



    private void insertionSort(int n)
    {
        String temp; 
        if(n <= 1)
        {
        //Do nothing, easiest case
        }

        else
        {
        for(int i = 1; i < a.length; i++)
        {
        int j;
        String key = a[i];

            while((j >= 0) && (a[i].compareTo(a[key]) > 0))
            {
            a[i+1] = a[i];
            j--;
            }
            a[i+1] = key;
        }   

        insertionSort(n-1);

        }
    } 

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

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

发布评论

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

评论(3

逆光下的微笑 2024-12-14 04:22:50

我的第一个建议是,如果传递所需的参数,通常会更容易理解方法。完全不清楚 a 是什么;我希望公共 insertionSort 方法将要排序的对象作为参数。 (我想如果你在自己的类似列表的类上定义它,它并没有那么糟糕,但听起来不像是这样的用法)。

同样,我也不完全确定 n 应该是什么(大概是您知道已排序的索引),但您根本不在私有方法的主体中使用它,所以您只是做同样的事情n次。

您似乎还交换了 a 的元素,而在插入排序中不需要这样做。这看起来更像是冒泡排序。

首先尝试将方法编写为伪代码(例如注释)以布局您的方法,然后用一小段代码充实每个注释。这样做可以避免过于陷入细节,通常概念性错误会显得更加明显,也更容易避免。这可能看起来像:

public static int[] insertionSort(int[] input) {
    // Create the array to hold the results

    // Iterate through the contents of input and insert each one into
    // the result array in order (method call here)

    // return the result array
}

private void insertOneElement(int toInsert, int[] resultArray) {
    // Compare toInsert with first element of resultArray

    // etc.
}

My first suggestion would be that it's usually easier to understand a method if you pass in the required arguments. It's not clear at all what a is; I'd expect the public insertionSort method to take as an argument the objects to sort. (I suppose if you're defining this on your own list-like class it's not as bad, but it doesn't sound like that's the usage).

Likewise I'm not entirely sure what n is supposed to be (presumably the index beyond which you know is sorted) but you don't use it at all in the body of the private method, so you're just doing the same thing n times.

You also appear to be swapping elements of a, which you shouldn't need to do in an insertion sort. This looks more like a bubble sort.

Try writing the method as pseudocode (e.g. comments) first to lay out your approach, then flesh out each comment with a small bit of code. Doing this can avoid getting too bogged down in the detail, and usually conceptual mistakes will appear more obvious, and be easier to avoid. This might look something like:

public static int[] insertionSort(int[] input) {
    // Create the array to hold the results

    // Iterate through the contents of input and insert each one into
    // the result array in order (method call here)

    // return the result array
}

private void insertOneElement(int toInsert, int[] resultArray) {
    // Compare toInsert with first element of resultArray

    // etc.
}
请止步禁区 2024-12-14 04:22:50

替换内循环如下:

j = i - 1;  //initialize j!!!
String key = a[i];   //take value
while((j >= 0) && (a[j].compareTo(key) > 0)){//Compare with key!!!
     a[j+1] = a[j];
     j--;
}
a[j + 1] = key; //index is j here!!!!

Replace the inner loop as follows:

j = i - 1;  //initialize j!!!
String key = a[i];   //take value
while((j >= 0) && (a[j].compareTo(key) > 0)){//Compare with key!!!
     a[j+1] = a[j];
     j--;
}
a[j + 1] = key; //index is j here!!!!
芯好空 2024-12-14 04:22:50

只需将其更改为 a[j].compareTo(key) (请注意,您要比较 a[j],而不是 a[i])。您还需要初始化 j,正如 smas 所评论的那样。

Just change it to a[j].compareTo(key) (note that you want to compare a[j], not a[i]). You also need to initialize j, as smas commented.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文