Java 我的插入排序有什么问题?

Java 我的插入排序有什么问题?,java,insertion-sort,Java,Insertion Sort,我不确定我的插入排序有什么问题。似乎对于i的每一个增量,数组[i]都被复制到数组[i+1],依此类推,直到整个数组被原始对象数组[i]填充` public static void insertionSort(Course[] courseArray, String sortBy) { Course value; // the next value from the unsorted list to be inserted into the sorted list int i;

我不确定我的插入排序有什么问题。似乎对于i的每一个增量,数组[i]都被复制到数组[i+1],依此类推,直到整个数组被原始对象数组[i]填充`

public static void insertionSort(Course[] courseArray, String sortBy) {
    Course value;   // the next value from the unsorted list to be inserted into the sorted list
    int i;     // i is a pointer to an item in the unsorted list
    int j;    // j is a pointer to an item in the sorted list; originally the sorted list is just a[0]

    for (i = 1; i < courseArray.length - 1; i++) 
    {

        value = courseArray[i];

        j = i - 1;

        while (j >= 0 && (courseArray[j].compareByCourse(value)) < 0) {
            courseArray[j + 1] = courseArray[j];

            j = j - 1;

        }
        courseArray[i + 1] = value;

        System.out.println("i= " + i + "--------------------------------------");

        for (int p = 0; p < courseArray.length; p++) 
        {
            System.out.println(courseArray[p].toString());
        }

    }//end for

}//end insertionSort()`
publicstaticvoidinsertionsort(课程[]courserray,字符串排序){
课程值;//未排序列表中要插入排序列表的下一个值
int i;//i是指向未排序列表中的项的指针
int j;//j是指向排序列表中某项的指针;最初排序列表只是一个[0]
对于(i=1;i=0&(courserray[j].compareByCourse(value))<0){
Courserray[j+1]=Courserray[j];
j=j-1;
}
courseArray[i+1]=值;
System.out.println(“i=“+i+”---------------------------------------------”);
对于(int p=0;p
我看过许多插入排序示例,我觉得我写的好像是正确的,但显然我错了。

您必须交换(交换)内部循环中的值(使用if条件)

您只是在覆盖其中一个


祝你好运。

它怎么不起作用?你期望什么样的产出,你得到了什么?是否收到任何错误?数组基于0。将循环更改为从0开始i。
courserray[i+1]=值应为
courserray[j+1]=值