0% found this document useful (0 votes)
3 views

COMP1002 - Algorithms (P2)

The document discusses loops and nested loops in programming and provides examples of for loops to iterate over ranges and multidimensional data like 2D arrays. It also covers bubble sort and indexing into multidimensional data.

Uploaded by

louis425llw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

COMP1002 - Algorithms (P2)

The document discusses loops and nested loops in programming and provides examples of for loops to iterate over ranges and multidimensional data like 2D arrays. It also covers bubble sort and indexing into multidimensional data.

Uploaded by

louis425llw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

COMP1002 –

Algorithms (Part 2)

Lec6
Muhammad Tayyab
Loops
for i in range(4):
print("A")

output:
A
A
A
A
Loops
for i in range(4):
print(i)

output:
0
1
2
3
Nested Loop
for j in range(3):
for i in range(4):
print("A")
output:
A
A
A
A
A
A
A
A
A
A
A
A
Nested Loop
for j in range(3):
for i in range(4):
print(i)
output:
0
1
j = 0
2
3
0
1
j = 1
2
3
0
1
j = 2
2
3
Nested Loop
for j in range(3):
for i in range(4):
print(j)
output:
_
_
j = 0
_
_
_
_
j = 1
_
_
_
_
j = 2
_
_
Nested Loop
for j in range(3):
for i in range(4):
print(j)
output:
0
0
j = 0
0
0
1
1
j = 1
1
1
2
2
j = 2
2
2
Nested Loop
for j in range(3):
for i in range(4):
print(j)
output:
0
0
j = 0
0
0
1
1
j = 1
1
1
2
2
j = 2
2
2
Nested Loop
for j in range(3):
for i in range(4):
print(i,j)
output:
0 0
0 1
j = 0
0 2
0 3
1 0
1 1
j = 1
1 2
1 3
2 0
2 1
j = 2
2 2
2 3
Nested Loop
for j in range(3):
for i in range(4):
print("A")
print("B")
output:
A
B
A
B
A
B
A
B
A
B
...
Nested Loop
for j in range(3):
for i in range(4):
print("A")
print("B")
output:
A
B
A
B
A
B
A
B
A
B
...
Nested Loop
for i in range(3):
print("A")
for j in range(4):
print("B")
print("C")
print("D")
print("E")
Nested Loop
for i in range(3):
print(i)
for j in range(4):
print(i)
print(j)
print(j)
print(j)
Quiz (Graded)
• Use BOTH your NAME and STUDENT ID
• Double check before making any mistakes
• There is different time limit for each question
• You need to be correct not fast
• If you are having any problem or errors raise you hand immediately
Sort
Bubble Sort
Bubble sort
Bubble sort
Bubble sort
Bubble sort
Bubble sort
nums=[6,0,3,5]
for j in range(4):
a,b=nums[j],nums[j+1]
if a>b:
nums[j],nums[j+1] = nums[j+1],nums[j]
Bubble sort
nums=[6,0,3,5]
for j in range(3):
a,b=nums[j],nums[j+1]
if a>b:
nums[j],nums[j+1] = nums[j+1],nums[j]
Bubble sort
Bubble Sort
Bubble Sort
nums=[6,0,3,5]

for j in range(3):
a,b=nums[j],nums[j+1]
if a>b:
nums[j],nums[j+1] = nums[j+1],nums[j]
Bubble Sort
nums=[6,0,3,5]
for i in range(3):
for j in range(3):
a,b=nums[j],nums[j+1]
if a>b:
nums[j],nums[j+1] = nums[j+1],nums[j]
Bubble sort
Bubble Sort

for i in range(3):
for j in range(3):
print(i,j)
Bubble Sort
Nested Loop
20 20
19 19
18 18
for i in range(3): 17
16 16
17

15 15
for j in range(i): 14 14
13 13
print(i,j) 12
11 11
12

10 10
9 9
8 8
7 7
6 6
5 5
4 4
3 3
2 2
1 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Bubble Sort
Nested Loop
20
19
18

for i in range(3): 17
16
15
for j in range(i): 14
13

print(i,j) 12
11
10
9
8
7
6
5
4
3
2
1
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Bubble Sort
Nested Loop
20
19
18

for i in range(3): 17
16
15
for j in range(3-i): 14
13

print(i,j) 12
11
10
9
8
7
6
5
4
3
2
1
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Multidimensional data

• Scalar
• Vector (1 dimensional)
• Multidimensional (2D, 3D, 4D …)
1D data
1D data

ex1 = [1,2,3,4,5,6]
for i in range(len(ex1)):
print(ex1[i])

import math
sine_wave=[]
for i in range(10):
sine_wave.append(math.sin(i))
2D Data
2D data

ex2=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]

ex2=[
1 2 3 4
[1,2,3,4],
[5,6,7,8],
[9,10,11,12] 5 6 7 8
]
9 10 11 12
2D data

ex2=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]

ex2=[
0 1 2 3
[1,2,3,4],
[5,6,7,8],
[9,10,11,12] 0 1 2 3 4
]
1 5 6 7 8

2 9 10 11 12
2D data

ex2=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]

ex2=[
0 1 2 3
[1,2,3,4],
[5,6,7,8],
0 1 2 3 4
[9,10,11,12] 0,0 0,1 0,2 0,3
]
1 5 6 7 8
1,0 1,1 1,2 1,3

2 9 10 11 12
2,0 2,1 2,2 2,3
2D Indexing

• 0,0
• 0,1
• 0,2
for i in range(3):
• 0,3
• 1,0 for j in range(4):
• 1,1 print(i,j)
• 1,2
• 1,3
• 2,0
• 2,1
• 2,2
• 2,3
2D Indexing

• 0,0
• 0,1
• 0,2
for row in range(3):
• 0,3
• 1,0 for col in range(4):
• 1,1 print(row,col)
• 1,2
• 1,3
• 2,0
• 2,1
• 2,2
• 2,3
2D Indexing

ex2=[
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]

for row in range(3):


for col in range(4):
print(row,col)
2D Indexing

ex2=[
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]

for row in range(3):


for col in range(4):
print(ex2[row][col])
Examples 2D data
nums1=[[1,2,3],[4,5,6],[7,8,9]]
nums2=[[10,11,12],[13,14,15],[16,17,18]]

for row in range(len(nums1)):


for col in range(len(nums1)):
print(nums1[row][col]+nums2[row][col])
1D and 2D

nums = 10
nums = [102, -10, 80, 53, -21, -5, 90]
nums = [[5, -2], [20, 0], [-5, 5]]
nums = [[[6, 9], [-10, 2]], [[-5, 20], [0, 4]]]

You might also like