Skip to content

Commit 03fcdfd

Browse files
author
daiju
committed
排序
0 parents  commit 03fcdfd

File tree

9 files changed

+318
-0
lines changed

9 files changed

+318
-0
lines changed

.idea/kotlinc.xml

Lines changed: 7 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.

myselfstudy/myselfstudy.iml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
</component>
15+
</module>

myselfstudy/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.sty</groupId>
8+
<artifactId>myself-study</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* @author <a href="mailto:daiju3@crnet.com.hk">daiju3</a>
7+
* @version 1.0.0
8+
* @description
9+
* @since 2018/6/29 16:31
10+
*/
11+
public class Ac {
12+
13+
public static String reverse(String s){
14+
char [] str=s.toCharArray();
15+
Stack<Character> stack=new Stack<Character>();
16+
for(int i=0;i<str.length;i++){
17+
stack.push(str[i]);
18+
}
19+
20+
String reversed="";
21+
for(int i=0;i<str.length;i++){
22+
reversed+=stack.pop();
23+
}
24+
return reversed;
25+
}
26+
27+
public static String reverse2(String s){
28+
int length=s.length();
29+
String reversed="";
30+
for(int i=0;i<length;i++){
31+
reversed=s.charAt(i)+reversed;
32+
}
33+
return reversed;
34+
}
35+
public static void main(String[] args) {
36+
String a=reverse("abcdefg");
37+
String b=reverse2("abcdefgh");
38+
System.out.println(a);
39+
System.out.println(b);
40+
}
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com;
2+
3+
/**
4+
* @author <a href="mailto:daiju3@crnet.com.hk">daiju3</a>
5+
* @version 1.0.0
6+
* @description
7+
* @since 2018/6/27 9:10
8+
*/
9+
abstract class Animal {
10+
11+
12+
13+
public static void quickSort(int a[],int low,int high){
14+
int start=low;
15+
int end =high;
16+
int key=a[low];
17+
18+
19+
while (end>start){
20+
while (end>start&&a[end]>=key)
21+
22+
end--;
23+
if(a[end]<=key){
24+
int temp=a[end];
25+
a[end]=a[start];
26+
a[start]=temp;
27+
}
28+
29+
while (end>start&&a[start]<=key)
30+
start++;
31+
if(a[start]>=key){
32+
int tem=a[start];
33+
a[start]=a[end];
34+
a[end]=tem;
35+
}
36+
}
37+
if(start>low)quickSort(a,low,start-1);
38+
if(end<high)quickSort(a,end+1,high);
39+
}
40+
41+
public static void main(String[] args) {
42+
int[] a = {12,20,5,16,15,1,30,45,23,9};
43+
int start = 0;
44+
int end = a.length-1;
45+
quickSort(a,start,end);
46+
for(int i = 0; i<a.length; i++){
47+
System.out.println(a[i]);
48+
}
49+
50+
}
51+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com;
2+
3+
import java.math.BigDecimal;
4+
5+
/**
6+
* @author <a href="mailto:daiju3@crnet.com.hk">daiju3</a>
7+
* @version 1.0.0
8+
* @description 冒泡排序算法以及优化
9+
* @since 2018/6/25 19:46
10+
*/
11+
public class BubbleSort {
12+
public static final BigDecimal FINAL_NUMBER = BigDecimal.valueOf(100);
13+
14+
public static void main(String[] args) {
15+
int a[]={111,2,3234,4323,24,421,999,65656,3332,44,55,333};
16+
bubbleSort3(a);
17+
for(int i:a){
18+
System.out.println(i);
19+
}
20+
21+
}
22+
23+
public static void exchange(Integer a){
24+
a=2222;
25+
}
26+
27+
28+
public static void bubbleSort1(int a[]){
29+
for(int i=0;i<a.length;i++){
30+
for(int j=0;j<a.length-i-1;j++){
31+
if(a[j]>a[j+1]){
32+
int temp=a[j];
33+
a[j]=a[j+1];
34+
a[j+1]=temp;
35+
}
36+
}
37+
}
38+
}
39+
//优化1
40+
public static void bubbleSort2(int a[]){
41+
for(int i=0;i<a.length;i++){
42+
boolean isExchange=true;
43+
for (int j=0;j<a.length-i-1;j++){
44+
if(a[j]>a[j+1]){
45+
int temp=a[j];
46+
a[j]=a[j+1];
47+
a[j+1]=temp;
48+
isExchange=false;
49+
}
50+
}
51+
if(isExchange){
52+
break;
53+
}
54+
}
55+
}
56+
57+
public static void bubbleSort3(int a[]){
58+
int temp=0;
59+
//记录最后一次交换的位置
60+
int lastExchangeIndex=0;
61+
//无序数列的边界,每次只需要比较到这里为止
62+
int sortBorder=a.length-1;
63+
for(int i=0;i<a.length;i++){
64+
//有序标记,每一轮的初始值为true
65+
boolean isSort=true;
66+
for(int j=0;j<sortBorder;j++){
67+
if(a[j]>a[j+1]){
68+
temp=a[j];
69+
a[j]=a[j+1];
70+
a[j+1]=temp;
71+
isSort=false;//有元素交换,所以不是有序,标记变为false
72+
lastExchangeIndex=j;//把无序数列的边界更新为最后一次交换元素的位置
73+
}
74+
}
75+
sortBorder=lastExchangeIndex;
76+
if(isSort){
77+
break;
78+
}
79+
}
80+
81+
}
82+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com;
2+
3+
/**
4+
* @author <a href="mailto:daiju3@crnet.com.hk">daiju3</a>
5+
* @version 1.0.0
6+
* @description
7+
* @since 2018/7/12 16:02
8+
*/
9+
public class IntegetComper {
10+
11+
public static Integer max = Integer.MAX_VALUE;
12+
13+
14+
public static void main(String[] args) {
15+
16+
17+
int a[]={1,2,3,42323,213,43323,55555,221332,996};
18+
int start=0;
19+
int end=a.length-1;
20+
System.out.println(a.length);
21+
quickSort(a,start,end);
22+
for(int i:a){
23+
System.out.println(i);
24+
}
25+
}
26+
27+
28+
public static void quickSort(int []a, int low, int high) {
29+
int start = low;
30+
int end = high;
31+
int key = a[low];
32+
33+
while (end > start) {
34+
while (end > start && a[end] >= key)
35+
end--;
36+
if (a[end] <= key) {
37+
int temp = a[end];
38+
a[end] = a[start];
39+
a[start] = temp;
40+
}
41+
while (end > start && a[start] <= key)
42+
start++;
43+
if (a[start] >= key) {
44+
int temp = a[start];
45+
a[start] = a[end];
46+
a[end] = temp;
47+
}
48+
}
49+
if(start>low) quickSort(a,low,start-1);
50+
if(end<high) quickSort(a,end+1,high);
51+
}
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com;
2+
3+
/**
4+
* @author <a href="mailto:daiju3@crnet.com.hk">daiju3</a>
5+
* @version 1.0.0
6+
* @description 快排
7+
* @since 2018/7/12 10:45
8+
*/
9+
public class QuickSort {
10+
11+
12+
public static void main(String[] args) {
13+
int a[]={12,20,4,16,1,43,56,32,444,112,6666};
14+
int start=0;
15+
int end =a.length-1;
16+
quickSort(a,start,end);
17+
for(Integer i:a){
18+
System.out.println(i);
19+
}
20+
}
21+
22+
public static void quickSort(int [] a,int low ,int high){
23+
int start=low;
24+
int end=high;
25+
int key=a[low];
26+
27+
while (end>start){
28+
//从后往前比较
29+
while (end>start&&a[end]>=key)
30+
end--;
31+
if(a[end]<=key){
32+
int temp=a[end];
33+
a[end]=a[start];
34+
a[start]=temp;
35+
}
36+
//从前往后比较
37+
while (end>start&&a[start]<=key)//如果没有比关键值大的,比较下一个,直到有比关键值大的交换位置
38+
start++;
39+
if(a[start]>=key){
40+
int temp=a[start];
41+
a[start]=a[end];
42+
a[end]=temp;
43+
}
44+
}
45+
//此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用
46+
47+
//递归
48+
if(start>low) quickSort(a,low,start-1);
49+
if(end<high) quickSort(a,end+1,high);
50+
51+
}
52+
}

0 commit comments

Comments
 (0)