-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion Operation on Sorted Array.java
More file actions
42 lines (39 loc) · 1.12 KB
/
Insertion Operation on Sorted Array.java
File metadata and controls
42 lines (39 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
/* INSERTION OPERATION ON SORTED ARRAY
In a sorted array, a search operation is performed for the possible position of
the given element by using Binary search, and then an insert operation is
performed followed by shifting the elements.
*/
class Main{
public static int insert_ele(int arr[],int n,int x,int size)
{
if(n>size)
return n;
int i;
for(i=n-1;(i>=0 && arr[i]>x);i--)
arr[i+1] = arr[i];
arr[i+1] = x;
return n+1;
}
public static void main(String[] args){
int size = 25;
int n = 7;
int arr[] = new int[size];
int x = 34;
arr[0] = 1;
arr[1] = 12;
arr[2] = 26;
arr[3] = 45;
arr[4] = 56;
arr[5] = 69;
arr[6] = 92;
//Before Insertion
System.out.print("Before Insertion: ");
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
n = insert_ele(arr,n,x,size);
//After Insertion
System.out.print("\nAfter Insertion: ");
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
}
}