排序
自定义类型怎么进行比较的?
自定义类型进行比较时,重写object的equals方法
自定义比较大小,重写compareto方法
1.插入排序
/**
* 稳定性:稳定
* 时间复杂度:最好情况下:O(n) 最坏情况下:O(n^2)
* 空间复杂度:O(1)
*
* @param array
*/
//插入排序
public static void insertSort(int[] array) {
for (int i = 1; i < array.length; i++) {
int temp = array[i];
int j = i - 1;
for (; j >= 0; j--) {
if (array[j] > temp) {
array[j + 1] = array[j];
} else {
break;
}
}
array[j + 1] = temp;
}
}
2.希尔排序
/**
* 稳定性:
* 时间复杂度:O(n^1.3)-O(n^1.5)
* 空间复杂度:O(1)
*/
//希尔排序
public static void shell(int[] array, int gap) {
for (int i = gap; i < array.length; i++) {
int temp = array[i];
int j = i - gap;
for (; j >= 0; j = j - gap) {
if (array[j] > temp) {
array[j + gap] = array[j];
} else {
break;
}
}
array[j + gap] = temp;
}
}
public static void shellSort(int[] array) {
int[] drr = {5, 3, 1};
for (int i = 0; i < drr.length; i++) {
shell(array, drr[i]);
}
}
3.选择排序
/**
* 稳定性:不稳定的排序
* 时间复杂度:O(n^2)
* 空间复杂度:O(1)
*/
//选择排序
public static void selectSort(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
int temp = array[i];
if (array[j] < array[i]) {
array[i] = array[j];
array[j] = temp;
}
}
}
}
4.堆排序
/**
* 时间复杂度:O(n*log2n)
* 空间复杂度:O(1)
* 稳定性:不稳定的排序
*/
//堆排序
public static void adjustDown(int[] array, int parent, int len) {
int child = 2 * parent + 1;
while (child < len) {
if (child + 1 < len && array[child] < array[child + 1]) {
child++;
}
if (array[child] > array[parent]) {
int temp = array[child];
array[child] = array[parent];
array[parent] = temp;
parent = child;
child = 2 * parent + 1;
} else {
break;
}
}
}
public static void createHeap(int[] array) {
for (int i = (array.length - 1 - 1) / 2; i >= 0; i--) {
adjustDown(array,i,array.length);
}
}
public static void heapSort(int[] array) {
createHeap(array);
int end = array.length - 1;
while (end > 0) {
int temp = array[0];
array[0] = array[end];
array[end] = temp;
adjustDown(array,0,end);
end--;
}
}
5.快速排序
/**
* 时间复杂度:O(n*log2n)
* 最坏:有序O(n^2)
* 空间复杂度:O(log2n)
* 稳定性:不稳定
*/
//快速排序递归
public static int partition(int[] array, int start, int end) {
int temp = array[start];
while (start < end) {
while (start < end && array[end] >= temp) {
end--;
}
if (start >= end) {
array[start] = temp;
break;
} else {
array[start] = array[end];
}
while (start < end && array[start] <= temp) {
start++;
}
if (start >= end) {
array[start] = temp;
break;
} else {
array[end] = array[start];
}
}
return start;
}
public static void insertSort(int[] array, int low, int high) {
for (int i = low + 1; i < high; i++) {
int temp = array[i];
int j = i - 1;
for (; j >= 0; j--) {
if (array[j] > temp) {
array[j + 1] = array[j];
} else {
break;
}
}
array[j + 1] = temp;
}
}
public static void quick(int[] array, int low, int high) {
if (low >= high) {
return;
}
medianOfThree(array, low, high);
int par = partition(array, low, high);
quick(array, low, par - 1);
quick(array, par + 1, high);
}
//三数取中法
public static void medianOfThree(int[] array, int low, int high) {
int mid = (low + high) / 2;
//array[mid] < array[low] < array[high]
if (array[low] >= array[high]) {
swap(array, low, high);
}
if (array[low] <= array[high]) {
swap(array, low, mid);
}
if (array[mid] >= array[high]) {
swap(array, mid, high);
}
}
public static void swap(int[] array, int low, int high) {
int temp = array[low];
array[low] = array[high];
array[high] = temp;
}
public static void quickSort(int[] array) {
//quick(array, 0, array.length - 1);
quickNor(array, 0, array.length - 1);
}
/**
* @param array
* @param low
* @param high
*/
//非递归快速排序
public static void quickNor(int[] array, int low, int high) {
Stack<Integer> stack = new Stack<>();
int start = 0;
int end = array.length - 1;
int par = partition(array, start, end);
}
6.归并排序
/**
* 时间复杂度:O(n*log2n)
* 空间复杂度:O(n)
* 稳定性:稳定
*
* @param array
* @param low
* @param high
* @param mid
*/
//归并排序
public static void merge(int[] array, int low, int high, int mid) {
int s1 = low;
int s2 = mid + 1;
int[] tempArray = new int[high - low + 1];
int k = 0;
while (s1 <= mid && s2 <= high) {
if (array[s1] <= array[s2]) {
tempArray[k++] = array[s1++];
} else {
tempArray[k++] = array[s2++];
}
}
while (s1 <= mid) {
tempArray[k++] = array[s1++];
}
while (s2 <= mid) {
tempArray[k++] = array[s2++];
}
for (int i = 0; i < tempArray.length; i++) {
array[low + i] = tempArray[i];
}
}
public static void mergeSortInternal(int[] array, int low, int high) {
if (low >= high) {
return;
}
int mid = (low + high) / 2;
mergeSortInternal(array, low, mid);
mergeSortInternal(array, mid + 1, high);
merge(array,low,high,mid);
}
public static void mergeSort(int[] array) {
mergeSortInternal(array, 0, array.length - 1);
}
/**
* 非递归的归并排序
*
* @param array
* @param gap
*/
public static void mergeNor(int[] array, int gap) {
int len = array.length;
int s1 = 0;
int e1 = s1 + gap - 1;
int s2 = e1 + 1;
int e2 = s2 + gap - 1 < len ? s2 + gap - 1 : len - 1;
int[] tempArr = new int[len];
int k = 0;
//先判断是否有两个归并段
while (s2 < len) {
while (s1 <= e1 && s2 <= e2) {
if (array[s1] <= array[s2]) {
tempArr[k] = array[s1];
k++;
s1++;
} else {
tempArr[k] = array[s2];
k++;
s2++;
}
}
while (s1 <= e1) {
tempArr[k] = array[s1];
k++;
s1++;
}
while (s2 <= e2) {
tempArr[k] = array[s2];
k++;
s2++;
}
s1 = e2 + 1;
e1 = s1 + gap - 1;
s2 = e1 + 1;
e2 = s2 + gap - 1 < len ? s2 + gap - 1 : len - 1;
}
//不够两个归并段
while (s1 <= len - 1) {
tempArr[k] = array[s1];
k++;
s1++;
}
for (int i = 0; i < array.length; i++) {
array[i] = tempArr[i];
}
}
public static void mergeNorSort(int[] array) {
for (int i = 0; i < array.length; i *= 2) {
mergeNor(array, i);
}
}