diff --git a/Sort/Sort.c b/Sort/Sort.c index 29dac03..ca0e6d1 100644 --- a/Sort/Sort.c +++ b/Sort/Sort.c @@ -59,4 +59,20 @@ void quickSort(int arr[], int low, int high) { } } +//直接选择排序 +void selectSort(int arr[], int n) { + for (int i = 0; i < n - 1; i++) { + int min = i; + for (int j = i + 1; j < n; j++) { + if (arr[j] < arr[min]) { + min = j; + } + } + if (min != i) { + int temp = arr[i]; + arr[i] = arr[min]; + arr[min] = temp; + } + } +} diff --git a/main.c b/main.c index a127a3f..1518040 100644 --- a/main.c +++ b/main.c @@ -14,7 +14,7 @@ int main() { int n = sizeof(arr) / sizeof(arr[0]); printf("%d\n",n); - quickSort(arr,0,4); + selectSort(arr,5); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); }