📜  递归插入排序

📅  最后修改于: 2021-05-04 15:18:09             🧑  作者: Mango

插入排序是一种简单的排序算法,其工作方式类似于我们手中对扑克牌的排序。
下面是插入排序的迭代算法
算法

// Sort an arr[] of size n
insertionSort(arr, n) 
    Loop from i = 1 to n-1.
       a) Pick element arr[i] and insert
          it into sorted sequence arr[0..i-1] 

例子:

插入排序

有关更多详细信息,请参见插入排序。
如何递归实现?
递归插入排序没有性能/实现方面的优势,但是对于检查一个人对插入排序和递归的理解可能是一个很好的问题。
如果我们仔细研究插入排序算法,我们将对处理过的元素进行排序,并在插入的数组中一个接一个地插入新元素。
递归的想法。

  1. 基本情况:如果数组大小为1或更小,则返回。
  2. 对前n-1个元素进行递归排序。
  3. 将最后一个元素插入其在已排序数组中的正确位置。

以下是上述想法的实现。

C++
// Recursive C++ program for insertion sort
#include 
using namespace std;
 
// Recursive function to sort an array using
// insertion sort
void insertionSortRecursive(int arr[], int n)
{
    // Base case
    if (n <= 1)
        return;
 
    // Sort first n-1 elements
    insertionSortRecursive( arr, n-1 );
 
    // Insert last element at its correct position
    // in sorted array.
    int last = arr[n-1];
    int j = n-2;
 
    /* Move elements of arr[0..i-1], that are
    greater than key, to one position ahead
    of their current position */
    while (j >= 0 && arr[j] > last)
    {
        arr[j+1] = arr[j];
        j--;
    }
    arr[j+1] = last;
}
 
// A utility function to print an array of size n
void printArray(int arr[], int n)
{
    for (int i=0; i < n; i++)
        cout << arr[i] <<" ";
}
 
/* Driver program to test insertion sort */
int main()
{
    int arr[] = {12, 11, 13, 5, 6};
    int n = sizeof(arr)/sizeof(arr[0]);
 
    insertionSortRecursive(arr, n);
    printArray(arr, n);
 
    return 0;
}


Java
// Recursive Java program for insertion sort
 
import java.util.Arrays;
 
public class GFG
{
    // Recursive function to sort an array using
    // insertion sort
    static void insertionSortRecursive(int arr[], int n)
    {
        // Base case
        if (n <= 1)
            return;
      
        // Sort first n-1 elements
        insertionSortRecursive( arr, n-1 );
      
        // Insert last element at its correct position
        // in sorted array.
        int last = arr[n-1];
        int j = n-2;
      
        /* Move elements of arr[0..i-1], that are
          greater than key, to one position ahead
          of their current position */
        while (j >= 0 && arr[j] > last)
        {
            arr[j+1] = arr[j];
            j--;
        }
        arr[j+1] = last;
    }
     
    // Driver Method
    public static void main(String[] args)
    {
        int arr[] = {12, 11, 13, 5, 6};
      
        insertionSortRecursive(arr, arr.length);
         
        System.out.println(Arrays.toString(arr));
    }
}


Python
# Recursive Python program for insertion sort
# Recursive function to sort an array using insertion sort
 
def insertionSortRecursive(arr,n):
    # base case
    if n<=1:
        return
     
    # Sort first n-1 elements
    insertionSortRecursive(arr,n-1)
    '''Insert last element at its correct position
        in sorted array.'''
    last = arr[n-1]
    j = n-2
     
      # Move elements of arr[0..i-1], that are
      # greater than key, to one position ahead
      # of their current position
    while (j>=0 and arr[j]>last):
        arr[j+1] = arr[j]
        j = j-1
 
    arr[j+1]=last
     
# A utility function to print an array of size n
def printArray(arr,n):
    for i in range(n):
        print arr[i],
 
# Driver program to test insertion sort
arr = [12,11,13,5,6]
n = len(arr)
insertionSortRecursive(arr, n)
printArray(arr, n)
 
# Contributed by Harsh Valecha


C#
// Recursive C# program
// for insertion sort
using System;
 
class GFG
{
 
    // Recursive function to sort
    // an array using insertion sort
    static void insertionSortRecursive(int []arr,
                                       int n)
    {
        // Base case
        if (n <= 1)
            return;
     
        // Sort first n-1 elements
        insertionSortRecursive(arr, n - 1);
     
        // Insert last element at
        // its correct position
        // in sorted array.
        int last = arr[n - 1];
        int j = n - 2;
     
        /* Move elements of arr[0..i-1],
        that are greater than key, to
        one position ahead of their
        current position */
        while (j >= 0 && arr[j] > last)
        {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = last;
    }
     
    //Driver Code
    static void Main()
    {
        int []arr = {12, 11, 13, 5, 6};
     
        insertionSortRecursive(arr, arr.Length);
         
        for(int i = 0; i < arr.Length; i++)
        Console.Write(arr[i] + " ");
    }
}
 
// This code is contributed by Sam007


PHP
= 0 && $arr[$j] > $last)
        {
            $arr[$j + 1] = $arr[$j];
            $j--;
        }
        $arr[$j + 1] = $last;
    }
     
    // A utility function to
    // print an array of size n
    function printArray(&$arr, $n)
    {
        for ($i = 0; $i < $n; $i++)
            echo $arr[$i]." ";
    }
 
// Driver Code
$arr = array(12, 11, 13, 5, 6);
$n = sizeof($arr);
 
insertionSortRecursive($arr, $n);
printArray($arr, $n);
 
// This code is contributed by ChitraNayal.
?>


Javascript


输出 :

5 6 11 12 13