From 73f904097229199670fe75d9e2755c82046773bb Mon Sep 17 00:00:00 2001 From: pec9ut8xf <2584299820@qq.com> Date: Sat, 19 Feb 2022 18:28:52 +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 --- 选择法排序 | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 选择法排序 diff --git a/选择法排序 b/选择法排序 new file mode 100644 index 0000000..480f0b5 --- /dev/null +++ b/选择法排序 @@ -0,0 +1,35 @@ +#include +int main() +{ + int a[100]; + int i,j,k; + int n; + scanf("%d",&n); + for(i = 0;i < n;i++) + { + scanf("%d",&a[i]); + } + for(i = 0;i < n - 1;i++) + { + k = i; + for(j = i + 1;j < n;j++) + { + if(a[j] > a[k]) + k = j; + } + if(i != k) + { + int t = a[i]; + a[i] = a[k]; + a[k] = t; + } + } + for(i = 0;i < n;i++) + { + printf("%d ",a[i]); + } + /** + 时间复杂度: 选择法排序的时间复杂度为O(n2); + 空间复杂度:选择法排序的空间复杂度为S(n); + **/ + } \ No newline at end of file