Describe how a new integer could be inserted into a sequence of sorted integers (so that the resulting sequence is also sorted) in each of the following cases: i. The sequence is stored in an array. ii. The sequence is stored in a linked list.
Q.) Describe how a new integer could be inserted into a sequence of sorted integers (so that the resulting sequence is also sorted) in each of the following cases: i. The sequence is stored in an array. ii. The sequence is stored in a linked list.
Subject: Data StructuresInserting an Integer into a Sorted Sequence
Case i: The Sequence is Stored in an Array
Inserting a new integer into a sorted sequence that is stored in an array involves the following steps:
Find the Insertion Point:
- Start from the beginning of the array and compare the new integer with each element until you find the first element that is greater than the integer to be inserted.
- Let's denote the index of this element as
insertionIndex
.
Shift Elements:
- If the array has enough space to accommodate the new integer, shift all elements from
insertionIndex
to the end of the array one position to the right to make space for the new integer. - This step is necessary because arrays have a fixed size and inserting an element without shifting would overwrite an existing element.
- If the array has enough space to accommodate the new integer, shift all elements from
Insert the New Integer:
- Place the new integer into the
insertionIndex
.
- Place the new integer into the
Handle Array Size:
- If the array is full, you may need to create a new array with a larger size and copy all elements including the new integer into it.
Here's a step-by-step example:
Suppose we have an array A
with the sorted sequence [1, 3, 5, 7]
and we want to insert the integer 4
.
Find the Insertion Point:
- Compare
4
with each element:4
is greater than1
and3
, but less than5
. insertionIndex = 2
(since arrays are zero-indexed).
- Compare
Shift Elements:
- Shift elements at and after index
2
:[1, 3, (5), 5, 7]
.
- Shift elements at and after index
Insert the New Integer:
- Insert
4
at index2
:[1, 3, 4, 5, 7]
.
- Insert
Handle Array Size:
- If the array was initially
[1, 3, 5, 7, _]
(with an empty space), no resizing is needed. - If the array was
[1, 3, 5, 7]
(no empty space), create a new array[1, 3, 4, 5, 7, _]
.
- If the array was initially
Case ii: The Sequence is Stored in a Linked List
Inserting a new integer into a sorted sequence that is stored in a linked list involves the following steps:
Find the Insertion Point:
- Traverse the linked list from the head and compare the new integer with the value of each node until you find the first node that has a value greater than the integer to be inserted.
- Let's denote this node as
insertionNode
and its predecessor aspreviousNode
.
Create a New Node:
- Allocate memory for a new node and set its value to the new integer.
Insert the New Node:
- Adjust the pointers such that
previousNode
points to the new node and the new node points toinsertionNode
.
- Adjust the pointers such that
Here's a step-by-step example:
Suppose we have a linked list with the sorted sequence 1 -> 3 -> 5 -> 7
and we want to insert the integer 4
.
Find the Insertion Point:
- Traverse the list:
4
is greater than1
and3
, but less than5
. previousNode
is the node containing3
, andinsertionNode
is the node containing5
.
- Traverse the list:
Create a New Node:
- Create a new node
newNode
with the value4
.
- Create a new node
Insert the New Node:
- Adjust pointers:
previousNode.next
points tonewNode
, andnewNode.next
points toinsertionNode
. - The linked list now looks like:
1 -> 3 -> 4 -> 5 -> 7
.
- Adjust pointers:
Comparison Table
Aspect | Array-Based Insertion | Linked List-Based Insertion |
---|---|---|
Find Insertion Point | O(n) time complexity | O(n) time complexity |
Space for New Element | May require resizing | Only requires new node |
Shift Elements | O(n) time complexity | Not required |
Insert New Element | O(1) time complexity | O(1) time complexity |
Overall Time Complexity | O(n) (due to shifting) | O(n) (due to traversal) |
Space Complexity | O(1) or O(n) if resize needed | O(1) |
In summary, inserting an integer into a sorted sequence stored in an array requires finding the insertion point, potentially shifting elements, and handling array size, which can lead to O(n) time complexity. In contrast, inserting into a sorted linked list also requires finding the insertion point, but the insertion itself is more straightforward with no need to shift elements, also resulting in O(n) time complexity. However, linked lists offer easier dynamic memory management compared to arrays.