Data Structure and Algorithm Notes
Data Structure and Algorithm Notes
Why do we need more data structures than the programming language provide?
Common operations:
• Accessing a value
• Searching for a value
• Inserting a value
• Deleting a value
Array
Array >>
Contiguous data structure..
Analogy of an array compared with train.
C, Java and Swift >> Arrays are homogenous containers.. Stored same type of values..
When an array is created, since the compiler knows the type of value is homogeneous, it can
choose a contiguous block of memory that fix the array size and values it created. It allocates
contiguous memory of same size.
In python as it is not homogenous it cannot do that. It allocates contiguous block of memory, but
inside that, it stores the reference, not the values.
Array operation
a. Insert. Insert can be divided into 3 sub-categories:
1. Insert --> Linear Runtime. Every index value has to be changed
2. Append --> Constant runtime. Simply end of the last index a new value added
3. Extend --> another list to add (concatenation) --> O(K) .. k is the number of item of 2nd
list
b. Delete --> Linear Runtime. O(n). Every index value has to be shifted left like the opposite
of Insert operation.
In python array data structure is handled by List type. If we see how python's append operation
in details turned out:
• Every time appending list resize is not done.
• The growth pattern of python's list type is: 0, 4, 8, 16, 25, 35, 46 …. which means as list
size approaches this specific values, resize is called again.
• In the append operation some operation don't increase spaces and others do, So in
average all of them out, append operation takes constant space. We say it has an
Ammortized Constant Space Complexity.
Extend: Extend effectively makes a series of append calls, on each of the item in the new list
until all of the item have been appended to the original list.
A Linked list is a linear data structure where each element in the list is contained in a separate
object called Node.
Node contains two information: value and reference/memory address for the next node.
https://digigate360-my.sharepoint.com/:o:/r/personal/sarker_mahmud_technonext_com/_layouts/15/Doc.aspx?sourcedoc=%7B5… 1/2
7/19/24, 3:11 AM OneNote
The first node is called the Head. Last node is called the tail. The list only maintains the
reference to the Head. In some implementations information of Tail are kept as well.
Two Types:
a. Singly Linked List: Each node references next node
b. Doubly Linked List: Each node references both before and after node.
3 types of insert
• Prepend (heads are changing)
• Append (tails are changing)
• Insert
The first two ones are the constant time operation. But inserting new element is linear time
operation.
https://digigate360-my.sharepoint.com/:o:/r/personal/sarker_mahmud_technonext_com/_layouts/15/Doc.aspx?sourcedoc=%7B5… 2/2