Array
Array
// Driver's Code
int main()
{
int arr[] = { 12, 34, 10, 6, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
int position = findElement(arr, n, key);
if (position == -1)
printf("Element not found");
else
printf("Element Found at Position: %d",
position + 1);
return 0;
}
Insert Operation:
1. Insert at the end:
In an unsorted array, the insert operation is faster as compared to
a sorted array because we don’t have to care about the position at
which the element is to be placed.
arr[n] = key;
return (n + 1);
}
// Driver Code
int main()
{
int arr[20] = { 12, 16, 20, 40, 50, 70 };
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;
printf("\n Before Insertion: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
// Inserting key
n = insertSorted(arr, n, key, capacity);
return 0;
}
#include <stdio.h>
arr[pos] = x;
}
// Driver's code
int main()
{
int arr[15] = { 2, 4, 1, 8, 5 };
int n = 5;
printf("\n");
// Function call
insertElement(arr, n, x, pos);
n++;
return 0;
}
Delete Operation:
In the delete operation, the element to be deleted is searched
using the linear search, and then the delete operation is performed
followed by shifting the elements.
if (pos == -1) {
printf("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
return -1;
}
// Driver's code
int main()
{
int i;
int arr[] = { 10, 50, 30, 40, 20 };
// Function call
n = deleteElement(arr, n, key);
return 0;
}
Time Complexity: O(N)
Auxiliary Space: O(1)