From 3a247035d5748f2c0dc6fc7fd5dff077d71f955f Mon Sep 17 00:00:00 2001 From: pbjgvwc2r <1487290298@qq.com> Date: Sat, 19 Feb 2022 16:02:57 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=E6=8F=92=E5=85=A5=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 插入排序 | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 插入排序 diff --git a/插入排序 b/插入排序 new file mode 100644 index 0000000..93bd98a --- /dev/null +++ b/插入排序 @@ -0,0 +1,34 @@ +//插入排序 +//复杂度:O(n^2) 运行时间:170ms +#include +void InsertSort(int arr[], int length) +{ + for (int i = 1; i < length; i++) + { + int j; + if (arr[i] < arr[i - 1]) + { + int temp = arr[i]; + for (j = i - 1; j >= 0 && temp < arr[j]; j--) + { + arr[j + 1] = arr[j]; + } + arr[j + 1] = temp; + } + } +} + +int main() +{ + int n; + scanf("%d",&n); + int arr[n]; + for(int i=0;i