1.Bubble sort 修改說明: 將原置於程式中的Bubble sort另寫為一個函數,並將陣列索引i和j改為count和swap,陣列大小n也改為SIZE leetcode結果 過了10個測資後就錯誤了,會發生TLE是因為冒泡排序效率較低。 ![bubble_sort](https://hackmd.io/_uploads/Skc3HXMBp.png) ``` #include <stdio.h> #define SIZE 10 // 函數:冒泡排序(Bubble Sort) // 參數:array - 待排序的整數陣列 void bubblesort(int array[]) { // 外層循環控制排序過程中的輪數 for (int count = 0; count < SIZE - 1; count++) { // 內層循環進行相鄰元素的比較和交換 for (int swap = 0; swap < SIZE - 1 - count; swap++) { // 如果前一個元素大於後一個元素,則進行交換 if (array[swap] > array[swap + 1]) { // 使用臨時變數 temp 進行交換 int temp = array[swap]; array[swap] = array[swap + 1]; array[swap + 1] = temp; } } } } int main(void) { // 初始化整數陣列 int arr[SIZE] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37}; // 輸出原始陣列的數據 puts("原始陣列數據"); for (size_t count_one = 0; count_one < SIZE; ++count_one) { printf("%4d", arr[count_one]); } // 使用冒泡排序對陣列進行升序排序 bubblesort(arr); // 輸出排序後的陣列數據 puts("\n升序排序後的陣列數據"); for (size_t count_two = 0; count_two < SIZE; ++count_two) { printf("%4d", arr[count_two]); } // 換行 puts(""); return 0; } ``` 2.selection sort 修改說明: 將swap函式裡的變數*xp和*yp改成*num_one和*num_two,selection函式裡的i和j改成count_one和count_two leetcode結果 selection sort會發成TLE的原因也是因為效率較低 ![selection_sort](https://hackmd.io/_uploads/SJsKdQfS6.png) ``` #include <stdio.h> // 函式:交換兩個整數的值 // 參數:num_one, num_two - 要交換的兩個整數 void swap(int *num_one, int *num_two) { int swap_data = *num_one; *num_one = *num_two; *num_two = swap_data; } // 函式:選擇排序(Selection Sort) // 參數:arr - 待排序的整數陣列 // numsSIZE - 陣列的大小 void selectionSort(int arr[], int numsSIZE) { int count_one, count_two, min_idx; // 逐步擴大未排序子陣列的邊界 for (count_one = 0; count_one < numsSIZE-1; count_one++) { // 在未排序陣列中找到最小的元素 min_idx = count_one; for (count_two = count_one+1; count_two < numsSIZE; count_two++) { if (arr[count_two] < arr[min_idx]) { min_idx = count_two; } } // 將找到的最小元素與未排序子陣列的第一個元素交換位置 swap(&arr[min_idx], &arr[count_one]); } } /* 函式:列印陣列 */ // 參數:arr - 要列印的整數陣列 // size - 陣列的大小 void printArray(int arr[], int size) { int count_one; for (count_one = 0; count_one < size; count_one++) printf("%d ", arr[count_one]); printf("\n"); } // 主程式:測試選擇排序 int main() { // 初始化一個整數陣列 int arr[] = {64, 25, 12, 22, 11}; int numsSIZE = sizeof(arr) / sizeof(arr[0]); // 使用選擇排序對陣列進行排序 selectionSort(arr, numsSIZE); // 列印排序後的陣列 printf("排序後的陣列: \n"); printArray(arr, numsSIZE); return 0; } ``` 3.insertion sort 修改說明: 將insertion函式裡的i,j改成count_one和swap,將陣列大小n改為numsSIZE leetcode結果 會發生TLE的原因是Insertion Sort 的時間複雜度也是 O(n^2),其中 n 是待排序數組的大小 ![insertion sort](https://hackmd.io/_uploads/BJOBcmGrT.png) ``` #include <math.h> #include <stdio.h> // 函數:插入排序(Insertion Sort) // 參數:arr - 待排序的整數陣列 // numsSIZE - 陣列的大小 void insertionSort(int arr[], int numsSIZE) { int count_one, key, swap; // 遍歷整個陣列,從索引1開始(第一個元素視為已排序部分) for (count_one = 1; count_one < numsSIZE; count_one++) { // 將當前元素儲存在 key 中,並設定 swap 為當前元素的前一個索引 key = arr[count_one]; swap = count_one - 1; // 在已排序的部分中,將大於 key 的元素往右移動 // 直到找到 key 的正確位置 while (swap >= 0 && arr[swap] > key) { arr[swap + 1] = arr[swap]; swap = swap - 1; } // 將 key 插入到正確的位置 arr[swap + 1] = key; } } // 函數:輸出整數陣列 // 參數:arr - 待輸出的整數陣列 // numsSIZE - 陣列的大小 void printArray(int arr[], int numsSIZE) { int count_one; for (count_one = 0; count_one < numsSIZE; count_one++) printf("%d ", arr[count_one]); printf("\n"); } // 主函數:插入排序的測試 int main() { // 初始化一個整數陣列 int arr[] = {12, 11, 13, 5, 6}; // 計算陣列的大小 int numsSIZE = sizeof(arr) / sizeof(arr[0]); // 使用插入排序對陣列進行排序 insertionSort(arr, numsSIZE); // 輸出排序後的陣列 printArray(arr, numsSIZE); return 0; } ``` 4.quick sort 修改說明: 將partition函式裡的i和j改成first_num和count_one,陣列大小n改成numsSIZE leetcode結果 雖然還是TLE,但在顯示的結果裡都是已經排序好了 ![quick sort](https://hackmd.io/_uploads/ry0DJEMSa.png) ``` #include <stdio.h> // 函數:交換兩個元素的值 void swap(int* num_one, int* num_two) { int swap_data = *num_one; *num_one = *num_two; *num_two = swap_data; } // 函數:分割陣列並回傳軸心的索引 int partition(int arr[], int low, int high) { int pivot = arr[high]; // 軸心 int first_num = (low - 1); // 標記較小元素的索引,同時指示目前找到的軸心的正確位置 // 遍歷區間 [low, high-1] for (int count_one = low; count_one <= high - 1; count_one++) { // 如果目前的元素小於軸心 if (arr[count_one] < pivot) { first_num++; // 增加較小元素的索引 swap(&arr[first_num], &arr[count_one]); // 交換元素,將較小元素放到左邊 } } swap(&arr[first_num + 1], &arr[high]); // 將軸心放到正確位置 return (first_num + 1); // 回傳軸心的索引 } // 函數:實現快速排序 void quickSort(int arr[], int low, int high) { if (low < high) { // 尋找軸心的索引,並將陣列分割成兩部分 int first_num = partition(arr, low, high); // 遞迴排序左右子陣列 quickSort(arr, low, first_num - 1); quickSort(arr, first_num + 1, high); } } // 函數:列印陣列 void printArray(int arr[], int numsSIZE) { int first_num; for (first_num = 0; first_num < numsSIZE; first_num++) printf("%d ", arr[first_num]); printf("\n"); } // 主程式 int main() { int arr[] = {10, 7, 8, 9, 1, 5}; int numsSIZE = sizeof(arr) / sizeof(arr[0]); // 呼叫快速排序函數 quickSort(arr, 0, numsSIZE - 1); // 列印排序後的陣列 printArray(arr, numsSIZE); return 0; } ```