update
This commit is contained in:
parent
3b734ae0d0
commit
e99aa927b3
@ -15,6 +15,7 @@ public class BubbleSort {
|
|||||||
int[] arr = {4, 12, 2, 8, 453, 1, 59, 33};
|
int[] arr = {4, 12, 2, 8, 453, 1, 59, 33};
|
||||||
for (int i = 0, length = arr.length; i < arr.length - 1; i++) {
|
for (int i = 0, length = arr.length; i < arr.length - 1; i++) {
|
||||||
for (int j = 0, tempLength = length - 1 - i; j < tempLength; j++) {
|
for (int j = 0, tempLength = length - 1 - i; j < tempLength; j++) {
|
||||||
|
//如果当前数大于下一个数那么和下一个数交换位置
|
||||||
if (arr[j] > arr[j + 1]) {
|
if (arr[j] > arr[j + 1]) {
|
||||||
int temp = arr[j];
|
int temp = arr[j];
|
||||||
arr[j] = arr[j + 1];
|
arr[j] = arr[j + 1];
|
||||||
|
47
3.排序算法/src/sort/exchange/CountSort.java
Normal file
47
3.排序算法/src/sort/exchange/CountSort.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package sort.exchange;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 类功能简述:计数排序
|
||||||
|
* 类功能详述:
|
||||||
|
*
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2019/8/6 17:35
|
||||||
|
*/
|
||||||
|
public class CountSort {
|
||||||
|
/**
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
* @param arr 待排序数组
|
||||||
|
* @return void
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2019/8/6 17:36
|
||||||
|
*/
|
||||||
|
public static void sort(Integer[] arr, Integer minValue, Integer maxValue) {
|
||||||
|
int range = maxValue - minValue + 1;
|
||||||
|
Integer[] numCount = new Integer[range];
|
||||||
|
Arrays.fill(numCount, 0);
|
||||||
|
for (Integer item : arr) {
|
||||||
|
item = item - minValue;
|
||||||
|
numCount[item]++;
|
||||||
|
}
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < range; i++) {
|
||||||
|
if (numCount[i] == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (int j = 0; j < numCount[i]; j++) {
|
||||||
|
arr[count] = minValue + i;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Integer[] arr = {1, 65, 32, 334, 12, 21, 65, 112, 444443};
|
||||||
|
sort(arr, 1, 444443);
|
||||||
|
System.out.println(Arrays.toString(arr));
|
||||||
|
}
|
||||||
|
}
|
@ -18,7 +18,7 @@ public class InsertSort {
|
|||||||
|
|
||||||
public static void sort(Integer[] arr) {
|
public static void sort(Integer[] arr) {
|
||||||
for (int i = 0, length = arr.length; i < length; i++) {
|
for (int i = 0, length = arr.length; i < length; i++) {
|
||||||
//有序部分从后向前比较
|
//有序部分从后向前比较,直到找到合适的位置
|
||||||
int j = i, temp = arr[i];
|
int j = i, temp = arr[i];
|
||||||
//如果arr[j-1]<=temp,说明arr[j]需为temp,否则将arr[j-1]向后移动一位
|
//如果arr[j-1]<=temp,说明arr[j]需为temp,否则将arr[j-1]向后移动一位
|
||||||
for (; j > 0 && temp < arr[j - 1]; j--) {
|
for (; j > 0 && temp < arr[j - 1]; j--) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package sort.exchange;
|
package sort.exchange;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 类功能简述:归并排序
|
* 类功能简述:归并排序
|
||||||
@ -13,13 +14,25 @@ import java.util.ArrayList;
|
|||||||
*/
|
*/
|
||||||
public class MergeSort {
|
public class MergeSort {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
* @param arr 待排序数组
|
||||||
|
* @param start 开始下标
|
||||||
|
* @param end 结束下标
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2019/8/6 9:29
|
||||||
|
*/
|
||||||
public static void mergeSort(Integer[] arr, int start, int end) {
|
public static void mergeSort(Integer[] arr, int start, int end) {
|
||||||
if (start >= end) {
|
if (start >= end) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int half = (start + end) / 2;
|
int half = (start + end) / 2;
|
||||||
|
//归并左边
|
||||||
mergeSort(arr, start, half);
|
mergeSort(arr, start, half);
|
||||||
|
//归并右边
|
||||||
mergeSort(arr, half + 1, end);
|
mergeSort(arr, half + 1, end);
|
||||||
|
//合并
|
||||||
merge(arr, start, half, end);
|
merge(arr, start, half, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,19 +63,23 @@ public class MergeSort {
|
|||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//说明第二个数组卖完了,将第二个数组剩余部分放到tempList中
|
//说明第二个数组已经完了,将第一个数组剩余部分放到tempList中
|
||||||
while (j <= half) {
|
while (i <= half) {
|
||||||
//说明第二个数组处理完了
|
//说明第二个数组处理完了
|
||||||
tempList.add(arr[j]);
|
tempList.add(arr[i]);
|
||||||
j++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//最后将tempList复制到arr中
|
||||||
|
for (int k = 0, length = tempList.size(); k < length; k++) {
|
||||||
|
arr[start + k] = tempList.get(k);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Integer[] arr = {4, 3, 1, 2, 5, 4, 2, 54};
|
Integer[] arr = {4, 3, 1, 2, 5, 4, 2};
|
||||||
mergeSort(arr, 0, arr.length);
|
mergeSort(arr, 0, arr.length - 1);
|
||||||
|
System.out.println(Arrays.toString(arr));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
58
3.排序算法/src/sort/exchange/RadixSort.java
Normal file
58
3.排序算法/src/sort/exchange/RadixSort.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package sort.exchange;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 类功能简述:基数排序
|
||||||
|
* 类功能详述:
|
||||||
|
*
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2019/8/6 14:33
|
||||||
|
*/
|
||||||
|
public class RadixSort {
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static void sort(Integer[] arr) {
|
||||||
|
//定义桶
|
||||||
|
LinkedList<Integer>[] buckets = new LinkedList[10];
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
buckets[i] = new LinkedList<>();
|
||||||
|
}
|
||||||
|
int size = arr.length;
|
||||||
|
//当前处理第几位的数
|
||||||
|
int count = 0;
|
||||||
|
while (true) {
|
||||||
|
//是否继续进位
|
||||||
|
boolean isContinue = false;
|
||||||
|
//将数放到桶中
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
int temp = arr[i] / (int) Math.pow(10, count) % 10;
|
||||||
|
if (!isContinue && temp != 0) {
|
||||||
|
// 如果存在一个数取的值不为0,说明还要继续循环。
|
||||||
|
isContinue = true;
|
||||||
|
}
|
||||||
|
buckets[temp].addLast(arr[i]);
|
||||||
|
}
|
||||||
|
if (!isContinue) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//从桶中取出放到arr中,注意以什么顺序放进去的就要以什么顺序取出来(先进先出)
|
||||||
|
int index = 0;
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
Integer item;
|
||||||
|
while ((item = buckets[i].pollFirst()) != null) {
|
||||||
|
arr[index++] = item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//位数+1
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Integer[] arr = {4, 31, 1, 29, 5, 4, 2};
|
||||||
|
sort(arr);
|
||||||
|
System.out.println(Arrays.toString(arr));
|
||||||
|
}
|
||||||
|
}
|
@ -34,7 +34,7 @@ public class ShellSort {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
n1 /= 2;
|
n1 /= 2;
|
||||||
} while (n1 > 0);
|
} while (n1 >= 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -30,7 +30,7 @@ public class SimpleSelectSort {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Integer[] arr = {1, 65, 32, 12, 21};
|
Integer[] arr = {1, 65, 32, 12, 21};
|
||||||
ShellSort.sort(arr);
|
sort(arr);
|
||||||
System.out.println(Arrays.toString(arr));
|
System.out.println(Arrays.toString(arr));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user