羅德興老師的教學歷程檔案 - 113-2 程式語言二(2025) - 單元六 (sorting)
 

企業資訊與管理系
助理教授/日導
羅德興


歷程檔案 Portfolio

    單元六 (sorting)






    # -*- coding: utf-8 -*-
    """
    sort0.ipynb
    這支程式 由 學號 xxxx000 羅老師 所撰寫 on 2025/05/04
    功能:從 Google 雲端硬碟中 myfile 資料夾讀取 myfile9.txt,
    (假設內容為:5, 17, 9, 10, 2, 6)
          進行氣泡排序後,寫出 test9.csv 到相同資料夾中
    """

    from google.colab import drive
    import numpy as np
    import csv
    import os

    # 掛載 Google 雲端硬碟
    drive.mount('/content/drive')

    # 設定 myfile 資料夾的路徑
    base_dir = '/content/drive/MyDrive/myfile'

    # 設定輸入與輸出檔案的完整路徑
    input_path = os.path.join(base_dir, 'myfile9.txt')
    output_path = os.path.join(base_dir, 'test9.csv')

    # 讀取檔案
    with open(input_path, 'r') as file:
        line = file.readline().strip()
        elements = line.split(',')

    # 去除空白並轉為 numpy 陣列
    elements = [e.strip() for e in elements]
    a = np.array(elements)

    # 排序函式
    def bubble_sort(b, n):
        print("排序中...")
        for i in range(n - 1):
            for j in range(n - i - 1):
                print(f"第 {i} 輪 第 {j} 次 比較\t", end=" ")
                if int(b[j]) > int(b[j + 1]):
                    b[j], b[j + 1] = b[j + 1], b[j]
                print_array(b)

    # 印出陣列
    def print_array(arr):
        print(' '.join(arr))

    print("原始陣列:")
    print_array(a)

    bubble_sort(a, len(a))

    print("排序後的陣列:")
    print_array(a)

    # 將排序結果寫入 CSV
    with open(output_path, 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(a)

    print(f"排序結果已寫入:{output_path}")
    全部共 15則留言
    黃品婷05-11 09:39:https://colab.research.google.com/drive/156VBVlT6HDZWffQ1h9k1NGBSdzUslxVf?usp=drive_link
    黃品婷05-11 09:43:https://colab.research.google.com/drive/156VBVlT6HDZWffQ1h9k1NGBSdzUslxVf?usp=drive_link
    馬秉昆05-11 09:55:https://colab.research.google.com/drive/19k1g_fAYvlK7ItQNOqa0vdbEwQqgWokQ?usp=sharing
    05-18 09:22:單元六 作業 已 O.K. 者:09, 10
    高民宏06-01 09:36:https://colab.research.google.com/drive/1EoFeOPx7aiqqYs_B644KvQYIu0tADvli
    蔡潔旻06-01 11:50:https://colab.research.google.com/drive/1wlNFg_VrbY6mbfDlyq3lUSt3ewCR_0Fo?usp=sharing
    胡媁婷06-01 12:04:https://colab.research.google.com/drive/1c29BiqixOdR3F1KmixdHR_T4P_nWYrAu?usp=sharing
    蘇琳媙06-01 12:17:https://colab.research.google.com/drive/1MB_K3eMC0LNX3m-lyPUn52rV63CvEsuU?usp=sharing
    石孟玄06-07 16:01:https://colab.research.google.com/drive/1beyqnl7qdtX5X2HrmHa_ZsEroHJrYF7u?usp=sharing
    石孟玄06-07 16:23:https://colab.research.google.com/drive/1E7UW1UFezDueXahuTtfP4VNWh6cieUbz?usp=sharing
    06-08 08:39:單元七 O.K. 者:09, 10, 15, 11114D014(^)
    06-08 08:50:更正: 單元六 O.K. 者:09, 10, 15, 11114D014(^)
    胡媁婷06-08 09:28: https://colab.research.google.com/drive/1c29BiqixOdR3F1KmixdHR_T4P_nWYrAu?usp=sharing
    蘇琳媙06-08 09:34: https://colab.research.google.com/drive/1MB_K3eMC0LNX3m-lyPUn52rV63CvEsuU?usp=sharing
    06-14 23:46: 單元六 O.K. 者:4, 09, 10, 15, 11114D014(^);待修改者:5
    登入帳號密碼代表遵守學術網路規範


    文章分類 Labels


    最新文章 Top10

    中華科技大學數位化學習歷程 - 意見反應