You are on page 1of 12

CS211-Algorithms & Data

Structures
Taibah University

Dr. Sameer M. Alrehaili


College of Science and Computer Engineering, Yanbu

1
Some applications of arrays

2
Arrays

● An array is a collection of elements of the Worst case


same type with a fixed number of
elements. space O(n)
● Arguably the most fundamental data
lookup (get) O(1)
structure
● Most of other data structures built on top append O(1)
of arrays
● Computer memory can be viewed as an insert O(n)
array of bytes (a giant array). delete O(n)
● Used to represent string, stacks and
queues. update O(1)
● Arrays are supported in python by array
iteration O(n)
module
Traverse O(n)

3
Pros and Cons

● One limitation of arrays is that they are fixed size, which means you need to
specify the number of elements , Unless you're using a dynamic array.
● A dynamic array is an array with big improvement
● Strengths:
○ Fast lookups O(1)
○ Append takes O(n)
○ Insert and delete jus like normal array O(n) Delete index 3 Insert into index 3

4
When to choose arrays

● Arrays are homogeneous, so if your data set is inhomogeneous, arrays would


not be the right data structure for your data
● If your data is limited and have a fixed size then, the right choice is arrays.
● If it is not fixed choose dynamic arrays.
● If you want to lookup items by index, then use arrays.
● If your data set consists of set of numbers in 1d or multidimensional to be
computed using numerical operations.

5
Linked Lists

● One disadvantage of using arrays to store Worst case


data is that arrays are static structures and
therefore cannot be easily extended or space O(n)
reduced to fit the dat set.
● Arrays also expensive to maintain new lookup (get) O(n)
insertions or deletions.
add O(n)
● Organises items sequentially, with each
item storing a pointer to the next one. remove O(n)
● Linked list structure:
○ Each element of a linked list is called a update O(n)
node, and every node has two different
fields:
■ Data: contains the value to be
stored in the node.
■ Next: contains a reference to the
next node on the list.
○ The first node is called the head, and it is
used the starting point for any iteration
through th list.
○ The last node must points to None, which
determines the end of the list.
6
Computer memory

0 1 2 3 4

First item index = memory address + (8*0)

10 4 1 3

7
Linked list operations

8
When to choose Linked list

● When you need to store of elements but do not know their total amount in
advance.
● When your application need to often add or remove elements from the
beginning or from the end.

9
Lists

● An ordered series of objects

squares = [1, 4, 9, 16, 25]


squares[0]
squares[-1]
squares[-3:]
squares[:]
● Assigning a value
squares[0] = 2
● Add new item at the end of the list
squares.append(216)
○ Remove some items
squares[2:4] = []
○ Clear the list
squares[:] = []
● Each object has a previous and next. (Except first has no previous, last has
no next)
● We 10
Lists

● Add an item at specified index


○ l.insert(1, “a”)
● Remove a specified item
○ l.remove(“a”)
● Get and remove the last item
○ l.pop()
● Removes specified item
○ del l[0]
○ del l
● Clear
○ l.clear()

11
● Copy
○ N = l.copy()
○ A = list(l)
● Length
○ len(squares)
● Nested list

● [['a', 'b', 'c'], [1, 2, 3]]

12

You might also like