排序,就是使一串记录按照其中某个或某些关键字的大小,递增或递减的排列起来的操作。通常指的是排升序。
直接插入排序:每次选择无序区间的第一个元素,在有序区间内选择合适的位置插入
插入排序的优化:在有序区间选择数据应该插入的位置时,因为区间的有序性,可以利用折半查找的思想
// 1.插入排序
public static void insertSort(int[] array) {
for (int i = 1; i < array.length; i++) {
int tmp = array[i]; //无序区间的第一个数
int j = i - 1;
for (; j >= 0; j--) {
if (array[j] > tmp) {
array[j + 1] = array[j];
} else {
break;
}
}
array[j + 1] = tmp;
}
}
// 折半建议了解
时间复杂度O(n²)
空间复杂度O(1)
稳定性:稳定
代码实现如下:
// 2.希尔排序
public static void shell(int[] array, int gap) {
for (int i = gap; i < array.length; i++) {
int tmp = array[i];
int j = i - gap;
for (; j >= 0; j = j - gap) {
if (array[j] > tmp) {
array[j + gap] = array[j];
} else {
break;
}
}
array[j + gap] = tmp;
}
}
public static void shellSort(int[] array) {
int[] drr = {
5, 3, 1};
for (int i = 0; i < drr.length; i++) {
shell(array, drr[i]);
}
}
// 3.选择排序
public static void selectSort(int[] array) {
for (int i = 1; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
}
// 4.堆排序
// 创建大堆
public static void createHeap(int[] array) {
for (int i = (array.length - 1) / 2; i >= 0; i--) {
adjustDown(array, array.length, i);
}
}
// 向上调整
public static void adjustDown(int[] array, int len, int parent) {
int leftChild = 2 * parent + 1;
while (leftChild < len) {
int maxChild = leftChild;
int rightChild = 2 * parent + 2;
if (rightChild < len) {
if (array[rightChild] > array[leftChild]) {
maxChild = rightChild;
}
}
if (array[parent] >= array[maxChild]) {
break;
}
int tmp = array[parent];
array[parent] = array[maxChild];
array[maxChild] = tmp;
parent = maxChild;
leftChild = 2 * parent + 1;
}
}
// 交换数值
public static void swap(int[] array, int i, int j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
public static void heapSort(int[] array) {
createHeap(array);
for (int i = 0; i < array.length - 1; i++) {
swap(array, 0, array.length - i - 1);
adjustDown(array, array.length - i - 1, 0);
}
}
// 5.冒泡排序
public static void bubbleSort1(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
}
}
}
// 优化后的冒泡排序
public static void bubbleSort2(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
boolean flag = false;
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
flag = true;
}
}
if (flag = false) {
break;
}
}
}
// 6.快排
public static int partion(int[] array, int left, int right) {
int tmp = array[left];
while (left < right) {
while (left < right && array[right] >= tmp) {
right--;
}
if (left >= right) {
array[left] = tmp;
break;
} else {
array[left] = array[right];
}
while (left < right && array[left] <= tmp) {
left++;
}
if (left >= right) {
array[right] = tmp;
break;
} else {
array[right] = array[left];
}
}
return left;
}
public static void quick(int[] array, int low, int hight) {
if (low >= hight) {
return;
}
int par = partion(array, low, hight);
quick(array, low, par - 1);
quick(array, par + 1, hight);
}
public static void quickSort(int[] array) {
quick(array, 0, array.length - 1);
}
// 快排优化1
public static void quick2(int[] array, int low, int hight) {
if (low >= hight) {
return;
}
// 快排优化2
if (hight - low + 1 < 100) {
insertSort2(array, low, hight);
return;
}
// 快排优化1
int mid = (low + hight) >>> 1;
medianOfThree(array, low, mid, hight);
int par = partion(array, low, hight);
quick(array, low, par - 1);
quick(array, par + 1, hight);
}
public static void insertSort2(int[] array, int start, int end) {
for (int i = start; i <= end; i++) {
int tmp = array[i];
int j = i - 1;
for (; j >= start; j--) {
if (array[j] > tmp) {
array[j + 1] = array[j];
} else {
break;
}
}
array[j + 1] = tmp;
}
}
选择基准值很重要,通常用几个数取中法
1. 选择左边或者右边
2. 随机选取
3. 几个数取中法
partition过程中把和基准值相等的数也选择出来
待排序区间小于一个阈值时,使用直接插入排序
// 7.归并排序
public static void merge(int[] array, int left, int mid, int right) {
int s1 = left;
int e1 = mid;
int s2 = mid + 1;
int e2 = right;
int[] tmpArray = new int[right - left + 1];
int k = 0;
while (s1 <= e1 && s2 <= e2) {
if (array[s1] <= array[s2]) {
tmpArray[k++] = array[s1++];
} else {
tmpArray[k++] = array[s2++];
}
}
while (s1 <= e1) {
tmpArray[k++] = array[s1++];
}
while (s2 <= e2) {
tmpArray[k++] = array[s2++];
}
for (int i = 0; i < tmpArray.length; i++) {
array[i + left] = tmpArray[i];
}
}
public static void mergeSortRec(int[] array, int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
mergeSortRec(array, left, mid);
mergeSortRec(array, mid + 1, right);
merge(array, left, mid, right);
}
public static void mergeSort(int[] array) {
mergeSortRec(array, 0, array.length - 1);
}