-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from diabl0-NEMESIS/patch-2
Recursive_Insertion_Sort
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
|
||
import java.util.Arrays; | ||
|
||
public class DEMONIC | ||
{ | ||
|
||
static void insertionSortRecursive(int arr[], int n) | ||
{ | ||
// Base case | ||
if (n <= 1) | ||
return; | ||
|
||
insertionSortRecursive( arr, n-1 ); | ||
|
||
int last = arr[n-1]; | ||
int j = n-2; | ||
|
||
while (j >= 0 && arr[j] > last) | ||
{ | ||
arr[j+1] = arr[j]; | ||
j--; | ||
} | ||
arr[j+1] = last; | ||
} | ||
|
||
|
||
public static void main(String[] args) | ||
{ | ||
int arr[] = {12, 11, 13, 5, 6}; | ||
|
||
insertionSortRecursive(arr, arr.length); | ||
|
||
System.out.println(Arrays.toString(arr)); | ||
} | ||
} |