Search Operation
In an unsorted array, the search operation can be performed by linear traversal from the first
element to the last element.
// Java program to implement linear
// search in unsorted arrays
class Main
{
// Function to implement
// search operation
static int findElement(int arr[], int n,
int key)
{
for (int i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
// Driver Code
public static void main(String args[])
{
int arr[] = {12, 34, 10, 6, 40};
int n = arr.length;
// Using a last element as search element
int key = 40;
int position = findElement(arr, n, key);
if (position == - 1)
System.out.println("Element not found");
else
System.out.println("Element Found at Position: "
+ (position + 1));
}
}
Element Found at Position: 5
Insert at the end
In an unsorted array, the insert operation is faster as compared to sorted array because we don’t
have to care about the position at which the element is to be placed.
// Java program to implement insert
// operation in an unsorted array.
class Main
{
// Function to insert a given key in
// the array. This function returns n+1
// if insertion is successful, else n.
static int insertSorted(int arr[], int n,
int key,
int capacity)
{
// Cannot insert more elements if n
// is already more than or equal to
// capcity
if (n >= capacity)
return n;
arr[n] = key;
return (n + 1);
}
// Driver Code
public static void main (String[] args)
{
int[] arr = new int[20];
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
int capacity = 20;
int n = 6;
int i, key = 26;
System.out.print("Before Insertion: ");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
// Inserting key
n = insertSorted(arr, n, key, capacity);
System.out.print("\n After Insertion: ");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
}
}
Output:
Before Insertion: 12 16 20 40 50 70
After Insertion: 12 16 20 40 50 70 26
Delete Operation
In delete operation, the element to be deleted is searched using the linear search and then delete
operation is performed followed by shifting the elements.
// Java program to implement delete
// operation in an unsorted array
class Main
{
// function to search a key to
// be deleted
static int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
// Function to delete an element
static int deleteElement(int arr[], int n, int key)
{
// Find position of element to be
// deleted
int pos = findElement(arr, n, key);
if (pos == -1)
{
System.out.println("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i< n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
// Driver Code
public static void main(String args[])
{
int i;
int arr[] = {10, 50, 30, 40, 20};
int n = arr.length;
int key = 30;
System.out.println("Array before deletion");
for (i=0; i<n; i++)
System.out.print(arr[i] + " ");
n = deleteElement(arr, n, key);
System.out.println("\n\nArray after deletion");
for (i=0; i<n; i++)
System.out.print(arr[i]+" ");
}
}
Output:
Array before deletion
10 50 30 40 20
Array after deletion
10 50 40 20