Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何递归执行插入排序_Java_Sorting_Recursion_Insertion Sort - Fatal编程技术网

Java 如何递归执行插入排序

Java 如何递归执行插入排序,java,sorting,recursion,insertion-sort,Java,Sorting,Recursion,Insertion Sort,我有两种方法打算对int数组执行插入排序。第一种方法执行插入排序算法的外循环,而第二种方法递归地将特定元素插入正确的位置。例如: int [] list = {20, 50, 30, 10, 60, 40}; sort(list); 结果应该是{10,20,30,40,50,60} 我的问题是插入法;这是我的密码: public static void sort(int[] list) { int position; for (position = 1; position &l

我有两种方法打算对int数组执行插入排序。第一种方法执行插入排序算法的外循环,而第二种方法递归地将特定元素插入正确的位置。例如:

int [] list = {20, 50, 30, 10, 60, 40};
sort(list);
结果应该是
{10,20,30,40,50,60}
我的问题是插入法;这是我的密码:

public static void sort(int[] list) {
    int position;
    for (position = 1; position < list.length; position++) {
        insert(list, list[position], position);
    }
}

public static void insert(int[] list, int element, int position) {
    int position2 = position - 1;
    if (position == list.length - 1) {
        // do nothing
    } else {
        while ((position > 0) && (list[position - 1] > element)) {
            element = list[position - 1];
            insert(list, element, position - 1);
        }
        list[position] = element;
    }
}

我不完全理解递归。请帮助

当您使某些函数递归时,应该有一个递归结束条件以及其他条件

运行此版本并尝试查看您做错了什么

请尝试以下版本:

public static void insertInOrder(int element, int[] a, int first, int last) {
    if (element >= a[last])
        a[last + 1] = element;
    else if (first < last) {
        a[last + 1] = a[last];
        insertInOrder(element, a, first, last - 1);
    } 
    else // first == last and element < a[last]
    {
        a[last + 1] = a[last];
        a[last] = element;
    }
}

public static void insertion_sort_recur(int[] arr, int first, int last) {
    if (first < last) {
        insertion_sort_recur(arr, first, last - 1); // avoids looping thru arr[0..last-1]
        insertInOrder(arr[last], arr, first, last - 1); // considers arr[last] as the first element in the unsorted list
    }
}

public static void main(String args[]) {
    int A[] = { 5, 3, 2, 4, 6, 1 };
    insertion_sort_recur(A, 0, 5);
    for(int i=0; i < A.length; i++) {
        System.out.println(A[i]);
    }
}
publicstaticvoidinsertinoder(int元素,int[]a,int-first,int-last){
如果(元素>=a[最后])
a[最后+1]=元素;
否则如果(第一次<最后一次){
a[last+1]=a[last];
插入器(元素,a,第一个,最后一个-1);
} 
else//first==last和element
资料来源:

通过将else作为整数数组提供,并将
sortedIndex
作为第一个元素的索引和
index
作为第二个元素的索引,尝试以下代码:

public static void insertionSort(int[] ele, int sortedIndex, int index) {
            if (sortedIndex < ele.length) {
                if (index < ele.length) {
                    if (ele[sortedIndex] > ele[index]) {
                        ele[sortedIndex] += ele[index];
                        ele[index] = ele[sortedIndex] - ele[index];
                        ele[sortedIndex] = ele[sortedIndex] - ele[index];
                    }
                    insertionSort(ele, sortedIndex, index + 1);
                    return;
                }
                if (index == ele.length) {
                    sortedIndex++;
                }
                insertionSort(ele, sortedIndex, sortedIndex + 1);
            }
        }
publicstaticvoidinsertionsort(int[]ele,int-sortedIndex,int-index){
if(分拣索引<元件长度){
if(索引<元素长度){
if(ele[sortedIndex]>ele[index]){
ele[sortedIndex]+=ele[index];
ele[index]=ele[sortedIndex]-ele[index];
ele[sortedIndex]=ele[sortedIndex]-ele[index];
}
插入排序(ele,sortedIndex,索引+1);
返回;
}
如果(索引==元素长度){
sortedIndex++;
}
插入排序(ele,sortedIndex,sortedIndex+1);
}
}
public static void insertionSort(int[] ele, int sortedIndex, int index) {
            if (sortedIndex < ele.length) {
                if (index < ele.length) {
                    if (ele[sortedIndex] > ele[index]) {
                        ele[sortedIndex] += ele[index];
                        ele[index] = ele[sortedIndex] - ele[index];
                        ele[sortedIndex] = ele[sortedIndex] - ele[index];
                    }
                    insertionSort(ele, sortedIndex, index + 1);
                    return;
                }
                if (index == ele.length) {
                    sortedIndex++;
                }
                insertionSort(ele, sortedIndex, sortedIndex + 1);
            }
        }