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

Array

a. Reading from a regular array or array-based set is a constant-time operation that takes approximately 1 step regardless of size. b. Searching for a non-existent value in a regular array or array-based set of 100 elements would require checking each element, taking around 100 steps. c. Inserting at the beginning of a regular array or array-based set of 100 elements requires shifting all existing elements, taking approximately 100 steps.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Array

a. Reading from a regular array or array-based set is a constant-time operation that takes approximately 1 step regardless of size. b. Searching for a non-existent value in a regular array or array-based set of 100 elements would require checking each element, taking around 100 steps. c. Inserting at the beginning of a regular array or array-based set of 100 elements requires shifting all existing elements, taking approximately 100 steps.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

For an regular array containing 100 elements, provide the

number of steps the following operations would take

a. Reading -
Answer: Approximately 1 step, because reading is a
straightforward process, a computer can read from an array in
just one step.

b. Searching for a value not contained within the array -


Answer: Approximately 100 steps, because If there are 100
elements and the value we’re seeking happens to be in final
cell in the array, searching needs to check each one once, so it
would take around 100 steps in total.

c. Insertion at the beginning of the array -


Answer: Approximately 100+1 steps, because we need to make
room for the new element at the beginning, we need to shift or
move all existing 100 elements to the right by one position,
which would take 100 steps and once the shifting is done, you
can insert the new element at the beginning. This step takes 1
additional step.

d. Insertion at the end of the array -


Answer:
e. Deletion at the beginning of the array
f. Deletion at the end of the array

For an array-based set containing 100 elements, provide the


number of steps the following operations would take

a. Reading
b. Searching for a value not contained within the array
c. Insertion of a new value at the beginning of the array
d. Insertion of a new value at the end of the array
e. Deletion at the beginning of the array
f. Deletion at the end of the array

ANSWER:

For an Array Containing 100 Elements:


a. Reading: Approximately 1 step, because reading is a
straightforward process, a computer can read from an array in
just one step.

a. . b. Searching for a value not contained within the array:


Approximately 100 steps (linear-time operation).
b.
c. c. Insertion at the beginning of the array: Approximately 100
steps (linear-time operation). d. Insertion at the end of the
array: Approximately 1 step (constant-time operation,
assuming no resizing). e. Deletion at the beginning of the array:
Approximately 100 steps (linear-time operation). f. Deletion at
the end of the array: Approximately 1 step (constant-time
operation).
For an Array-Based Set Containing 100 Elements:
a. Reading: Approximately 1 step (constant-time operation). b.
Searching for a value not contained within the array:
Approximately 100 steps (linear-time operation). c. Insertion of
a new value at the beginning of the array: Approximately 100
steps (linear-time operation). d. Insertion of a new value at the
end of the array: Approximately 100 steps for checking
duplicates + 1 step for insertion (linear-time operation for
checking duplicates). e. Deletion at the beginning of the array:
Approximately 100 steps (linear-time operation). f. Deletion at
the end of the array: Approximately 1 step (constant-time
operation).
Please note that these estimates are based on common
algorithms and data structures. The actual number of steps can
vary based on factors such as hardware, language
optimizations, and specific implementations.

a. Reading: When you read an element from an array-based set, it's


like looking up a book page by its number – very quick and takes the
same amount of time no matter how many pages (elements) the
book (array-based set) has. So, for a set with 100 elements, reading
one element would take a single step.

b. Searching for a value not contained within the array: If you're


trying to find something that isn't in the set, it's like checking every
item in your room for a missing toy. If there are 100 things, you might
need to check each one once, taking around 100 steps in total.
c. Insertion at the beginning of the array: Imagine you're adding a
new shirt to a packed closet – you need to make space for it by
moving all the other clothes. Similarly, adding something new to the
start of the set with 100 elements means you might need to shift
everything, which takes around 100 steps.

d. Insertion at the end of the array: If there's room at the end, adding
an item there is quick – like placing a new book at the end of a shelf.
Assuming there's space, it takes just one step.

e. Deletion at the beginning of the array: Removing something from


the start is like taking the first book off a shelf – all the other books
need to be shifted to fill the gap. For a set with 100 elements, this
would take around 100 steps.

f. Deletion at the end of the array: Taking the last item out of the set
is easy – like removing the last piece of candy from a bag. This
operation generally takes just one step.

Remember, these explanations are simplified and based on common


concepts. The actual time might vary based on programming details
and other factors.

Reading:

 Regular Array: Reading from a regular array is a constant-time


operation (O(1)), regardless of the size of the array. You access
elements using their indices directly.
 Array-based Set: Reading from an array-based set is also a constant-
time operation (O(1)), just like a regular array. Both structures allow
direct access to elements by index.

b. Searching for a value not contained within the array:

 Regular Array: If the array is unsorted, searching for a value in a


regular array requires checking each element in the worst case,
resulting in a linear time complexity (O(n)) for an array with n
elements.
 Array-based Set: Searching for a value not in an array-based set is
similar to searching in a regular array, so it also has a linear time
complexity (O(n)) in the worst case.

c. Insertion of a new value at the beginning of the array:

 Regular Array: Insertion at the beginning of a regular array requires


shifting all existing elements to the right, resulting in a linear time
complexity (O(n)) for an array with n elements.
 Array-based Set: Similarly, inserting at the beginning of an array-
based set involves shifting elements and has a linear time complexity
(O(n)).

d. Insertion of a new value at the end of the array:

 Regular Array: If there's space at the end of a regular array, adding an


element there is a constant-time operation (O(1)). Resizing the array
could introduce complexity.
 Array-based Set: Inserting a value at the end of an array-based set
requires checking for duplicates and then potentially resizing the
underlying array. The worst-case time complexity for checking
duplicates is linear (O(n)).

e. Deletion at the beginning of the array:

 Regular Array: Removing an element from the beginning of a regular


array requires shifting all remaining elements, resulting in a linear
time complexity (O(n)).
 Array-based Set: Deleting from the beginning of an array-based set is
also a linear-time operation (O(n)) due to element shifting.

f. Deletion at the end of the array:

 Regular Array: Deleting an element from the end of a regular array is


a constant-time operation (O(1)), as it only involves updating the
length.
 Array-based Set: Deleting from the end of an array-based set is also a
constant-time operation (O(1)), similar to a regular array.
In summary, the main difference between a regular array and an
array-based set lies in the consideration of unique values in the set.
Array-based sets require additional operations to ensure uniqueness,
which can impact the time complexity of certain operations
compared to a regular array.

You might also like