How to Search, Insert, and Delete in an Unsorted Array

This post discusses a code that performs search, insert, and delete actions on an unsorted array.

Search Operation:

With an unsorted array, the search operation can be accomplished by doing a linear traversal from the first to the final element.

How to Search, Insert, and Delete in an Unsorted Array

Programming execution of the search operation:

C Programming Language:

Output

Element Found at Position: 4

C++ Program:

Output

Element Found at Position: 4

Java:

Output

Element Found at Position: 4
  • Time Complexity will be O(N).
  • Auxiliary Space will be O(1).

Insert Operation:

1. Insert at the end:

In an unsorted array, the insert operation is quicker than in a sorted array as we do not need to worry about the position at which the element is to be inserted.

How to Search, Insert, and Delete in an Unsorted Array

Programming execution of the insertion operation:

C Programming Language:

Output

Before Insertion: 22 26 30 50 60 80 
 After Insertion: 22 26 30 50 60 80 34

C++ Program:

Output

Before Insertion: 22 26 30 50 60 80 
 After Insertion: 22 26 30 50 60 80 34

Java:

Output

Before Insertion: 22 26 30 50 60 80 
 After Insertion: 22 26 30 50 60 80 34
  • Time Complexity will be O(1).
  • Auxiliary Space will be O(1).

2. Insert at any point

Insert operations in an array can be performed at any point by moving elements to the right that are on the right side of the desired position.

How to Search, Insert, and Delete in an Unsorted Array

Programming execution of the insertion operation:

C Programming Language:

Output

Before insertion : 1 3 5 7 4 
After insertion : 1 3 5 9 7 4

C++ Program:

Output

Before insertion : 1 3 5 7 4 
After insertion : 1 3 5 9 7 4

Java:

Output

Before insertion : 1 3 5 7 4 
After insertion : 1 3 5 9 7 4
  • Time complexity will be O(N).
  • Auxiliary Space will be O(1).

Delete Operation:

The element to be deleted is found using a linear search, and then the delete operation is executed, followed by relocating the elements.

How to Search, Insert, and Delete in an Unsorted Array

Programming execution of the deletion operation:

C Programming Language:

Output

Array before deletion
20 60 40 50 30 
Array after deletion
20 60 50 30

C++ Program:

Output

Array before deletion
20 60 40 50 30 
Array after deletion
20 60 50 30

Java:

Output

Array before deletion
20 60 40 50 30 
Array after deletion
20 60 50 30
  • Time Complexity will be O(N).
  • Auxiliary Space will be O(1).