Week-3 Lists, Searching and Sorting
Week-3 Lists, Searching and Sorting
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 3, Lecture 1
for i in [0,1,2,3,4,5,6,7,8,9]:
for i in range(0,10):
Is range(0,10) == [0,1,2,3,4,5,6,7,8,9]?
In Python2, yes
In Python3, no!
range() and lists
Can convert range() to a list using list()
list(range(0,5)) == [0,1,2,3,4]
str(78) = "78"
int("321") = 321
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 3, Lecture 2
list1 = [1,3,5,6]
list2 = list1
list1[2] = 7
So is list2
Lists
On the other hand
list1 = [1,3,5,6]
list2 = list1
list1 = list1[0:2] + [7] + list1[3:]
list1 = [1,3,5,6]
list2 = list1
list1.append(12)
list1 = [1,3,5,6]
list2 = list1
list1 = list1 + [12]
list1 is an object
y = x + 1 # Error if x is unassigned
l.append(v)
for i in range(1,n+1):
if n%i == 0:
flist.append(i)
return(flist)
Initialising names …
def factors(n):
flist = []
for i in range(1,n+1):
if n%i == 0:
flist.append(i)
return(flist)
Summary
To extend lists in place, use l.append(),
l.extend()
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 3, Lecture 3
while condition:
. . .
def findpos(l,v):
for x in l:
if x == v:
# Exit and report position of x
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 3, Lecture 4
Arrays
Lists
Indexing is fast
Access seq[i] in constant time for any i
Compute offset from start of memory block
Contraction is expensive
Lists
Values scattered in memory
Each element points to the next—“linked” list
Flexible size
Array vs list
Values sorted/unsorted
The unsorted case
def search(seq,v):
for x in seq:
if x == v:
return(True)
return(False)
Worst case
Binary search
Binary search …
def bsearch(seq,v,l,r):
// search for v in seq[l:r], seq is sorted
if r - l == 0:
return(False)
mid = (l + r) // 2 // integer division
if v == seq[mid]:
return (True)
if v < seq[mid]:
return (bsearch(seq,v,l,mid))
else:
return (bsearch(seq,v,mid+1,r))
Binary Search …
How long does this take?
Each step halves the interval to search
For an interval of size 0, the answer is
immediate
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 3, Lecture 5
Polynomial time
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 3, Lecture 6
74 32 89 55 21 64
Strategy 1 …
74 32 89 55 21 64
21
Strategy 1 …
74 32 89 55 21 64
21 32
Strategy 1 …
74 32 89 55 21 64
21 32 55
Strategy 1 …
74 32 89 55 21 64
21 32 55 64
Strategy 1 …
74 32 89 55 21 64
21 32 55 64 74
Strategy 1 …
74 32 89 55 21 64
21 32 55 64 74 89
Strategy 1 …
Selection Sort
Select the next element in sorted order
…
Selection Sort
74 32 89 55 21 64
Selection Sort
74 32 89 55 21 64
Selection Sort
21 32 89 55 74 64
Selection Sort
21 32 89 55 74 64
Selection Sort
21 32 89 55 74 64
Selection Sort
21 32 89 55 74 64
Selection Sort
21 32 55 89 74 64
Selection Sort
21 32 55 89 74 64
Selection Sort
21 32 55 64 74 89
Selection Sort
21 32 55 64 74 89
Selection Sort
21 32 55 64 74 89
Selection Sort
21 32 55 64 74 89
Selection Sort
def SelectionSort(l):
# Scan slices l[0:len(l)], l[1:len(l)], …
for start in range(len(l)):
# Find minimum value in slice . . .
minpos = start
for i in range(start,len(l)):
if l[i] < l[minpos]:
minpos = i
# . . . and move it to start of slice
(l[start],l[minpos]) = (l[minpos],l[start])
Analysis of Selection Sort
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 3, Lecture 7
Second paper:
Third paper
74 32 89 55 21 64
Strategy 2 …
74 32 89 55 21 64
74
Strategy 2 …
74 32 89 55 21 64
32 74
Strategy 2 …
74 32 89 55 21 64
32 74 89
Strategy 2 …
74 32 89 55 21 64
32 55 74 89
Strategy 2 …
74 32 89 55 21 64
21 32 55 74 89
Strategy 2 …
74 32 89 55 21 64
21 32 55 64 74 89
Strategy 2 …
Insertion Sort
Start building a sorted sequence with one element
74 32 89 55 21 64
Insertion Sort
74 32 89 55 21 64
Insertion Sort
32 74 89 55 21 64
Insertion Sort
32 74 89 55 21 64
Insertion Sort
32 74 55 89 21 64
Insertion Sort
32 55 74 89 21 64
Insertion Sort
32 55 74 21 89 64
Insertion Sort
32 55 21 74 89 64
Insertion Sort
32 21 55 74 89 64
Insertion Sort
21 32 55 74 89 64
Insertion Sort
21 32 55 74 64 89
Insertion Sort
21 32 55 64 74 89
Analysis of Insertion Sort
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 3, Lecture 8
Factorial
0! = 1
n! = n x (n-1)!
def factorial(n):
if n == 0:
return(1)
else:
return(n * factorial(n-1))
Recursive computation
def multiply(m,n):
if n == 1:
return(m)
else:
return(m + multiply(m,n-1))
Inductive definitions for lists
Lists can be decomposed as
Length of a list
def length(l):
if l == []:
return(0)
else:
return(1 + length(l[1:])
Inductive definitions for lists
def sumlist(l):
if l == []:
return(0)
else:
return(l[0] + sumlist(l[1:])
Recursive insertion sort
Inductive step:
>>> l = list(range(1000,0,-1))
>>> InsertionSort(l)
. . .
RecursionError: maximum recursion depth
exceeded in comparison
Recurrence
T(n) = n-1 + T(n-1)
T(1) = 1
T(n) = n-1 + T(n-1) = n-1 + ((n-2) + T(n-2)) = … =
(n-1) + (n-2) + … + 1 = n(n-1)/2 = O(n2)
O(n )
2 sorting algorithms
Selection sort and insertion sort are both O(n2)