profile picture

Anonymous

upvote

0

downvote

0

star

Sử dụng thuật toán sắp xếp chọn viết lại chương trình trong Nhiệm vụ 1

clock icon

- asked 6 months agoVotes

message

0Answers

eye

0Views

Giải Tin học 11 Bài 22: Thực hành bài toán sắp xếp

Luyện tập 1 trang 105 Tin học 11: Sử dụng thuật toán sắp xếp chọn viết lại chương trình trong Nhiệm vụ 1.

Lời giải:

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)

Bài tập liên quan

Write your answer here

© 2025 Pitomath. All rights reserved.