Skip to content

Commit 3318b50

Browse files
committed
新增了3个新算法
1 parent d681cb0 commit 3318b50

File tree

11 files changed

+356
-0
lines changed

11 files changed

+356
-0
lines changed

.classpath

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="lib" path="E:/workspace/Jar/william-jutils/jutils-core-0.1.jar" sourcepath="/Naga-UtilLibrary"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010

1111
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1212
hs_err_pid*
13+
/bin/

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ArraySortAlgorithm</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# ArraySortAlgorithm
22
各个排序算法
3+
4+
本人CSDN博客 [点击链接](http://blog.csdn.net/lemon_tree12138)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.algorithm.array.sort.client;
2+
3+
import org.algorithm.array.sort.impl.BubbleBothwaySort;
4+
import org.algorithm.array.sort.impl.BubbleSort;
5+
import org.algorithm.array.sort.impl.QuickSort;
6+
import org.algorithm.array.sort.impl.RadixSort;
7+
import org.algorithm.array.sort.interf.Sortable;
8+
import org.utils.naga.containers.ArrayUtils;
9+
10+
public class SortClient {
11+
12+
public static void main(String[] args) {
13+
int[] array = {34, 23, 98, 76, 56, 54, 90, 12, 34, 65, 45, 78};
14+
Sortable sortable = new QuickSort();
15+
array = sortable.sort(array);
16+
17+
ArrayUtils.show(array);
18+
}
19+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.algorithm.array.sort.impl;
2+
3+
import org.algorithm.array.sort.interf.Sortable;
4+
import org.utils.naga.containers.ArrayUtils;
5+
6+
/**
7+
* <p>
8+
* 冒泡排序(双向冒泡)
9+
* </p>
10+
* 2016年1月19日
11+
*
12+
* @author <a href="http://weibo.com/u/5131020927">Q-WHai</a>
13+
* @see <a href="http://blog.csdn.net/lemon_tree12138">http://blog.csdn.net/lemon_tree12138</a>
14+
* @version 0.1.1
15+
*/
16+
public class BubbleBothwaySort implements Sortable {
17+
18+
@Override
19+
public int[] sort(int[] array) {
20+
if (array == null) {
21+
return null;
22+
}
23+
24+
int arrayLength = array.length;
25+
26+
int preIndex = 0;
27+
int backIndex = arrayLength - 1;
28+
while(preIndex < backIndex) {
29+
preSort(array, arrayLength, preIndex);
30+
preIndex++;
31+
32+
if (preIndex >= backIndex) {
33+
break;
34+
}
35+
36+
backSort(array, arrayLength, backIndex);
37+
backIndex--;
38+
}
39+
40+
return array;
41+
}
42+
43+
// 从前向后排序
44+
private void preSort(int[] array, int length, int preIndex) {
45+
for (int i = preIndex + 1; i < length; i++) {
46+
if (array[preIndex] > array[i]) {
47+
ArrayUtils.swap(array, preIndex, i);
48+
}
49+
}
50+
}
51+
52+
// 从后向前排序
53+
private void backSort(int[] array, int length, int backIndex) {
54+
for (int i = backIndex - 1; i >= 0; i--) {
55+
if (array[i] > array[backIndex]) {
56+
ArrayUtils.swap(array, i, backIndex);
57+
}
58+
}
59+
}
60+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.algorithm.array.sort.impl;
2+
3+
import org.algorithm.array.sort.interf.Sortable;
4+
import org.utils.naga.containers.ArrayUtils;
5+
6+
/**
7+
* <p>
8+
* 冒泡排序(单向冒泡)
9+
* </p>
10+
* 2016年1月19日
11+
*
12+
* @author <a href="http://weibo.com/u/5131020927">Q-WHai</a>
13+
* @see <a href="http://blog.csdn.net/lemon_tree12138">http://blog.csdn.net/lemon_tree12138</a>
14+
* @version 0.1.1
15+
*/
16+
public class BubbleSort implements Sortable {
17+
18+
@Override
19+
public int[] sort(int[] array) {
20+
if (array == null) {
21+
return null;
22+
}
23+
24+
int arrayLength = array.length;
25+
for (int i = 0; i < arrayLength; i++) {
26+
for (int j = i + 1; j < arrayLength; j++) {
27+
if (array[i] > array[j]) {
28+
ArrayUtils.swap(array, i, j);
29+
}
30+
}
31+
}
32+
33+
return array;
34+
}
35+
36+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.algorithm.array.sort.impl;
2+
3+
import org.algorithm.array.sort.interf.Sortable;
4+
5+
/**
6+
* <p>
7+
* 快速排序算法
8+
* </p>
9+
* 2016年1月19日
10+
*
11+
* @author <a href="http://weibo.com/u/5131020927">Q-WHai</a>
12+
* @see <a href="http://blog.csdn.net/lemon_tree12138">http://blog.csdn.net/lemon_tree12138</a>
13+
* @version 0.1.1
14+
*/
15+
public class QuickSort implements Sortable {
16+
17+
@Override
18+
public int[] sort(int[] array) {
19+
if (array == null) {
20+
return null;
21+
}
22+
23+
sortCore(array, 0, array.length - 1);
24+
return array;
25+
}
26+
27+
/*
28+
* 排序的核心算法
29+
*
30+
* @param array
31+
* 待排序数组
32+
* @param startIndex
33+
* 开始位置
34+
* @param endIndex
35+
* 结束位置
36+
*/
37+
private void sortCore(int[] array, int startIndex, int endIndex) {
38+
if (startIndex >= endIndex) {
39+
return;
40+
}
41+
42+
int boundary = boundary(array, startIndex, endIndex);
43+
44+
sortCore(array, startIndex, boundary - 1);
45+
sortCore(array, boundary + 1, endIndex);
46+
}
47+
48+
/*
49+
* 交换并返回分界点
50+
*
51+
* @param array
52+
* 待排序数组
53+
* @param startIndex
54+
* 开始位置
55+
* @param endIndex
56+
* 结束位置
57+
* @return
58+
* 分界点
59+
*/
60+
private int boundary(int[] array, int startIndex, int endIndex) {
61+
int standard = array[startIndex]; // 定义标准
62+
int leftIndex = startIndex; // 左指针
63+
int rightIndex = endIndex; // 右指针
64+
65+
while(leftIndex < rightIndex) {
66+
while(leftIndex < rightIndex && array[rightIndex] >= standard) {
67+
rightIndex--;
68+
}
69+
array[leftIndex] = array[rightIndex];
70+
71+
while(leftIndex < rightIndex && array[leftIndex] <= standard) {
72+
leftIndex++;
73+
}
74+
array[rightIndex] = array[leftIndex];
75+
}
76+
77+
array[leftIndex] = standard;
78+
return leftIndex;
79+
}
80+
81+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package org.algorithm.array.sort.impl;
2+
3+
import org.algorithm.array.sort.interf.Sortable;
4+
5+
/**
6+
* <p>
7+
* 基数排序/桶排序
8+
* </p>
9+
* 2016年1月19日
10+
*
11+
* @author <a href="http://weibo.com/u/5131020927">Q-WHai</a>
12+
* @see <a href="http://blog.csdn.net/lemon_tree12138">http://blog.csdn.net/lemon_tree12138</a>
13+
* @version 0.1.1
14+
*/
15+
public class RadixSort implements Sortable {
16+
17+
@Override
18+
public int[] sort(int[] array) {
19+
if (array == null) {
20+
return null;
21+
}
22+
23+
int maxLength = maxLength(array);
24+
25+
return sortCore(array, 0, maxLength);
26+
}
27+
28+
private int[] sortCore(int[] array, int digit, int maxLength) {
29+
if (digit >= maxLength) {
30+
return array;
31+
}
32+
33+
final int radix = 10; // 基数
34+
int arrayLength = array.length;
35+
int[] count = new int[radix];
36+
int[] bucket = new int[arrayLength];
37+
38+
// 统计将数组中的数字分配到桶中后,各个桶中的数字个数
39+
for (int i = 0; i < arrayLength; i++) {
40+
count[getDigit(array[i], digit)]++;
41+
}
42+
43+
// 将各个桶中的数字个数,转化成各个桶中最后一个数字的下标索引
44+
for (int i = 1; i < radix; i++) {
45+
count[i] = count[i] + count[i - 1];
46+
}
47+
48+
// 将原数组中的数字分配给辅助数组bucket
49+
for (int i = arrayLength - 1; i >= 0; i--) {
50+
int number = array[i];
51+
int d = getDigit(number, digit);
52+
53+
bucket[count[d] - 1] = number;
54+
55+
count[d]--;
56+
}
57+
58+
return sortCore(bucket, digit + 1, maxLength);
59+
}
60+
61+
/*
62+
* 一个数组中最数字的位数
63+
*
64+
* @param array
65+
* @return
66+
*/
67+
private int maxLength(int[] array) {
68+
int maxLength = 0;
69+
int arrayLength = array.length;
70+
for (int i = 0; i < arrayLength; i++) {
71+
int currentLength = length(array[i]);
72+
if (maxLength < currentLength) {
73+
maxLength = currentLength;
74+
}
75+
}
76+
77+
return maxLength;
78+
}
79+
80+
/*
81+
* 计算一个数字共有多少位
82+
*
83+
* @param number
84+
* @return
85+
*/
86+
private int length(int number) {
87+
return String.valueOf(number).length();
88+
}
89+
90+
/*
91+
* 获取x这个数的d位数上的数字
92+
* 比如获取123的0位数,结果返回3
93+
*
94+
* @param x
95+
* @param d
96+
* @return
97+
*/
98+
private int getDigit(int x, int d) {
99+
int a[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
100+
return ((x / a[d]) % 10);
101+
}
102+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.algorithm.array.sort.interf;
2+
3+
/**
4+
* <p>
5+
* 排序策略的公共接口
6+
* </p>
7+
* 2016年1月19日
8+
*
9+
* @author <a href="http://weibo.com/u/5131020927">Q-WHai</a>
10+
* @see <a href="http://blog.csdn.net/lemon_tree12138">http://blog.csdn.net/lemon_tree12138</a>
11+
* @version 0.1.1
12+
*/
13+
public interface Sortable {
14+
15+
public int[] sort(int[] array);
16+
}

0 commit comments

Comments
 (0)