-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrInsertionSort.java
More file actions
58 lines (41 loc) · 1.12 KB
/
rInsertionSort.java
File metadata and controls
58 lines (41 loc) · 1.12 KB
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
53
54
55
56
57
58
import java.util.Random;
public class rInsertionSort {
public static void main(String[]arg){
System.out.println("1st Array Sorted (non-random): ");
int[] arr1 = {8,5,4,2,7,10,19,12,5,1};
RecursiveInsertionSort(arr1, 0, 9);
for(int i = 0; i < arr1.length ; i++){
System.out.println(arr1[i]);
}
System.out.println("2nd Array Sorted (random): ");
int[] arr2 = new int[30];
for (int i = 0; i < arr2.length; i++){
arr2[i] = (int) (Math.random() * 100);
}
int first = 0;
int last = arr2.length - 1;
RecursiveInsertionSort(arr2, first, last);
for (int i = 0; i < arr2.length; i++){
System.out.println(arr2[i]);
}
}
public static void insert(int element, int[] a, int first, int last){
if (element >= a[last]){
a[last+1] = element;
}
else if (first < last) {
a[last+1] = a[last];
insert(element, a, first, last-1);
}
else {
a[last+1] = a[last];
a[last] = element;
}
}
public static void rInsertionSort(int[] arr, int first, int last){
if (first < last){
rInsertionSort(arr, first, last-1);
insert(arr[last], arr, first, last-1);
}
}
}