From ec09688d23d8dbeb768f0db0a97909d3c828fc99 Mon Sep 17 00:00:00 2001 From: pn43tqvrm <2516333959@qq.com> Date: Sat, 19 Feb 2022 16:09:24 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=E9=80=89=E6=8B=A9=E6=B3=95=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 选择法排序 | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 选择法排序 diff --git a/选择法排序 b/选择法排序 new file mode 100644 index 0000000..145798d --- /dev/null +++ b/选择法排序 @@ -0,0 +1,21 @@ +//选择法排序 +void Sort(int a[], int n) +{ + int i, j, k, temp; + for(i = 0; i < n - 1; i ++) + { + k = i; + for(j = i + 1; j < n; j ++) + { + if(a[j] < a[i])//a[j] > a[i]是升序, a[j] < a[i]是降序 + k = j //记录最小(大)数 下标位置 + } + } + if(k != i) + { + trmp = a[k]; + a[k] = a[i]; + a[i] = temp; + } + } + //复杂度说明:从代码中可以看出一共遍历了n + n-1 + n-2 + … + 2 + 1 = n * (n+1) / 2 = 0.5 * n ^ 2 + 0.5 * n,那么时间复杂度是O(N^2)。