快速排序:
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
int i, j, temp, t;
if (low > high) {
return;
}
i = low;
j = high;
//temp就是基准位
temp = arr[low];
while (i < j) {
//先看右边,依次往左递减
while (temp <= arr[j] && i < j) {
//arr[j]一定要等到小于基准数时,才跳出,这样才能放到左边
j--;
}
//再看左边,依次往右递增
while (temp >= arr[i] && i < j) {
//arr[i]一定要等到大于基准数时,才跳出,这样才能放到右边
i++;
}
//如果满足条件则交换
if (i < j) {
t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
//最后将基准为与i和j相等位置的数字交换
arr[low] = arr[i];
arr[i] = temp;
//递归调用左半数组
quickSort(arr, low, j - 1);
//递归调用右半数组
quickSort(arr, j + 1, high);
}
public static void main(String[] args) {
int[] arr = {10, 7, 2, 4, 7, 62, 3, 4, 2, 1, 8, 9, 19};
quickSort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+” “);
}
}
}
堆排序:
public class HeapSort {
//大顶堆,从小到大排序
public static void heapSort(int[] arr) {
int len = arr.length;
for (int i = len / 2 - 1; i >= 0; i--) {
heapAdjust(arr, len, i);
}
for (int i = len - 1; i >= 1; i--) {
int temp = arr[i];
arr[i] = arr[0];
arr[0] = temp;
heapAdjust(arr, i, 0);
}
}
//从节点i到节点len,从上向下调整
public static void heapAdjust(int[] arr, int len, int i) {
//k是父亲,index是儿子
int k = i, index = 2 * k + 1, temp = arr[i];
//arr[len]已经是在范围内最大的了,已经在合适的位置了
while (index < len) {
if (index + 1 < len && arr[index + 1] > arr[index]) {
index = index + 1;
}
if (arr[index] > temp) {
arr[k] = arr[index];
k = index;//儿子取代父亲k
index = 2 * k + 1;//父亲k,生出新的儿子index
} else {
//temp一直在寻找合适的,大于等于的index,然后作index的父亲k。
break;
}
}
arr[k] = temp;
}
public static void main(String[] args) {
int[] arr = new int[]{16, 7, 3, 20, 17, 8};
heapSort(arr);
for (int a : arr) {
System.out.print(a + " ");
}
}
}
归并排序:
public class MergeSort {
public static void merge(int[] arr, int low, int mid, int high, int[] temp) {
int i = 0, j = low, k = mid + 1;
while (j <= mid && k <= high) {
if (arr[j] < arr[k]) {
temp[i++] = arr[j++];
} else {
temp[i++] = arr[k++];
}
}
while (j <= mid) {
temp[i++] = arr[j++];
}
while (k <= high) {
temp[i++] = arr[k++];
}
for (int t = 0; t < i; t++) {
arr[low + t] = temp[t];
}
}
public static void mergeSort(int[] arr, int low, int high, int[] temp) {
if (low < high) {
int mid = (low + high) / 2;
mergeSort(arr, low, mid, temp);
mergeSort(arr, mid + 1, high, temp);
merge(arr, low, mid, high, temp);
}
}
public static void main(String[] args) {
int[] arr = {11, 44, 23, 67, 88, 65, 34, 48, 9, 12};
int[] tmp = new int[arr.length]; //新建一个临时数组存放
mergeSort(arr, 0, arr.length - 1, tmp);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
插入排序:
待排序的数组,分成已排序的数组,和未排序的数组。
排序过程,类似于打扑克牌,整理手上的排。
public class InsertSort {
private int[] array;
private int length;
public InsertSort(int[] array) {
this.array = array;
this.length = array.length;
}
public void display() {
for (int a : array) {
System.out.print(a + " ");
}
System.out.println();
}
public void doInsertSort() {
for (int index = 1; index < length; index++) {
int temp = array[index];//当前要插入的数据,
int leftindex = index - 1;
while (leftindex >= 0 && array[leftindex] > temp) {
array[leftindex + 1] = array[leftindex];//左边的数据,从前往后挪动了位置
leftindex--;
}
array[leftindex + 1] = temp;//把temp放到空位上
}
}
public static void main(String[] args) {
int[] array = {38, 65, 97, 76, 13, 27, 49};
InsertSort is = new InsertSort(array);
System.out.println("排序前的数据为:");
is.display();
is.doInsertSort();
System.out.println("排序后的数据为:");
is.display();
}
}