Skip to content

Commit 5b194d4

Browse files
邵明邵明
authored andcommitted
Bubble and Selection
0 parents  commit 5b194d4

File tree

12 files changed

+206
-0
lines changed

12 files changed

+206
-0
lines changed

.idea/.gitignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JavaSortAlgorithm.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Java ten kinds of Sort Algorithm

src/cn/shellming/impl/ArraySort.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cn.shellming.impl;
2+
3+
/**
4+
* @ClassName ArraySort
5+
* @Description TODO
6+
* @Author shaoming
7+
* @Date 2020/3/31 10:30 下午
8+
*/
9+
public interface ArraySort {
10+
int[] sort(int[] sourceArray);
11+
}

src/cn/shellming/impl/BubbleSort.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cn.shellming.impl;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @ClassName BubbleSort
7+
* @Description TODO
8+
* @Author shaoming
9+
* @Date 2020/3/31 10:33 下午
10+
*/
11+
/**
12+
* 算法步骤
13+
*
14+
* 1.比较相邻的元素。如果第一个比第二个大,就交换他们两个。
15+
* 2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。
16+
* 3.针对所有的元素重复以上的步骤,除了最后一个。
17+
* 4.持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
18+
*/
19+
public class BubbleSort implements ArraySort {
20+
@Override
21+
public int[] sort(int[] sourceArray) {
22+
long t1 = System.currentTimeMillis();
23+
int[] res = Arrays.copyOf(sourceArray,sourceArray.length);
24+
for(int i = 1; i < res.length; ++i){
25+
boolean flag = true;
26+
for (int j = 0; j < res.length - i; ++j){
27+
if(res[j] > res[j + 1]){
28+
int temp = res[j];
29+
res[j] = res[j + 1];
30+
res[j + 1] = temp;
31+
flag = false;
32+
}
33+
}
34+
if (flag){
35+
break;
36+
}
37+
}
38+
System.err.println(this.getClass().getName() + " " + (System.currentTimeMillis() - t1));
39+
return res;
40+
}
41+
}

0 commit comments

Comments
 (0)