0% found this document useful (0 votes)
4 views

Array

Uploaded by

sahilkatiyar860
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Array

Uploaded by

sahilkatiyar860
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

What is an Array?

An array is a collection of items of same data type stored at


contiguous memory locations.

For simplicity, we can think of an array as a flight of stairs where on


each step is placed a value (let’s say one of your friends). Here, you
can identify the location of any of your friends by simply knowing
the count of the step they are on.
This makes it easier to calculate the position of each element by
simply adding an offset to a base value, i.e., the memory location of
the first element of the array (generally denoted by the name of the
array). The base value is index 0 and the difference between the two
indexes is the offset.
Remember: “Location of next index depends on the data type we
use”.

Is the array always of a fixed size?


In C language, the array has a fixed size meaning once the size is
given to it, it cannot be changed i.e. you can’t shrink it nor can you
expand it. The reason was that for expanding if we change the size
we can’t be sure ( it’s not possible every time) that we get the next
memory location to us for free. The shrinking will not work because
the array, when declared, gets memory statically allocated, and thus
compiler is the only one that can destroy it.
Search, Insert, and Delete in an
Unsorted Array | Array Operations
Search Operation:
In an unsorted array, the search operation can be performed by
linear traversal from the first element to the last element.

Coding implementation of the search operation:


// C program to implement linear
// search in unsorted array
#include <stdio.h>

// Function to implement search operation


int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;

// If the key is not found


return -1;
}

// Driver's Code
int main()
{
int arr[] = { 12, 34, 10, 6, 40 };
int n = sizeof(arr) / sizeof(arr[0]);

// Using a last element as search element


int key = 40;

// 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.

Coding implementation of inserting an element at the


end:
// C program to implement insert
// operation in an unsorted array.
#include <stdio.h>

// Inserts a key in arr[] of given capacity.


// n is current size of arr[]. This
// function returns n + 1 if insertion
// is successful, else n.
int insertSorted(int arr[], int n, int key, int capacity)
{

// Cannot insert more elements if n is


// already more than or equal to capacity
if (n >= capacity)
return n;

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);

printf("\n After Insertion: ");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

return 0;
}

2. Insert at any position


Insert operation in an array at any position can be performed by
shifting elements to the right, which are on the right side of the
required position

// C Program to Insert an element


// at a specific position in an Array

#include <stdio.h>

// Function to insert element


// at a specific position
void insertElement(int arr[], int n, int x, int pos)
{
// shift elements to the right
// which are on the right side of pos
for (int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];

arr[pos] = x;
}

// Driver's code
int main()
{
int arr[15] = { 2, 4, 1, 8, 5 };
int n = 5;

printf("Before insertion : ");


for (int i = 0; i < n; i++)
printf("%d ", arr[i]);

printf("\n");

int x = 10, pos = 2;

// Function call
insertElement(arr, n, x, pos);
n++;

printf("After insertion : ");


for (int i = 0; i < n; i++)
printf("%d ", arr[i]);

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.

// C program to implement delete operation in a


// unsorted array
#include <stdio.h>

// To search a key to be deleted


int findElement(int arr[], int n, int key);
// Function to delete an element
int deleteElement(int arr[], int n, int key)
{
// Find position of element to be deleted
int pos = findElement(arr, n, key);

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;
}

// Function to implement search operation


int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;

return -1;
}

// Driver's code
int main()
{
int i;
int arr[] = { 10, 50, 30, 40, 20 };

int n = sizeof(arr) / sizeof(arr[0]);


int key = 30;

printf("Array before deletion\n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

// Function call
n = deleteElement(arr, n, key);

printf("\nArray after deletion\n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

return 0;
}
Time Complexity: O(N)
Auxiliary Space: O(1)

You might also like