From 9170256621e3aaf10d6851483e07937bc826daf1 Mon Sep 17 00:00:00 2001 From: pmkfqw2gu <2511089200@qq.com> Date: Thu, 17 Feb 2022 21:04:52 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E5=86=92=E6=B3=A1=E6=B3=95.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 冒泡法.cpp | 55 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/冒泡法.cpp b/冒泡法.cpp index 55f43b4..254254a 100644 --- a/冒泡法.cpp +++ b/冒泡法.cpp @@ -1,16 +1,39 @@ -void bubble(int *a,int n) -{ - int i,j,temp; - for(i=0;ia[j]) - { - temp=a[i]; - a[i]=a[j]; - a[j]=temp; - } - } - } - } +1.冒泡排序 + +1.1 描述 +冒泡排序(英语:Bubble Sort)是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素, +如果他们的顺序(如从大到小、首字母从A到Z)错误就把他们交换过来。 + +1.2 复杂程度 +时间复杂度O(n^2) +空间复杂度O(1) +空间复杂度O(n^2) + +1.3 代码 +#include +void bubble_sort(int arr[], int len) +{ + int i, j, temp; + for (i = 0; i < len - 1; i++) + for (j = 0; j < len - 1 - i; j++) + if (arr[j] > arr[j + 1]) + { + temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } +} +int main() +{ + int arr[] = { 22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70 }; + int len = (int) sizeof(arr) / sizeof(*arr); + bubble_sort(arr, len); + int i; + for (i = 0; i < len; i++) + printf("%d ", arr[i]); + return 0; +} + + 1.4 运行时间 + 0.01884s +