Skip to content

Commit 34a4689

Browse files
authored
Merge pull request #490 from PrasoonSingh28/RecInsSort
Recursive Insertion Sort
2 parents 84c246f + 1bcb69a commit 34a4689

File tree

1 file changed

+29
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)