Skip to content

Commit fb1893e

Browse files
authored
Merge pull request #16 from diabl0-NEMESIS/patch-2
Recursive_Insertion_Sort
2 parents 01b036b + 0be0a38 commit fb1893e

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

JAVA/Recursive_Insertion_Sort.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
3+
import java.util.Arrays;
4+
5+
public class DEMONIC
6+
{
7+
8+
static void insertionSortRecursive(int arr[], int n)
9+
{
10+
// Base case
11+
if (n <= 1)
12+
return;
13+
14+
insertionSortRecursive( arr, n-1 );
15+
16+
int last = arr[n-1];
17+
int j = n-2;
18+
19+
while (j >= 0 && arr[j] > last)
20+
{
21+
arr[j+1] = arr[j];
22+
j--;
23+
}
24+
arr[j+1] = last;
25+
}
26+
27+
28+
public static void main(String[] args)
29+
{
30+
int arr[] = {12, 11, 13, 5, 6};
31+
32+
insertionSortRecursive(arr, arr.length);
33+
34+
System.out.println(Arrays.toString(arr));
35+
}
36+
}

0 commit comments

Comments
 (0)