Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary DSA IN ARRAY

Rating
-
Sold
-
Pages
5
Uploaded on
22-04-2024
Written in
2021/2022

data structure may helps many people in information technology field

Institution
Course

Content preview

Variable Declaration and Memory Allocation
Variable Declaration:

Defining a variable in a program, specifying its data type and name.
Example: int age;
Memory Allocation:

Assigning memory space to a variable during its declaration.
Example: int *ptr = malloc(sizeof(int));
Array Declaration and Initialization:

Defining an array variable and assigning initial values to its elements.
Example: int numbers[5] = {1, 2, 3, 4, 5};
Representation of Arrays in Memory:

Arrays are stored in contiguous memory locations.
Example: int arr[3] = {10, 20, 30};
Variable Declaration and Memory Allocation
Variable Declaration:

Defining a variable in a program, specifying its data type and name.
Example: int age;
Memory Allocation:

Assigning memory space to a variable during its declaration.
Example: int *ptr = malloc(sizeof(int));
Array Declaration and Initialization:

Defining an array variable and assigning initial values to its elements.
Example: int numbers[5] = {1, 2, 3, 4, 5};
Representation of Arrays in Memory:

Arrays are stored in contiguous memory locations.
Example: int arr[3] = {10, 20, 30};
Representation of Arrays in Memory
Arrays are stored in consecutive memory locations
Each element in the array is stored next to each other in memory
The memory address of the first element is used as the base address for the array
Accessing elements in an array is done by calculating the offset from the base
address
Arrays can be multi-dimensional, where each dimension is stored in consecutive
memory locations
Arrays are stored in row-major order in memory for easier access
Arrays
What is an array?
An array is a contiguous area of memory. It consists of equal-sized items (or
elements) indexed by a contiguous integer starting from 0 (or 1). Figure 1 shows
the visual representation of an array consisting of 7 items.

Fig 1: Visual representation of an array
Fig 1: Visual representation of an array

Once we know the address of the starting position of the array, we can calculate
the position of ith
(index) item in constant time as
Ai=A0+i∗s

Where A0
is the address of the starting position of the array (also called address of an

, array or array address) and s
is the size of each item. Suppose an array starts at memory 4000
and has 30 items with size 4
, the location (or address or position) of 22nd item can be calculated as
4000+22∗4=4088
. If we want to get the item in ith
position, we simply calculate its address and read the content of that address.
Even if the array has millions of items, the read (and write) operation takes a
constant time. All we need to do is calculate the position in memory and we are
done.

Operations on array
We can perform read, write, insert and delete operations on an array. If we give a
position i
in the array, read operation gives the item located at position i
, write overwrites the content in the memory located at position i
, insert inserts the new item at position i
and delete removes the item located at position i
. The complexities of all operations are described below.

Read
As described above we can perform the read operation on an array to get the content
of any position from first to the last position. This operation takes constant time
O(1)
.

Write
The write operation also takes constant time O(1)
. Once we know what to write and on what position to write, we can calculate the
memory address of that position and write the content.

Insert
Inserting an item to the end of the array takes only a constant time. We calculate
the position of the last block of the array and write the item. But if we want to
insert the item in the first position, we need to shift all the items one step to
the right and then write the item in the first position. If there are n
items in the array, inserting an item in the first position requires to move all
the items in the one step to the right. If moving one item takes O(1)
time then moving n
items take O(n)
time. In a similar way, if we want to insert an item to the middle of the array,
we need O(n/2)=O(n)
time. Therefore the worst case complexity of insertion is O(n)
.

Delete
The delete operation is similar to insert operation in terms of complexity. To
remove the item in the last position requires constant time. To remove the item in
the first position requires O(n)
time. This is because after we remove the item in the first position, we need to
move all the items one step to the left to fill the gap. The worst case complexity
is, therefore, O(n)
.

Multi-dimensional array
An array can be more than one dimensional. Even though we can have more than 2
dimensions, we only talk about 2-dimensional array here. The 2-dimensional array
has row and column as shown in Figure 2.

Written for

Course

Document information

Uploaded on
April 22, 2024
Number of pages
5
Written in
2021/2022
Type
SUMMARY

Subjects

$17.39
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
vasanthikavi09

Get to know the seller

Seller avatar
vasanthikavi09 anna university
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
2
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions