//直接插入排序
public int[] insertSort(int[] a) {
for (int i = 1; i < a.length; i++) {
int j = i - 1;
for (; j >= 0 && a[j] > a[i]; j--) {
a[j + 1] = a[j];//往后移
}
a[j + 1] = a[i];
}
return a;
}
//希尔排序
public int[] shellSort(int[] a) {
int n = a.length;
int gap = n;//数组数
while (gap > 0) {
gap = gap / 2;//将数组缩小一半
for (int x = 0; x < gap; x++) {//将每组数进行直接插入排序
//直接插入排序
for (int i = x + gap; i < n; i += gap) {
int j = i - gap;
for (; j >= 0 && a[j] > a[i]; j -= gap) {
a[j + gap] = a[j];
}
a[j + gap] = a[i];
}
}
}
return a;
}
//简单选择排序
public int[] simpleSort(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
if (a[j] < a[i]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}
//堆排序
public void heapSort(int[] a) {
System.out.println("开始排序");
int arrayLength = a.length;
//循环建堆,每次把最大值放在最后,不参与下一次的建堆
for (int i = 0; i < arrayLength - 1; i++) {
//建堆
buildMaxHeap(a, arrayLength - 1 - i);
//交换堆顶和最后一个元素
swap(a, 0, arrayLength - 1 - i);
System.out.println(Arrays.toString(a));
}
}
private void swap(int[] data, int i, int j) {
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
//对data数组从0到lastIndex建大顶堆
private void buildMaxHeap(int[] data, int lastIndex) {
//从lastIndex处节点(最后一个节点)的父节点开始,(lastIndex - 1) / 2为节点数
for (int i = (lastIndex - 1) / 2; i >= 0; i--) {
//k保存正在判断的节点
int k = i;
//如果当前k节点的子节点存在,把堆中最大的数放在对顶
while (k * 2 + 1 <= lastIndex) {
//k节点的左子节点的索引
int biggerIndex = 2 * k + 1;
//如果biggerIndex小于lastIndex,即biggerIndex+1代表的k节点的右子节点存在
if (biggerIndex < lastIndex) {
//若果右子节点的值较大
if (data[biggerIndex] < data[biggerIndex + 1]) {
//biggerIndex总是记录较大子节点的索引
biggerIndex++;
}
}
//如果k节点的值小于其较大的子节点的值
if (data[k] < data[biggerIndex]) {
//交换他们
swap(data, k, biggerIndex);
//将biggerIndex赋予k,开始while循环的下一次循环,重新保证k节点的值大于其左右子节点的值
k = biggerIndex;
} else {
break;
}
}
}
}
//冒泡排序
public int[] bubbleSort(int[] a) {
int temp = 0;
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
return a;
}
//快速排序
public void quickSort() {
quick(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
public int getMiddle(int[] list, int low, int high) {
int tmp = list[low]; //数组的第一个作为中轴
while (low < high) {
while (low < high && list[high] >= tmp) {
high--;
}
list[low] = list[high]; //比中轴小的记录移到低端
while (low < high && list[low] <= tmp) {
low++;
}
list[high] = list[low]; //比中轴大的记录移到高端
}
list[low] = tmp; //中轴记录到尾
return low; //返回中轴的位置
}
public void _quickSort(int[] list, int low, int high) {
if (low < high) {
int middle = getMiddle(list, low, high); //将list数组进行一分为二
_quickSort(list, low, middle - 1); //对低字表进行递归排序
_quickSort(list, middle + 1, high); //对高字表进行递归排序
}
}
public void quick(int[] a2) {
if (a2.length > 0) { //查看数组是否为空
_quickSort(a2, 0, a2.length - 1);
}
}
//归并排序
public void mergeSort(int[] arr) {
int[] temp = new int[arr.length];
internalMergeSort(arr, temp, 0, arr.length - 1);
}
private void internalMergeSort(int[] a, int[] b, int left, int right) {
//当left==right的时,已经不需要再划分了
if (left < right) {
int middle = (left + right) / 2;
internalMergeSort(a, b, left, middle);//左子数组
internalMergeSort(a, b, middle + 1, right);//右子数组
mergeSortedArray(a, b, left, middle, right);//合并两个子数组
}
}
// 合并两个有序子序列 arr[left, ..., middle] 和 arr[middle+1, ..., right],temp是辅助数组。
private void mergeSortedArray(int arr[], int temp[], int left, int middle, int right) {
int i = left;
int j = middle + 1;
int k = 0;
//从两个数组中取出最小的放入临时数组
while (i <= middle && j <= right) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
//将左数组剩下的数据复制到临时数组
while (i <= middle) {
temp[k++] = arr[i++];
}
//将右数组剩下的数据复制到临时数组
while (j <= right) {
temp[k++] = arr[j++];
}
//把数据复制回原数组
for (i = 0; i < k; ++i) {
arr[left + i] = temp[i];
}
}
//基数排序
public void radixSort() {
sort(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
public void sort(int[] array) {
//首先确定排序的趟数;
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
int time = 0;
//判断位数;
while (max > 0) {
max /= 10;
time++;
}
//建立10个队列;
List queue = new ArrayList();
for (int i = 0; i < 10; i++) {
ArrayList queue1 = new ArrayList();
queue.add(queue1);
}
//进行time次分配和收集;
for (int i = 0; i < time; i++) {
//分配数组元素;
for (int j = 0; j < array.length; j++) {
//得到数字的第time+1位数;
int x = array[j] % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i);
ArrayList queue2 = queue.get(x);
queue2.add(array[j]);
queue.set(x, queue2);
}
int count = 0;//元素计数器;
//收集队列元素;
for (int k = 0; k < 10; k++) {
while (queue.get(k).size() > 0) {
ArrayList queue3 = queue.get(k);
array[count] = queue3.get(0);
queue3.remove(0);
count++;
}
}
}
}
原文链接: Java常用排序算法/程序员必须掌握的8大排序算法