-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion.java
More file actions
34 lines (33 loc) · 1 KB
/
Copy pathinsertion.java
File metadata and controls
34 lines (33 loc) · 1 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
package sorting;
import java.util.Arrays;
import java.util.Collections;
public class insertion {
public static int[] insert(int arr[]){
for (int i = 1; i < arr.length; i++) {
int curr=arr[i];
int prev=i-1;
//finding the correct position to insert
while(prev>=0 && arr[prev]>curr){
arr[prev+1]=arr[prev]; //pushing the elements that are greater than current
prev--;
}
//insertion
arr[prev+1]=curr; //+1 in the prev array because it shifts one index to the right
}
return arr;
}
public static void main(String[] args) {
int arr[]={12,78,65,2,9};
//insert(arr);
Arrays.sort(arr);
for(int i:arr){
System.out.print(i+" ");
}
System.out.println();
Integer carr[]={12,78,65,2,9};
Arrays.sort(carr,Collections.reverseOrder());
for(int i:carr){
System.out.print(i+" ");
}
}
}