Lec 4
Lec 4
Lec 4
1
Last Lecture Summary
Introduction to Algorithms
What is an Algorithm?
Computer Algorithm and Program
Favorite and Efficient Algorithm
Measuring Efficiency
Running Time of an Algorithm
Analyzing an Algorithm with Simple Example
Algorithm History
Algorithm Applications
2
Python Arrays
An array is a collection of items stored at contiguous
memory locations.
The idea is to store multiple items of the same data type
together.
This makes it easier to calculate the position of each
element by simply adding an offset to a base value, i.e.,
the index location of the first element of the array
(generally denoted by the name of the array).
3
Python Arrays
4
Python Arrays
The array can be handled in Python by a module named
array.
They can be useful when we have to manipulate only
specific data type values e.g. integers, float, char
If you create arrays using the array module, all elements of
the array must be of the same type.
A user can treat lists as arrays. However, the user cannot
constrain the type of elements stored in a list.
5
Example (Python Arrays)
# Python program to demonstrate creation of Array
# importing "array" for array creations
import array as arr
6
Memory Allocation
Memory allocation can be classified as either:
o Contiguous
o Linked
Prototypical examples:
o Contiguous allocation: arrays
o Linked allocation: linked lists
7
Memory Allocation
An array stores n objects in a single contiguous space
of memory
Unfortunately, if more memory is required, a request for
new memory usually requires copying all information into
the new memory
In general, you cannot requestr the operating
system to allocate to you the next n memory
locations
8
List (ADT) in Python
Python does not have built-in support for Arrays,
but Python Lists can be used instead.
9
The LIST Data Structure
Real life:
a. shopping list,
b. groceries list,
c. list of people to invite to dinner
d. List of presents to get
10
List
11
List
List is a set of elements in a linear order.
12
Lists in Python
List is a sequence of values called items or
elements.
The elements can be of any data type.
The list is a most versatile data type available in
Python which can be written as a list of comma
separated values (items) between square brackets.
List are mutable, meaning, their elements can be
changed.
13
Lists in Python
In Python programming, a list is created by
placing all the items (elements) inside a square
bracket [ ], separated by commas.
14
Syntax
15
Lists in Python
Index operator [] is used to access an item in a
list. Index starts from 0.
For Example
marks = [90,80,50,70,60]
print(marks[0])
Output: 90
16
Negative Indexing
Python allows negative indexing for its sequences.
my_list = ['p','r','o','b','e']
# Output: e
print(my_list[-1])
#Output: p
print(my_list[-5])
17
Implementation of Lists
pyLists = [1,23,4,33,10]
The above statement will call the list() constructor
to create a list object with the given values
18
Implementation of Lists
Abstract View
1 23 4 33 10
0 1 2 3 4
19
Appending Item to List
pyLists.append(50)
pyLists
1 23 4 33 10 50 . .
0 1 2 3 4 5 6 7
20
Expansion of List (When there isn’t any capacity)
1. A new array is created with the additional capacity
21
Extending a List
A list can be appended to a second list using extend()
method
Example
pyListA = [34,12]
pyListB=[4, 5, 7, 11, 5]
pyListA.extend(pyListB)
22
Inserting Item
pyList.insert(position, value)
pyList.insert(3,79)-- insert 79 at index position 3
23
Example of Append()
list1.append(2)
list1.append(3)
list1.append(4)
24
list1.insert(1,5)
25
Removing an element
Pop(0)---- Remove the First element
26
Example
Output
27
Example
mylist = [1,2,3] mylist.insert (10,709)
print("initial List") print("after inserting 709 at index 10")
print(mylist) print(mylist)
28
Output
29
Operations on Lists
append()
extend()
insert()
sort()
pop()
copy()
30
More Methods
More Methods
• list.count(element) –> returns number of times element occurs
in the list
• list.sort –> sorts the element in place
• list.reverse –> reverses the element in place
Length of lists
len(list) => 7
31
Arrays vs. Lists in Python
Arrays Lists
Need to import Built in support in python
Can only hold same type of Python lists are very
objects flexible and can hold
arbitrary data types.
Can not be resized Can be resized
Since arrays stay the same Lists consume more
size that they were when memory as they are
they were first initialized, allocated a few extra
they are compact. elements to allow for
quicker appending of items.
32
Time Cost
To execute a single statement unit time is required which
is denoted as O(1)-----means constant time
a =10 ------- O(1)
33
Time Cost for different Operations in Lists
Operations Iterations Worst Time
copy Need to traverse the whole list, item by item O(n)
Append Last item is known, so the new item is inserted in just O(1)
a single statement without iteration
34
Motivation for Linked Structures
An array is the most basic sequence container used to store
and access a collection of data
It provides easy and direct access but are limited in their
functionality
Python list is an abstract sequence type implemented using
an array structure
Insertion and deletion requires shifting, so are bit costly
Expansion (in size) is also time consuming
For large arrays, a large contiguous block may not be
available
Lists can grow in size thus require much larger block
memory
35
THANK YOU
36