Array Operations | Deletion from Array | Explanation
with Code | Data Structure
Jenny's Lectures CS IT
In this lecture, we will discuss the deletion operation in arrays. We will use an example array
of size 5 to explain the process of deleting data from a specific position, beginning, and end
of the array. We will also write the code and analyze the time complexity of the operation.
First, we initialize an array of size 50 but ask the user for the number of elements they want
to insert. We then populate the array with the user's input and ask them which position they
want to delete data from. For example, if they choose position 2 (index 1), we cannot leave
that space blank. Instead, we shift the elements to fill the empty space and decrease the
size of the array by 1.
We start a loop to shift the elements to the left, starting from the index before the position
chosen by the user until the second last index of the array. We shift the element at index i+1
to index i and continue until the end of the loop, overwriting the deleted element. If we want
to print the deleted data, we store it in a separate variable before shifting the elements.
We provide the code for the deletion operation, where we ask the user for the position to
delete, check if it's a valid position, and then start the loop to shift the elements.
int size = 5; int arr[size]; int pos, i; int item; // ask
user for position to delete printf("Enter position to delete: ");
scanf("%d", &pos); // check if valid position if (pos <= 0 ||
pos > size) { printf("Invalid position"); } else if (pos ==
size) { printf("No data at position %d", pos); } else { //
start loop to shift elements for (i = pos-1; i < size-1; i++) {
arr[i] = arr[i+1]; } size--; // decrease size of array }
When deleting data from an array, shifting the elements is necessary to maintain the order of
the array. To delete data from a specific position, the value at that position is shifted to the
left to fill the gap. The array's size is also decremented by one after shifting. If deleting from
the end of the array, no shifting is necessary. If deleting from the beginning, all values are
shifted to the left, and the updated array starts from index 0.
, When declaring an array, the size cannot be changed. Therefore, it is essential to check the
boundaries of the array before entering data. If the size entered is greater than the maximum
size allocated for the array, an "out of bounds" message should be displayed.
The time complexity of deleting data from an array depends on the position of the data being
deleted. If deleting from a specific position, the time complexity is θ(n) because all elements
after the deleted element must be shifted to the left. If deleting from the end, the time
complexity is θ(1) because no shifting is necessary. If deleting from the beginning, the time
complexity is θ(n-1) because all elements must be shifted to the left.
If the array is unsorted, the quickest algorithm for deleting data is to replace the deleted
element with the last element of the array. This method takes constant time (θ(1)). However,
if the array is sorted, this method will disturb the order of the elements, and the shifting
method must be used.
Question / Answer
How does the deletion operation work?
The deletion operation involves shifting values in the array and reducing the size of the array
by 1.
What happens if the user wants to delete data from a specific position?
The values from the position to be deleted to the end of the array are shifted to the left, and
the size of the array is decreased by 1.
What should be done before deleting data from the array?
It's important to check the validity of the position entered by the user. If the position is invalid
or greater than the size of the array, it's considered an invalid position.
with Code | Data Structure
Jenny's Lectures CS IT
In this lecture, we will discuss the deletion operation in arrays. We will use an example array
of size 5 to explain the process of deleting data from a specific position, beginning, and end
of the array. We will also write the code and analyze the time complexity of the operation.
First, we initialize an array of size 50 but ask the user for the number of elements they want
to insert. We then populate the array with the user's input and ask them which position they
want to delete data from. For example, if they choose position 2 (index 1), we cannot leave
that space blank. Instead, we shift the elements to fill the empty space and decrease the
size of the array by 1.
We start a loop to shift the elements to the left, starting from the index before the position
chosen by the user until the second last index of the array. We shift the element at index i+1
to index i and continue until the end of the loop, overwriting the deleted element. If we want
to print the deleted data, we store it in a separate variable before shifting the elements.
We provide the code for the deletion operation, where we ask the user for the position to
delete, check if it's a valid position, and then start the loop to shift the elements.
int size = 5; int arr[size]; int pos, i; int item; // ask
user for position to delete printf("Enter position to delete: ");
scanf("%d", &pos); // check if valid position if (pos <= 0 ||
pos > size) { printf("Invalid position"); } else if (pos ==
size) { printf("No data at position %d", pos); } else { //
start loop to shift elements for (i = pos-1; i < size-1; i++) {
arr[i] = arr[i+1]; } size--; // decrease size of array }
When deleting data from an array, shifting the elements is necessary to maintain the order of
the array. To delete data from a specific position, the value at that position is shifted to the
left to fill the gap. The array's size is also decremented by one after shifting. If deleting from
the end of the array, no shifting is necessary. If deleting from the beginning, all values are
shifted to the left, and the updated array starts from index 0.
, When declaring an array, the size cannot be changed. Therefore, it is essential to check the
boundaries of the array before entering data. If the size entered is greater than the maximum
size allocated for the array, an "out of bounds" message should be displayed.
The time complexity of deleting data from an array depends on the position of the data being
deleted. If deleting from a specific position, the time complexity is θ(n) because all elements
after the deleted element must be shifted to the left. If deleting from the end, the time
complexity is θ(1) because no shifting is necessary. If deleting from the beginning, the time
complexity is θ(n-1) because all elements must be shifted to the left.
If the array is unsorted, the quickest algorithm for deleting data is to replace the deleted
element with the last element of the array. This method takes constant time (θ(1)). However,
if the array is sorted, this method will disturb the order of the elements, and the shifting
method must be used.
Question / Answer
How does the deletion operation work?
The deletion operation involves shifting values in the array and reducing the size of the array
by 1.
What happens if the user wants to delete data from a specific position?
The values from the position to be deleted to the end of the array are shifted to the left, and
the size of the array is decreased by 1.
What should be done before deleting data from the array?
It's important to check the validity of the position entered by the user. If the position is invalid
or greater than the size of the array, it's considered an invalid position.