
4 câu hỏi
Nếu muốn sắp xếp danh sách theo thứ tự giảm dần thay vì thứ tự tăng dần, ta cần thay đổi câu lệnh so sánh trong vòng lặp của thuật toán sắp xếp. Cụ thể, cần đảo ngược dấu so sánh.
def selection_sort(arr):
for i in range(len(arr) - 1):
min_idx = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
# Đọc dữ liệu từ file kho.inp
with open('kho.inp', 'r') as file:
lines = file.readlines()
quantities = [int(line.strip()) for line in lines]
# Sắp xếp danh sách số lượng các mặt hàng theo thứ tự tăng dần
selection_sort(quantities)
# In danh sách số lượng các mặt hàng đã được sắp xếp ra màn hình
print("Danh sách số lượng các mặt hàng sau khi sắp xếp:")
for quantity in quantities:
print(quantity)
def bubble_sort(arr):
n = len(arr)
for i in range(n - 1):
for j in range(0, n - i - 1):
if arr[j] < arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
# Đọc dữ liệu từ file diem.inp
with open('diem.inp', 'r') as file:
lines = file.readlines()
scores = [float(line.strip()) for line in lines]
# Sắp xếp danh sách điểm trung bình giảm dần
bubble_sort(scores)
# In danh sách điểm trung bình đã được sắp xếp ra màn hình
print("Danh sách điểm trung bình giảm dần:")
for score in scores:
print(score)
# Đọc dữ liệu từ file muahang.inp
with open('muahang.inp', 'r') as file:
lines = file.readlines()
mat_hangs = []
for line in lines:
data = line.strip().split(',')
ten = data[0]
don_gia = float(data[1])
so_luong = int(data[2])
thanh_tien = don_gia * so_luong
mat_hangs.append((ten, thanh_tien))
# Sắp xếp danh sách các mặt hàng theo thứ tự thành tiền tăng dần
n = len(mat_hangs)
for i in range(n - 1):
for j in range(0, n - i - 1):
if mat_hangs[j][1] > mat_hangs[j + 1][1]:
mat_hangs[j], mat_hangs[j + 1] = mat_hangs[j + 1], mat_hangs[j]
# In danh sách các mặt hàng và thành tiền tương ứng ra màn hình
print("Danh sách các mặt hàng và thành tiền tương ứng:")
for mat_hang in mat_hangs:
print("Tên mặt hàng: ", mat_hang[0])
print("Thành tiền: ", mat_hang[1])
print("-----")

![[Năm 2022] Đề thi thử môn Vật lí THPT Quốc gia có lời giải - Đề số 21](/_next/image?url=https%3A%2F%2Fvideo.vietjack.com%2Fupload%2Fimages%2Fvj-image%2Fimage-3izficdefgfoel0s9j8s-1786772791.jpg&w=3840&q=75)
![[Năm 2022] Đề thi thử môn Vật lí THPT Quốc gia có lời giải - Đề số 20](/_next/image?url=https%3A%2F%2Fvideo.vietjack.com%2Fupload%2Fimages%2Fvj-image%2Fimage-emy0gtcksawvorajezvv-1786772460.jpg&w=3840&q=75)




















