Java递归插入排序?

Java递归插入排序?,java,sorting,recursion,insertion-sort,Java,Sorting,Recursion,Insertion Sort,因此,我试图将下面的代码转换为递归方法,插入排序,但尽管我尽了最大努力,我还是做不到。有人能帮我吗 public static void insertionSort(int[] array){ for (int i = 1; i < array.length; i++){ int j = i; int B = array[i]; while ((j > 0) && (array[j-1] > B)){

因此,我试图将下面的代码转换为递归方法,插入排序,但尽管我尽了最大努力,我还是做不到。有人能帮我吗

public static void insertionSort(int[] array){
    for (int i = 1; i < array.length; i++){
        int j = i;
        int B = array[i];
        while ((j > 0) && (array[j-1] > B)){
            array[j] = array[j-1];
            j--;
        }
        array[j] = B;
    }
}
公共静态void insertionSort(int[]数组){
for(int i=1;i0)&(数组[j-1]>B)){
数组[j]=数组[j-1];
j--;
}
数组[j]=B;
}
}
编辑: 我在想这样的事情,但对我来说,它看起来不是很递归

public static void insertionSort(int[] array, int index){
    if(index < array.length){
        int j = index;
        int B = array[index];
        while ((j > 0) && (array[j-1] > B)){
            array[j] = array[j-1];
            j--;
        }
        array[j] = B;
        insertionSort(array, index + 1);
    }
}
公共静态void insertionSort(int[]数组,int索引){
if(索引<数组长度){
int j=指数;
int B=数组[索引];
而((j>0)&(数组[j-1]>B)){
数组[j]=数组[j-1];
j--;
}
数组[j]=B;
insertionSort(数组,索引+1);
}
}

对于递归算法,我们从整个数组A[1..n]开始,对A[1..n-1]进行排序,然后将A[n]插入正确的位置

public int[] insertionSort(int[] array)
{
   //base case
   if(array.length==1) return new int[]{ array[0] };

   //get array[0..n-1] and sort it
   int[] arrayToSort = new int[array.length - 1]{ };
   System.arraycopy(array, 0, arrayToSort, 0, array.length -1);
   int[] B = insertionSort(arrayToSort);

   //now, insert array[n] into its correct position
   int[] C = merge(B, array[array.length - 1]);

   return C;
}

private int[] merge(int[] array, int n)
{ 
   int[] arrayToReturn = new int[array.length + 1] {};
   int j = array.length-1;
   while(j>=0 && n <= array[j])
   {
      arrayToReturn[j+1]=array[j;
      j--;
   }

   arrayToReturn[j] = 
}
public int[]插入排序(int[]数组)
{
//基本情况
if(array.length==1)返回新的int[]{array[0]};
//获取数组[0..n-1]并对其排序
int[]arrayToSort=新的int[array.length-1]{};
System.arraycopy(数组,0,arrayToSort,0,array.length-1);
int[]B=插入排序(arrayToSort);
//现在,将数组[n]插入其正确位置
int[]C=merge(B,数组[array.length-1]);
返回C;
}
私有int[]合并(int[]数组,int n)
{ 
int[]arrayToReturn=new int[array.length+1]{};
int j=array.length-1;

当(j>=0&&n时,您可以尝试此代码。它工作正常

public static int[] InsertionSort(int[] dizi, int n) 
{
    int i;
    if (n < 1) {
        InsertionSort(dizi, n - 1);
    } 
    else {

        int key = dizi[n];
        i = n - 1;

        while (i >= 0 && dizi[i] > key) {
            dizi[i + 1] = dizi[i];
            i = i - 1;
        }

        dizi[i + 1] = key;
    }

    return dizi;
}
公共静态int[]插入排序(int[]dizi,int n)
{
int i;
if(n<1){
插入排序(dizi,n-1);
} 
否则{
int key=dizi[n];
i=n-1;
while(i>=0&&dizi[i]>键){
笛子[i+1]=笛子[i];
i=i-1;
}
dizi[i+1]=键;
}
返回笛子;
}
试试这个:

public class RecursiveInsertionSort {
    static int[] arr = {5, 2, 4, 6, 1, 3};
    int maxIndex = arr.length;

    public static void main(String[] args) {
        print(arr);
        new RecursiveInsertionSort().sort(arr.length);
    }

    /*
      The sorting function uses 'index' instead of 'copying the array' in each    
      recursive call to conserve memory and improve efficiency.
    */
    private int sort(int maxIndex) {
        if (maxIndex <= 1) {
            // at this point maxIndex points to the second element in the array.
            return maxIndex;
        }

        maxIndex = sort(maxIndex - 1);  // recursive call

        // save a copy of the value in variable 'key'.
        // This value will be placed in the correct position
        // after the while loop below ends.
        int key = arr[maxIndex];  

        int i = maxIndex - 1;

        // compare value in 'key' with all the elements in array 
        // that come before the element key. 
        while ((i >= 0) && (arr[i] > key)) {
            arr[i+1] = arr[i];
            i--;
        }
        arr[i+1] = key;
        print(arr);
        return maxIndex + 1;
    }

    // code to print the array on the console.
    private static void print(int[] arr) {
        System.out.println();
        for (int i : arr) {
            System.out.print(i + ", ");
        }
    }
}
公共类递归插入排序{
静态int[]arr={5,2,4,6,1,3};
int maxIndex=arr.length;
公共静态void main(字符串[]args){
打印(arr);
新的递归插入排序().sort(arr.length);
}
/*
排序函数使用“索引”而不是“复制数组”
递归调用以节省内存并提高效率。
*/
私有整数排序(整数最大索引){
if(maxIndex=0)&(arr[i]>key)){
arr[i+1]=arr[i];
我--;
}
arr[i+1]=键;
打印(arr);
返回maxIndex+1;
}
//在控制台上打印阵列的代码。
专用静态无效打印(int[]arr){
System.out.println();
用于(int i:arr){
系统输出打印(i+“,”);
}
}
}

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

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);
}
}
公共类测试
{
公共静态void main(字符串[]args){
测试h=新测试();
INTA[]={5,8,9,13,65,74,25,44,67,2,1};
h、 ins_rec(a,a.长度-1);
对于(int i=0;i
公共静态无效排序(int[]A,int p,int r){
if(p=0&&valSystem.out.print(i+“,”));
}

第1步:使用可读格式您需要更加具体。您希望递归做什么?我希望该方法是递归的。在方法内部调用自身,然后有一个基本条件阻止它永远调用自身。“尽管我尝试了,但我无法”嗯,我曾考虑将for循环取出,并通过使用不同的内部数据再次调用该方法使其递归,但当我尝试这样做时,我感到困惑。缺少答案的描述。请添加描述和注释以解释您的答案。@Varun:欢迎这样做,尝试对您的答案添加一些解释,并提供一些注释代码中的内容也将有助于其他人理解/快速改进它。很抱歉……我希望这有帮助,如果需要更多,请告诉我。它实际上无法正确工作。当您回答时,最好给出解释。
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);
    }
}
public class test
{
   public static void main(String[] args){
        test h = new test();

        int a[] = { 5, 8, 9, 13, 65, 74, 25, 44, 67, 2, 1 };

        h.ins_rec(a, a.length-1);

        for(int i=0; i<a.length; i++)
          log(a[i]);

    }



    void ins_rec(int a[], int j){
    if( j == 0 ) return;

    ins_rec(a, j - 1);
    int key = a[ j ];
    sort(a, key, j - 1);

    }



    void sort(int a[], int key, int i){
    if( (i < 0) || (a[i] < key) ) {
      a[ i + 1 ] = key;
      return;
    }

    a[ i + 1 ] = a[ i ];
    sort(a, key, i - 1);

    }



private static void log(int aMessage){
    System.out.println("\t\t"+aMessage);
  }
}
public static void sort(int[] A, int p, int r) {
    if (p < r) {
        int q = r - 1;
        sort(A, p, q);
        combine(A, p, q, r);
    }
}

private static void combine(int[] A, int p, int q, int r) {
    int i = q - p;
    int val = A[r];
    while (i >= 0 && val < A[p + i]) {
        A[p + i + 1] = A[p + i];
        A[p + i] = val;
        i--;
    }
}

public static void main(String... strings) {
    int[] A = { 2, 5, 3, 1, 7 };
    sort(A, 0, A.length - 1);
    Arrays.stream(A).sequential().forEach(i -> System.out.print(i + ", "));
}