diff --git a/1.bubbleSort.md b/1.bubbleSort.md index 78c3b83..d6d12fe 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -21,14 +21,38 @@ ![动图演示](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 代码实现 diff --git a/2.selectionSort.md b/2.selectionSort.md index 103830e..0a3dea1 100644 --- a/2.selectionSort.md +++ b/2.selectionSort.md @@ -16,8 +16,49 @@ ![动图演示](res/selectionSort.gif) +## 3. C++代码实现 -## 3. JavaScript 代码实现 +```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(); + //边界值判定 + 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; +} +``` + +## 4. JavaScript 代码实现 ```js function selectionSort(arr) { @@ -38,7 +79,7 @@ function selectionSort(arr) { } ``` -## 4. Python 代码实现 +## 5. Python 代码实现 ```python def selectionSort(arr): @@ -54,7 +95,7 @@ def selectionSort(arr): return arr ``` -## 5. Go 代码实现 +## 6. Go 代码实现 ```go func selectionSort(arr []int) []int { @@ -72,7 +113,7 @@ func selectionSort(arr []int) []int { } ``` -## 6. Java 代码实现 +## 7. Java 代码实现 ```java public class SelectionSort implements IArraySort { @@ -106,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..931c244 100644 --- a/3.insertionSort.md +++ b/3.insertionSort.md @@ -16,8 +16,37 @@ ![动图演示](res/insertionSort.gif) +## 3. C++代码实现 -## 3. JavaScript 代码实现 +```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; +} +``` + +## 4. JavaScript 代码实现 ```js function insertionSort(arr) { @@ -36,7 +65,7 @@ function insertionSort(arr) { } ``` -## 4. Python 代码实现 +## 5. Python 代码实现 ```python def insertionSort(arr): @@ -50,7 +79,7 @@ def insertionSort(arr):    return arr ``` -## 5. Go 代码实现 +## 6. Go 代码实现 ```go func insertionSort(arr []int) []int { for i := range arr { @@ -66,7 +95,7 @@ func insertionSort(arr []int) []int { } ``` -## 6. Java 代码实现 +## 7. Java 代码实现 ```java public class InsertSort implements IArraySort { @@ -100,7 +129,7 @@ public class InsertSort implements IArraySort { } ``` -## 7. PHP 代码实现 +## 8. PHP 代码实现 ```php function insertionSort($arr) 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) diff --git a/5.mergeSort.md b/5.mergeSort.md index bf0de32..2ed42bd 100644 --- a/5.mergeSort.md +++ b/5.mergeSort.md @@ -35,8 +35,77 @@ ![动图演示](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