From 7a268f44d234cc3e8c68de5d36771aacf5cf394b Mon Sep 17 00:00:00 2001 From: lisen Date: Tue, 26 Feb 2019 21:12:26 +0800 Subject: [PATCH 01/13] Update 1.bubbleSort.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加c语言实现代码 --- 1.bubbleSort.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index 78c3b83..84c11c0 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -129,3 +129,20 @@ function bubbleSort($arr) return $arr; } ``` +## 10. C 代码实现 +```c +void BubbleSort1(int data[],int n) //n为data长度 +{ + int t; + for(int i=0;i data[j+1]) + { + t = data[j+1]; + data[j+1] = data[j]; + data[j] = t; + } + } + } +} +``` From 4d09d57c02960e3cd1710a6ab245f1c1763e9d05 Mon Sep 17 00:00:00 2001 From: lisen Date: Tue, 26 Feb 2019 21:13:54 +0800 Subject: [PATCH 02/13] Update 1.bubbleSort.md --- 1.bubbleSort.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index 84c11c0..b93ad36 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -133,16 +133,16 @@ function bubbleSort($arr) ```c void BubbleSort1(int data[],int n) //n为data长度 { - int t; - for(int i=0;i data[j+1]) - { - t = data[j+1]; - data[j+1] = data[j]; - data[j] = t; - } - } + int t; + for(int i=0;i data[j+1]) + { + t = data[j+1]; + data[j+1] = data[j]; + data[j] = t; + } } + } } ``` From ae46a2ade7f64396696fc476c96cb22ada997c64 Mon Sep 17 00:00:00 2001 From: lisen Date: Thu, 28 Feb 2019 10:12:38 +0800 Subject: [PATCH 03/13] Update 1.bubbleSort.md --- 1.bubbleSort.md | 1 + 1 file changed, 1 insertion(+) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index b93ad36..46b2131 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -24,6 +24,7 @@ ## 3. 什么时候最快 当输入的数据已经是正序时(都已经是正序了,我还要你冒泡排序有何用啊)。 +这时只进行一趟排序:元素比较次数为n-1,元素移动为0。 ## 4. 什么时候最慢 From a8e6f8bb38896efd237ef6a30987292a0cd9376f Mon Sep 17 00:00:00 2001 From: lisen Date: Thu, 28 Feb 2019 11:03:48 +0800 Subject: [PATCH 04/13] Update 2.selectionSort.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加c代码实现 --- 2.selectionSort.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/2.selectionSort.md b/2.selectionSort.md index 103830e..680cc53 100644 --- a/2.selectionSort.md +++ b/2.selectionSort.md @@ -126,3 +126,25 @@ function selectionSort($arr) return $arr; } ``` + +## 8. C 代码实现 + +```c +void SelectSort(int *data, int n) +{ + int i, j,t; + int min_index; + for(i = 0; i < n - 1; i ++){ + min_index = i; + for( j = i + 1; j < n; j++){ + if(data[j] < data[min_index]) + min_index = j; + } + if (min_index != i){ + t = data[i]; + data[i] = data[min_index]; + data[min_index] = t; + } + } +} +``` From 9aff8bc2ce3e81a559b9b21295d568ec8111e30d Mon Sep 17 00:00:00 2001 From: lisen Date: Mon, 4 Mar 2019 07:52:40 +0800 Subject: [PATCH 05/13] Update 1.bubbleSort.md --- 1.bubbleSort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index 46b2131..8cb7643 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -55,7 +55,7 @@ function bubbleSort(arr) { ## 6. Python 代码实现 ```python -def bubbleSort(arr): +def BubbleSort(arr): for i in range(1, len(arr)): for j in range(0, len(arr)-i): if arr[j] > arr[j+1]: From b97957b675f0ce02f59f5582052a1a645a1ff979 Mon Sep 17 00:00:00 2001 From: lisen Date: Mon, 4 Mar 2019 15:47:29 +0800 Subject: [PATCH 06/13] Update 1.bubbleSort.md --- 1.bubbleSort.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index 8cb7643..e2c447c 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -20,17 +20,26 @@ ![动图演示](res/bubbleSort.gif) +## 3. 算法分析 -## 3. 什么时候最快 +#### 3.1 什么时候最快 当输入的数据已经是正序时(都已经是正序了,我还要你冒泡排序有何用啊)。 这时只进行一趟排序:元素比较次数为n-1,元素移动为0。 - -## 4. 什么时候最慢 +#### 3.2 什么时候最慢 当输入的数据是反序时(写一个 for 循环反序输出数据不就行了,干嘛要用你冒泡排序呢,我是闲的吗)。 +#### 3.3 时间复杂度 + +设初始文件是反序的,需要进行 n-1 趟排序。每趟排序要进行 n-i 次关键字的比较(1≤i≤n-1),且每次比较都必须移动记录三次来达到交换记录位置。在这种情况下,比较和移动次数均达到最大值: +$$ 比较:C_{max} = \frac{n(n-1)}{2} = O(n^2)$$ +$$ 移动:M_{max} = \frac{3n(n-1)}{2} = O(n^2)$$ + +冒泡排序的最坏时间复杂度为$$ O(n^2)$$。 +综上,因此冒泡排序总的平均时间复杂度为$$ O(n^2)$$。 + ## 5. JavaScript 代码实现 From 36897dec4daabb2bca4c8d1397617583cf8d2e31 Mon Sep 17 00:00:00 2001 From: lisen Date: Mon, 4 Mar 2019 15:48:02 +0800 Subject: [PATCH 07/13] Update 1.bubbleSort.md --- 1.bubbleSort.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index e2c447c..b686353 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -33,12 +33,13 @@ #### 3.3 时间复杂度 -设初始文件是反序的,需要进行 n-1 趟排序。每趟排序要进行 n-i 次关键字的比较(1≤i≤n-1),且每次比较都必须移动记录三次来达到交换记录位置。在这种情况下,比较和移动次数均达到最大值: +设初始文件是反序的,需要进行 n-1 趟排序。每趟排序要进行 n-i 次关键字的比较(1≤i≤n-1),且每次比较都必须移动记录三次来达到交换记录位置。在这种情况下,比较和移动次数均达到最大值: + $$ 比较:C_{max} = \frac{n(n-1)}{2} = O(n^2)$$ $$ 移动:M_{max} = \frac{3n(n-1)}{2} = O(n^2)$$ -冒泡排序的最坏时间复杂度为$$ O(n^2)$$。 -综上,因此冒泡排序总的平均时间复杂度为$$ O(n^2)$$。 +冒泡排序的最坏时间复杂度为$$ O(n^2)$$。 +综上,因此冒泡排序总的平均时间复杂度为$$ O(n^2)$$。 ## 5. JavaScript 代码实现 From 6516bb8b6023b51b25ccb43b3fd16a3d53d6c9ee Mon Sep 17 00:00:00 2001 From: lisen Date: Mon, 4 Mar 2019 15:49:41 +0800 Subject: [PATCH 08/13] Update 1.bubbleSort.md --- 1.bubbleSort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index b686353..06642d3 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -35,7 +35,7 @@ 设初始文件是反序的,需要进行 n-1 趟排序。每趟排序要进行 n-i 次关键字的比较(1≤i≤n-1),且每次比较都必须移动记录三次来达到交换记录位置。在这种情况下,比较和移动次数均达到最大值: -$$ 比较:C_{max} = \frac{n(n-1)}{2} = O(n^2)$$ +$$ 比较:C_{max} = \frac{n(n-1)}{2} = O(n^2)$$ $$ 移动:M_{max} = \frac{3n(n-1)}{2} = O(n^2)$$ 冒泡排序的最坏时间复杂度为$$ O(n^2)$$。 From 2a19dbe03a79f5f69c5ace5bb06dcf2aa092c8d2 Mon Sep 17 00:00:00 2001 From: lisen Date: Mon, 4 Mar 2019 15:52:02 +0800 Subject: [PATCH 09/13] Update 1.bubbleSort.md --- 1.bubbleSort.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index 06642d3..3d9c5f9 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -35,8 +35,8 @@ 设初始文件是反序的,需要进行 n-1 趟排序。每趟排序要进行 n-i 次关键字的比较(1≤i≤n-1),且每次比较都必须移动记录三次来达到交换记录位置。在这种情况下,比较和移动次数均达到最大值: -$$ 比较:C_{max} = \frac{n(n-1)}{2} = O(n^2)$$ -$$ 移动:M_{max} = \frac{3n(n-1)}{2} = O(n^2)$$ +比较: $$ C_{max} = \frac{n(n-1)}{2} = O(n^2) $$ +移动: $$ M_{max} = \frac{3n(n-1)}{2} = O(n^2) $$ 冒泡排序的最坏时间复杂度为$$ O(n^2)$$。 综上,因此冒泡排序总的平均时间复杂度为$$ O(n^2)$$。 From 5991c21cf3936321121fdc86609591d13575c99f Mon Sep 17 00:00:00 2001 From: lisen Date: Mon, 4 Mar 2019 16:02:10 +0800 Subject: [PATCH 10/13] Update 1.bubbleSort.md --- 1.bubbleSort.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index 3d9c5f9..df3f289 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -35,11 +35,11 @@ 设初始文件是反序的,需要进行 n-1 趟排序。每趟排序要进行 n-i 次关键字的比较(1≤i≤n-1),且每次比较都必须移动记录三次来达到交换记录位置。在这种情况下,比较和移动次数均达到最大值: -比较: $$ C_{max} = \frac{n(n-1)}{2} = O(n^2) $$ -移动: $$ M_{max} = \frac{3n(n-1)}{2} = O(n^2) $$ +比较: C_max = n(n-1)/2 = O(n^2) +移动: M_max = 3n(n-1)/2 = O(n^2) -冒泡排序的最坏时间复杂度为$$ O(n^2)$$。 -综上,因此冒泡排序总的平均时间复杂度为$$ O(n^2)$$。 +冒泡排序的最坏时间复杂度为 O(n^2)。 +综上,因此冒泡排序总的平均时间复杂度为 O(n^2)。 ## 5. JavaScript 代码实现 From 06d9816ad06e21cbf8f664871337e21b8757ec6a Mon Sep 17 00:00:00 2001 From: lisen Date: Mon, 4 Mar 2019 16:48:33 +0800 Subject: [PATCH 11/13] Update 3.insertionSort.md add c code --- 3.insertionSort.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/3.insertionSort.md b/3.insertionSort.md index c828cb3..ca5f319 100644 --- a/3.insertionSort.md +++ b/3.insertionSort.md @@ -118,3 +118,27 @@ function insertionSort($arr) return $arr; } ``` + +## 8. C 代码实现 + +``` c +void insert_sort(int data[], int n) +{ + int i,j; + int temp; + for(i=1;i=0)&&(data[j]>temp)) + { + data[j+1]=data[j]; + j--; + } + //存在大于temp的数 + if(j!=i-1) + data[j+1]=temp; + } +} +``` From 46ba2b7c13944e576cb7c7a0e12928eed5b7b152 Mon Sep 17 00:00:00 2001 From: lisen Date: Wed, 6 Mar 2019 14:13:29 +0800 Subject: [PATCH 12/13] Update 1.bubbleSort.md --- 1.bubbleSort.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index df3f289..ed2eb42 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -65,7 +65,7 @@ function bubbleSort(arr) { ## 6. Python 代码实现 ```python -def BubbleSort(arr): +def bubbleSort(arr): for i in range(1, len(arr)): for j in range(0, len(arr)-i): if arr[j] > arr[j+1]: @@ -142,7 +142,7 @@ function bubbleSort($arr) ``` ## 10. C 代码实现 ```c -void BubbleSort1(int data[],int n) //n为data长度 +void bubbleSort1(int data[],int n) //n为data长度 { int t; for(int i=0;i Date: Wed, 6 Mar 2019 14:14:07 +0800 Subject: [PATCH 13/13] Update 2.selectionSort.md --- 2.selectionSort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2.selectionSort.md b/2.selectionSort.md index 680cc53..4c9e004 100644 --- a/2.selectionSort.md +++ b/2.selectionSort.md @@ -130,7 +130,7 @@ function selectionSort($arr) ## 8. C 代码实现 ```c -void SelectSort(int *data, int n) +void selectSort(int *data, int n) { int i, j,t; int min_index;