University of Science, VNU – HCM
Faculty of Physics and Engineering
Department of Physics and Computer Science
Python
Programming
MSc. Vo Hoang Thuy Tien
Tuples, Lists
Week 5
3
Tuples, Lists
Exercise 1. Write a Pseudocode and draw a flow chart to find the smallest
element in a list. The list should contain 10 random elements selected from
the range 1 to 100.
Step 1: Start
Step 2: Import random library
Step 3: Initialize
Step 3.1: List number equal random value in range
Step 3.2: Suppose the smallest number is the first element in the list
Step 3.3: Get the length of list number
Step 3.4: Initialize temp variable
Step 4: If temp is large than the length of list number, then goto step 5 else goto step 7
Step 5: If min > list_number [temp], then goto step 5.1, 5.2 else goto 5.2
Step 5.1: min = list_number [temp]
Step 5.2: Increase temp by 1 unit, goto step 4
Step 6: Print min
Step 7: Stop 4
Tuples, Lists
Exercise 2. Sorts a randomly initialized list using the selection sort method.
Step 1: Start
Step 2: Receive the list to be sorted
Step 3: Get the length of the list
Step 4: Iterate through each element in the list (variable index from 0 to length -1)
Step 4.1: Assume the smallest element is arr[index], set min_index = index
Step 4.2: Iterate through the remaining part of the list (sub_index from index+1 to lenght-1)
Step 4.2.1: If arr[sub_index ] < arr[min_index], update min_index = sub_index
Step 4.3: If min_index has changed, swap arr[index] with arr[min_index]
Step 5: Repeat until the list is fully sorted
Step 6: Print the sorted list
Step 7: Stop
7
THANK YOU!
MSc. Vo Hoang Thuy Tien
Lecturer and Researcher
+84 937 64 99 14
vhttien@[Link]
University of Science, VNU – HCM
227 Nguyen Van Cu, Ward 4, District 5, Ho Chi Minh City