阿摩線上測驗 登入

申論題資訊

試卷:110年 - 110-2 國立清華大學期末考試題_電機工程學系:計算機程式設計#112945
科目:程式設計
年份:110年
排序:0

題組內容

1. (20%) Answer the following questions briefly.

申論題內容

(c) Give a reason why selection sort could be more efficient than bubble sort for large arrays of big elements? (Note that a big element means an element with many data, for example, a student record containing a lot of information about a student). (5%)

詳解 (共 1 筆)

詳解 提供者:hchungw

Selection sort could be more efficient than bubble sort for large arrays of big elements due to the number of swaps performed during the sorting process.

In bubble sort, a swap is performed every time a comparison determines that two elements are in the wrong order. Since a swap of big elements (which contain a lot of data) can be costly in terms of memory operations, bubble sort can become less efficient because it potentially makes many such swaps throughout its sorting process.

On the other hand, selection sort performs the minimum number of swaps: it looks for the smallest (or largest, depending on the sorting order) element in each pass and makes exactly one swap at the end of each pass — it swaps the smallest found element with the element at the current position. So, even though selection sort may perform the same number of comparisons as bubble sort, it minimizes the number of swaps. When the elements are large and each swap is expensive, the reduced number of swaps can lead to better performance in terms of time complexity for selection sort compared to bubble sort.

選擇排序對於包含大型元素的大數組而言,可能比冒泡排序更高效,原因在於排序過程中執行的交換操作次數。
在冒泡排序中,每次比較發現兩個元素順序錯誤時就會執行一次交換。由於交換大型元素(包含大量數據)在內存操作上可能代價較高,冒泡排序可能會變得效率較低,因為在整個排序過程中它可能會執行許多此類交換。
另一方面,選擇排序執行的交換次數最少:它在每次遍曆中查找最小(或最大,根據排序順序而定)的元素,並在每次遍曆結束時執行確切一次交換 —— 它將找到的最小元素與當前位置的元素交換。因此,即使選擇排序可能執行與冒泡排序相同數量的比較,它也最小化了交換次數。當元素很大且每次交換的代價昂貴時,減少的交換次數可以在時間複雜度上為選擇排序帶來比冒泡排序更好的性能。