Java常用的排序算法
1、插入排序
public static void insertionSort(int[] data) {
for (int i = 1; i < data.length; i++) {
for (int j = i; j > 0; j--) {
if (data[j] < data[j - 1]) {
int temp = data[j - 1];
data[j - 1] = data[j];
data[j] = temp;
}
}
}
}
2、希尔排序
public static void shellSort(int[] data) {
for (int step = data.length / 2; step > 0; step /= 2) {
for (int i = step; i < data.length; i++) {
int value = data[i];
int j;
for (j = i - step; j >= 0 && data[j] > value; j -= step) {
data[j + step] = data[j];
}
data[j + step] = value;
}
}
}
public static void selectSort(int[] data) {
int temp = 0;
for (int i = 0; i < data.length - 1; i++) {
int index = i;
for (int j = i + 1; j < data.length; j++) {
if (data[index] > data[j]) {
index = j;
}
}
if (index != i) {
temp = data[i];
data[i] = data[index];
data[index] = temp;
}
}
}
4、堆排序
public static void heapSort(int[] data) {
int i;
int len = data.length;
for (i = len / 2 - 1; i >= 0; i--) {
heap(data, i, len);
}
for (i = len - 1; i >= 0; i--) {
int temp = data[0];
data[0] = data[i];
data[i] = temp;
heap(data, 0, i);
}
}
private static void heap(int[] data, int pos, int len) {
int child = 2 * pos + 1;
if (child + 1 < len && data[child] < data[child + 1]) {
child++;
}
if (child < len && data[pos] < data[child]) {
int temp = data[pos];
data[pos] = data[child];
data[child] = temp;
heap(data, child, len);
}
}
5、冒泡排序
public static void bubbleSort(int[] data) {
int temp = 0;
for (int i = 0; i < data.length - 1; i++) {
for (int j = 0; j < data.length - 1 - i; j++) {
if (data[j] > data[j + 1]) {
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
}
6、快速排序
public static void quickSort(int[] data, int low, int high) {
int pivot, pos, temp;
if (low < high) {
pos = low;
pivot = data[pos];
for (int i = low + 1; i <= high; i++) {
if (data[i] < pivot) {
pos++;
temp = data[pos];
data[pos] = data[i];
data[i] = temp;
}
}
temp = data[low];
data[low] = data[pos];
data[pos] = temp;
quickSort(data, low, pos - 1);
quickSort(data, pos + 1, high);
}
}
public static void mergeSort(int[] data, int low, int high) {
int mid = (low + high) / 2;
if (low < high) {
mergeSort(data, low, mid);
mergeSort(data, mid + 1, high);
merge(data, low, mid, high);
}
}
private static void merge(int[] data, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int i = low;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= high) {
if (data[i] < data[j]) {
temp[k++] = data[i++];
} else {
temp[k++] = data[j++];
}
}
while (i <= mid) {
temp[k++] = data[i++];
}
while (j <= high) {
temp[k++] = data[j++];
}
for (int x = 0; x < temp.length; x++) {
data[x + low] = temp[x];
}
}
8、基数排序
public static void basicSort(int[] data) {
int max = 0;
for (int i = 0; i < data.length; i++) {
if (data[i] > max) {
max = data[i];
}
}
int times = 0;
while (max > 0) {
max = max / 10;
times++;
}
List<List<Integer>> queen = new ArrayList<>();
for (int i = 0; i < 10; i++) {
List<Integer> q = new ArrayList<>();
queen.add(q);
}
for (int i = 0; i < times; i++) {
for (int j = 0; j < data.length; j++) {
int x = data[j] % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i);
List<Integer> q = queen.get(x);
q.add(data[j]);
}
}
int count = 0;
for (int z = 0; z < 10; z++) {
while (queen.get(z).size() > 0) {
List<Integer> c = queen.get(z);
data[count] = c.get(0);
c.remove(0);
count++;
}
}
}