From 9d70ddef9725e996b26a72a3803feb8b80c89e42 Mon Sep 17 00:00:00 2001 From: lwy Date: Fri, 14 Apr 2023 00:14:36 +0800 Subject: [PATCH] update Sort.c --- Sort/Sort.c | 16 ++++++++++++++++ main.c | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) 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]); }