1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package insr.java;
import java.util.Arrays;
public class insr {
// Recursive function to sort an array using
// insertion sort
static void insertionSortRecursive(int arr[], int n)
{
// Base case
if (n <= 1)
return;
print("passage ligne 13 n=", n);
// Sort first n-1 elements
insertionSortRecursive( arr, n-1 );
// Insert last element at its correct position
// in sorted array.
print("passage ligne 13 n=", n);
int last = arr[n-1];
int j = n-2;
// 12, 11, 13, 5, 6
/* 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]; // 12, 11, 13, 5, 6
j--;
}
arr[j+1] = last;
}
// 12, 11, 13, 5, 6
// 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));
}
private static void print(String passage_ligne_8_n, int n) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
} |
Partager