moea_joint_essay
113年
[統計資訊] 資料庫及資料探勘、程式設計
第 五 題
請實作下列函式以完成設計 1 個插入排序法(Insertion Sort),據以依參數值決定排序方式採遞增或遞減。(18 分)
bool isInverse(int x, int y, bool isAsc); //判斷傳入的 x、y 是否反序
void InsertionSort(int *arr, int len, bool isAsc); //插入排序
(註:參數 arr 為傳入的整數陣列;參數 len 為整數陣列的長度;參數 isAsc 為是否遞增,函式 InsertionSort 應呼叫函式 isInverse。)
bool isInverse(int x, int y, bool isAsc); //判斷傳入的 x、y 是否反序
void InsertionSort(int *arr, int len, bool isAsc); //插入排序
(註:參數 arr 為傳入的整數陣列;參數 len 為整數陣列的長度;參數 isAsc 為是否遞增,函式 InsertionSort 應呼叫函式 isInverse。)
📝 此題為申論題
思路引導 VIP
實作 isInverse 根據 isAsc 判斷是否發生順序錯置。若遞增(isAsc=true),則 x > y 視為反序;若遞減,則 x < y 視為反序。接著實作經典的插入排序法,在內層 while 迴圈使用 isInverse 來判定是否需要進行元素平移。