From 3c0a5bc4fbc486f6dabf4b240aac993ae5e729e9 Mon Sep 17 00:00:00 2001 From: Lucky <231876009@qq.com> Date: Mon, 7 Jan 2019 21:56:37 +0800 Subject: [PATCH 1/8] add C++ Algorithm add C++ Algorithm --- 1.bubbleSort.md | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index 78c3b83..0d8ed94 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -21,14 +21,37 @@ ![动图演示](res/bubbleSort.gif) -## 3. 什么时候最快 - -当输入的数据已经是正序时(都已经是正序了,我还要你冒泡排序有何用啊)。 - - -## 4. 什么时候最慢 +## 3. C++代码实现 +```cpp +/* + 冒泡排序,升序排列 + O(N^2)时间复杂度,O(1)空间复杂度 +*/ +void changeValue(int *a, int *b) +{ + int temp = *a; + *a = *b; + *b = temp; +} +vector AlgorithmSort::bubbleSort(vector vec) +{ + int len = vec.size(); + //边界值判定 + if (len <= 1) + return vec; + //冒泡排序 + for (int i = 0; i < len; i++) + { + for (int j = 0; j < len-1; j++) + { + if (vec[j] > vec[j + 1]) + changeValue(&vec[j], &vec[j + 1]); + } + } -当输入的数据是反序时(写一个 for 循环反序输出数据不就行了,干嘛要用你冒泡排序呢,我是闲的吗)。 + return vec; +} +``` ## 5. JavaScript 代码实现 From 89ce6da157daad41ea3cbf476a89ba1a91505657 Mon Sep 17 00:00:00 2001 From: Lucky <231876009@qq.com> Date: Mon, 7 Jan 2019 22:25:47 +0800 Subject: [PATCH 2/8] add cpp algorithm add cpp algorithm --- 2.selectionSort.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/2.selectionSort.md b/2.selectionSort.md index 103830e..6d9f64a 100644 --- a/2.selectionSort.md +++ b/2.selectionSort.md @@ -16,6 +16,46 @@ ![动图演示](res/selectionSort.gif) +```cpp +void changeValue(int *a, int *b) +{ + int temp = *a; + *a = *b; + *b = temp; +} +/* + 选择排序,升序排列 + O(N^2)时间复杂度,O(1)空间复杂度 +*/ +vector AlgorithmSort::selectionSort(vector vec) +{ + int len = vec.size(); + int min = 0; + //边界值判定 + if (len <= 1) + return vec; + + //原始写法 + //for (int i = 0; i < len; i++) + //{ + // for (int j = i+1; j < len; j++) + // { + // if (vec[i] > vec[j]) + // changeValue(&vec[i], &vec[j]); + // } + //} + + //优化写法 + for (int i = 0; i < len-1; i++) + { + // 取最小元素 + vector::iterator it = min_element(vec.begin() + i + 1, vec.end()); + if(vec[i]>*it) + changeValue(&vec[i], &vec[it-vec.begin()]); + } + return vec; +} +``` ## 3. JavaScript 代码实现 From 18e2630fa90364e963ad2d89a455354018382065 Mon Sep 17 00:00:00 2001 From: Lucky <231876009@qq.com> Date: Mon, 7 Jan 2019 22:42:28 +0800 Subject: [PATCH 3/8] add insertionSort cpp program add insertionSort cpp program --- 2.selectionSort.md | 13 +++++++------ 3.insertionSort.md | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/2.selectionSort.md b/2.selectionSort.md index 6d9f64a..0a3dea1 100644 --- a/2.selectionSort.md +++ b/2.selectionSort.md @@ -16,6 +16,8 @@ ![动图演示](res/selectionSort.gif) +## 3. C++代码实现 + ```cpp void changeValue(int *a, int *b) { @@ -30,7 +32,6 @@ void changeValue(int *a, int *b) vector AlgorithmSort::selectionSort(vector vec) { int len = vec.size(); - int min = 0; //边界值判定 if (len <= 1) return vec; @@ -57,7 +58,7 @@ vector AlgorithmSort::selectionSort(vector vec) } ``` -## 3. JavaScript 代码实现 +## 4. JavaScript 代码实现 ```js function selectionSort(arr) { @@ -78,7 +79,7 @@ function selectionSort(arr) { } ``` -## 4. Python 代码实现 +## 5. Python 代码实现 ```python def selectionSort(arr): @@ -94,7 +95,7 @@ def selectionSort(arr): return arr ``` -## 5. Go 代码实现 +## 6. Go 代码实现 ```go func selectionSort(arr []int) []int { @@ -112,7 +113,7 @@ func selectionSort(arr []int) []int { } ``` -## 6. Java 代码实现 +## 7. Java 代码实现 ```java public class SelectionSort implements IArraySort { @@ -146,7 +147,7 @@ public class SelectionSort implements IArraySort { } ``` -## 7. PHP 代码实现 +## 8. PHP 代码实现 ```php function selectionSort($arr) diff --git a/3.insertionSort.md b/3.insertionSort.md index c828cb3..88cd6ee 100644 --- a/3.insertionSort.md +++ b/3.insertionSort.md @@ -16,8 +16,36 @@ ![动图演示](res/insertionSort.gif) +## 3. C++代码实现 +```cpp +void changeValue(int *a, int *b) +{ + int temp = *a; + *a = *b; + *b = temp; +} +//插入排序 +vector AlgorithmSort::insertionSort(vector vec) +{ + int len = vec.size(); + //边界值判定 + if (len <= 1) + return vec; + + for (int i = 0; i < len; i++) + { + for (int j = len-1; j > 0; j--) + { + if (vec[j-1] > vec[j]) + changeValue(&vec[j-1], &vec[j]); + } + } + + return vec; +} +``` -## 3. JavaScript 代码实现 +## 4. JavaScript 代码实现 ```js function insertionSort(arr) { @@ -36,7 +64,7 @@ function insertionSort(arr) { } ``` -## 4. Python 代码实现 +## 5. Python 代码实现 ```python def insertionSort(arr): @@ -50,7 +78,7 @@ def insertionSort(arr):    return arr ``` -## 5. Go 代码实现 +## 6. Go 代码实现 ```go func insertionSort(arr []int) []int { for i := range arr { @@ -66,7 +94,7 @@ func insertionSort(arr []int) []int { } ``` -## 6. Java 代码实现 +## 7. Java 代码实现 ```java public class InsertSort implements IArraySort { @@ -100,7 +128,7 @@ public class InsertSort implements IArraySort { } ``` -## 7. PHP 代码实现 +## 8. PHP 代码实现 ```php function insertionSort($arr) From 52d5fe7e661e73100c4e58d08d42fbeaf0e52927 Mon Sep 17 00:00:00 2001 From: Lucky <231876009@qq.com> Date: Mon, 7 Jan 2019 23:08:50 +0800 Subject: [PATCH 4/8] add shell sort cpp program add shell sort cpp program --- 1.bubbleSort.md | 1 + 3.insertionSort.md | 1 + 4.shellSort.md | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index 0d8ed94..d6d12fe 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -22,6 +22,7 @@ ## 3. C++代码实现 + ```cpp /* 冒泡排序,升序排列 diff --git a/3.insertionSort.md b/3.insertionSort.md index 88cd6ee..931c244 100644 --- a/3.insertionSort.md +++ b/3.insertionSort.md @@ -17,6 +17,7 @@ ![动图演示](res/insertionSort.gif) ## 3. C++代码实现 + ```cpp void changeValue(int *a, int *b) { diff --git a/4.shellSort.md b/4.shellSort.md index 187ac0e..cbf4b2e 100644 --- a/4.shellSort.md +++ b/4.shellSort.md @@ -18,8 +18,34 @@ 3. 每趟排序,根据对应的增量 ti,将待排序列分割成若干长度为 m 的子序列,分别对各子表进行直接插入排序。仅增量因子为 1 时,整个序列作为一个表来处理,表长度即为整个序列的长度。 +## 2. C++代码实现 -## 2. JavaScript 代码实现 +```cpp +vector AlgorithmSort::shellSort(vector vec) +{ + int gap = 1; + while (gap < vec.size()) { + gap = gap * 3 + 1; + } + + while (gap > 0) { + for (int i = gap; i < vec.size(); i++) { + int tmp = vec[i]; + int j = i - gap; + while (j >= 0 && vec[j] > tmp) { + vec[j + gap] = vec[j]; + j -= gap; + } + vec[j + gap] = tmp; + } + gap = (int)(gap / 3); + } + + return vec; +} +``` + +## 3. JavaScript 代码实现 ```js function shellSort(arr) { @@ -42,7 +68,7 @@ function shellSort(arr) { } ``` -## 3. Python 代码实现 +## 4. Python 代码实现 ```python def shellSort(arr): @@ -63,7 +89,7 @@ def shellSort(arr): } ``` -## 4. Go 代码实现 +## 5. Go 代码实现 ```go func shellSort(arr []int) []int { @@ -88,7 +114,7 @@ func shellSort(arr []int) []int { } ``` -## 5. Java 代码实现 +## 6. Java 代码实现 ```java public class ShellSort implements IArraySort { @@ -121,7 +147,7 @@ public class ShellSort implements IArraySort { } ``` -## 6. PHP 代码实现 +## 7. PHP 代码实现 ```php function shellSort($arr) From 35de10adee6e6cb5644ab0707b64b5573e0b76f4 Mon Sep 17 00:00:00 2001 From: Lucky <231876009@qq.com> Date: Tue, 8 Jan 2019 23:47:47 +0800 Subject: [PATCH 5/8] add mergeSort algorithm --- 5.mergeSort.md | 73 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/5.mergeSort.md b/5.mergeSort.md index bf0de32..7b598ed 100644 --- a/5.mergeSort.md +++ b/5.mergeSort.md @@ -35,8 +35,71 @@ ![动图演示](res/mergeSort.gif) +## 4. C++代码实现 -## 4. JavaScript 代码实现 +```cpp +void merge(vector vec, vector::iterator start, vector::iterator mid, vector::iterator end, vector result) +{ + int i = 0; + auto leftStart = start; + auto rightStart = mid + 1; + while (leftStart <= mid && rightStart <= end) + { + if (*leftStart < *rightStart) + result[i++] = *(leftStart++); + else + result[i++] = *(rightStart++); + } + while (leftStart <= mid) + { + result[i++] = *(leftStart++); + } + while (rightStart <= end) + { + result[i++] = *(rightStart++); + } + + for (int j=0;j + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/java/.project b/src/java/.project new file mode 100644 index 0000000..a228c93 --- /dev/null +++ b/src/java/.project @@ -0,0 +1,23 @@ + + + sort + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/src/java/.settings/org.eclipse.core.resources.prefs b/src/java/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/src/java/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/src/java/.settings/org.eclipse.jdt.apt.core.prefs b/src/java/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 0000000..d4313d4 --- /dev/null +++ b/src/java/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=false diff --git a/src/java/.settings/org.eclipse.jdt.core.prefs b/src/java/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..5592a0a --- /dev/null +++ b/src/java/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.processAnnotations=disabled +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/src/java/.settings/org.eclipse.m2e.core.prefs b/src/java/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/src/java/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 From cfa3265ec57378b766066ae6ceaaf9b39cdadc2d Mon Sep 17 00:00:00 2001 From: Lucky <231876009@qq.com> Date: Thu, 10 Jan 2019 02:10:01 +0800 Subject: [PATCH 8/8] upd --- 5.mergeSort.md | 2 +- 6.quickSort.md | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/5.mergeSort.md b/5.mergeSort.md index e030d11..2ed42bd 100644 --- a/5.mergeSort.md +++ b/5.mergeSort.md @@ -39,7 +39,7 @@ ```cpp /* - 用迭代器实现归并排序过程 + 用迭代器,递归方法实现归并排序过程 */ //归并排序合并过程 diff --git a/6.quickSort.md b/6.quickSort.md index 6f38c97..a33d912 100644 --- a/6.quickSort.md +++ b/6.quickSort.md @@ -183,23 +183,23 @@ int QuickSortPart(vector *vec, int left, int right) return index; } //快速排序递归函数 -void QuickSortA(vector *vec, int left, int right) +void QuickSortSub(vector *vec, int left, int right) { if(right>left) { //patition分区 int pivot = QuickSortPart(vec, left, right); //左排序 - QuickSortA(vec, left, pivot - 1); + QuickSortSub(vec, left, pivot - 1); //右排序 - QuickSortA(vec, pivot + 1, right); + QuickSortSub(vec, pivot + 1, right); } } //快速排序母函数 vector AlgorithmSort::QuickSort(vector vec) { - QuickSortA(&vec,0,vec.size()-1); + QuickSortSub(&vec,0,vec.size()-1); return vec; } ```