【问题标题】:how to perform insertion sort recursively如何递归地执行插入排序
【发布时间】:2014-04-03 00:03:33
【问题描述】:

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

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;
    }
}

目前我的输出是,

 {20, 50, 50, 50, 60, 40}

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

【问题讨论】:

    标签: java sorting recursion insertion-sort


    【解决方案1】:

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

    运行这个版本并尝试看看你做错了什么。

    试试这个版本:

    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]);
        }
    }
    

    来源: http://analgorithmaday.blogspot.com/2011/01/insertion-sortrecursive-method.html

    【讨论】:

      【解决方案2】:

      通过提供 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);
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-17
        • 1970-01-01
        • 1970-01-01
        • 2012-02-29
        • 2018-07-17
        相关资源
        最近更新 更多