-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deletion Operation on Sorted Array.java
52 lines (44 loc) · 1.52 KB
/
Deletion Operation on Sorted Array.java
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
/*
In the delete operation, the element to be deleted is searched using
binary search, and then the delete operation is performed followed by
shifting the elements.
*/
class Main{
//binary search function(Recursive approach)
public static int binary_search(int arr[],int x, int low,int high)
{
if(high<low)
return -1;
int mid = (low+high)/2;
if(x==arr[mid])
return mid;
else if(x>arr[mid])
return binary_search(arr,x,mid+1,high);
return binary_search(arr,x,low,mid-1);
}
public static int delete_ele(int arr[],int n,int x)
{
int position = binary_search(arr,x,0,n-1); //return position of element to be deleted
if(position==-1){
System.out.println("Element not found: ");
return n;
}
for(int i=position;i<n-1;i++)
arr[i] = arr[i+1]; //shifting elements
return n-1; //new array size should be now n-1
}
public static void main(String[] args){
int arr[] = {1,12,34,56,76,89,110};
int n = arr.length;
int x = 12;
//Before Deletion
System.out.print("Before Deletion: ");
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
n = delete_ele(arr,n,x); //calling the delete function
//After Deletion
System.out.print("\nAfter Deletion: ");
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
}
}