本文给出了八大排序算法的java实现:
代码如下:
package test;
import java.util.Arrays;
public class sort_collection {
public static void main(String[] args) {
int[] arr1 = {5, 3, 6, 10, 2, 8, 1, 7, 12, 4, 1, 20, 5, 9};
int[] arr2 = {5, 3, 6, 10, 2, 8, 1, 7, 12, 4, 1, 20, 5, 9};
int[] arr3 = {5, 3, 6, 10, 2, 8, 1, 7, 12, 4, 1, 20, 5, 9};
int[] arr4 = {5, 3, 6, 10, 2, 8, 1, 7, 12, 4, 1, 20, 5, 9};
int[] arr5 = {5, 3, 6, 10, 2, 8, 1, 7, 12, 4, 1, 20, 5, 9};
int[] arr6 = {5, 3, 6, 10, 2, 8, 1, 7, 12, 4, 1, 20, 5, 9};
int[] arr7 = {5, 3, 6, 10, 2, 8, 1, 7, 12, 4, 1, 20, 5, 9};
int[] arr8 = {5, 3, 6, 10, 2, 8, 1, 7, 12, 4, 1, 20, 5, 9};
System.out.println("初始数组: " + Arrays.toString(arr1));
straight_insert_sort(arr1);
System.out.println("初始数组: " + Arrays.toString(arr2));
binary_insert_sort(arr2);
System.out.println("初始数组: " + Arrays.toString(arr3));
shell_sort(arr3);
System.out.println("初始数组: " + Arrays.toString(arr4));
bubble_sort(arr4);
System.out.println("初始数组: " + Arrays.toString(arr5));
quick_sort(arr5);
System.out.println("初始数组: " + Arrays.toString(arr6));
simple_select_sort(arr6);
System.out.println("初始数组: " + Arrays.toString(arr7));
heap_sort(arr7);
System.out.println("初始数组: " + Arrays.toString(arr8));
merge_sort(arr8);
}
//直接插入排序
public static void straight_insert_sort(int[] arr) {
int tmp;
for(int i = 1; i < arr.length; i++) {
tmp = arr[i];
int j;
for(j = i - 1; j >= 0 && tmp < arr[j]; j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = tmp;
}
System.out.println("直接插入排序结果: " + Arrays.toString(arr));
}
//二分插入排序
public static void binary_insert_sort(int[] arr) {
for(int i = 1; i < arr.length; i++) {
if(arr[i] < arr[i - 1]) {
int tmp = arr[i];
int low = 0;
int high = i - 1;
while(low < high) {
int middle = (low + high) / 2;
if(arr[middle] > arr[i])
high = middle - 1;
else
low = middle + 1;
}
for(int j = i; j > low; j--) {
arr[j] = arr[j - 1];
}
arr[low] = tmp;
}
}
System.out.println("二分插入排序结果: " + Arrays.toString(arr));
}
//希尔排序,分部直接插入排序,最后基本有序
public static void shell_sort(int[] arr) {
for(int d = arr.length / 2; d > 0; d /= 2) { //步长初始为len/2,之后步长减半直到为1
for(int i = d; i < arr.length; i++) {
int tmp = arr[i];
int j;
for(j = i - d; j >= 0 && tmp < arr[j]; j -= d) {
arr[j + d] = arr[j];
}
arr[j + d] = tmp;
}
}
System.out.println("希尔排序结果: " + Arrays.toString(arr));
}
//冒泡排序
public static void bubble_sort(int[] arr) {
for(int i = 1; i < arr.length; i++) { //一共只需要冒泡len - 1次
for(int j = 0; j < arr.length - 1; j++) {
if(arr[j] > arr[j + 1]) { //将大的元素一直往后移动
int tmp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = tmp;
}
}
}
System.out.println("冒泡排序结果: " + Arrays.toString(arr));
}
//快速排序
public static void quick_sort(int[] arr) {
pre_quick_sort(arr, 0, arr.length - 1);
System.out.println("快速排序结果: " + Arrays.toString(arr));
}
private static void pre_quick_sort(int[] arr, int left, int right) {
int i = left;
int j = right;
int privot = arr[left];
while(i < j) {
while(i < j && arr[j] >= privot) //从左开始,如大于等于基准值,则下标一直向左移
j--;
if(arr[j] < privot) { //交换基准值和左边遇到比基准小的值
int tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
while(i < j && arr[i] < privot) //从右开始。。。
i++;
if(arr[i] > privot) { //交换。。。
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
if(i > left)
pre_quick_sort(arr, left, i - 1);
if(j < right)
pre_quick_sort(arr, j + 1, right);
}
//简单选择排序,每次从n-i+1开始选择选最小值插入到i处
public static void simple_select_sort(int[] arr) {
int min;
for(int i = 0; i < arr.length - 1; i++) { //只需要选择len-1次
min = i; //记录最小的下标值
for(int j = i + 1; j < arr.length; j++) {
if(arr[j] < arr[min])
min = j;
}
if(min != i) {
int tmp = arr[i];
arr[i] = arr[min];
arr[min] = tmp;
}
}
System.out.println("简单选择排序结果: " + Arrays.toString(arr));
}
//堆排序,大顶堆
public static void heap_sort(int[] arr) {
int n = arr.length - 1;
build_max_heap(arr);
for(int i = n; i >= 0; i--) {
int tmp = arr[0];
arr[0] = arr[i];
arr[i] = tmp;
adjust_max_heap(arr, 0, i);
}
System.out.println("堆排序结果: " + Arrays.toString(arr));
}
//建立初始大顶堆
private static void build_max_heap(int[] arr) {
int n = arr.length;
for(int i = n / 2; i >= 0; i--) {
adjust_max_heap(arr, i, n);
}
}
//取出最大数后调整大顶堆
private static void adjust_max_heap(int[] arr, int index, int len) {
int tmp = arr[index];
for(int i = 2 * index + 1; i < len - 1; i = 2 * i + 1) {
if(i < len && arr[i] < arr[i + 1])
i++;
if(tmp >= arr[i]) //如果此时i节点的值大于了两个子节点的最大值,不操作
break;
else {
arr[index] = arr[i]; //交换之后后面可能会改变,继续调整
index = i;
}
}
arr[index] = tmp; //将调整节点的值放入最终的位置
}
//归并排序
public static void merge_sort(int[] arr) {
sort(arr, 0, arr.length - 1);
System.out.println("归并排序结果: " + Arrays.toString(arr));
}
private static void sort(int[] arr, int left, int right) {
int middle = (left + right) / 2;
if(left < right) {
sort(arr, left, middle); //左边
sort(arr, middle + 1, right); //右边
merge(arr, left, middle, right); //左右归并
}
}
private static void merge(int[] arr, int left, int middle, int right) {
int[] tmp = new int[arr.length];
int i = left;
int j = middle + 1;
int k = left;
while(i <= middle && j <= right) {
if(arr[i] < arr[j]) //取小的放入临时数组,指针后移
tmp[k++] = arr[i++];
else
tmp[k++] = arr[j++];
}
while(i <= middle) //将剩余的数组直接放入后面,两个while至多执行其中的一个
tmp[k++] = arr[i++];
while(j <= right)
tmp[k++] = arr[j++];
while(left <= right) { //将临时数组复制到arr中
arr[left] = tmp[left];
left++;
}
}
}