import java.util.*;
/**
* Describe:排序的相关知识
* User:lenovo
* Date:2023-01-29
* Time:15:59
*/
class Sort {
//基数排序,这里我们只考虑正数的情况。
public static void radixSort(int[] array) {
int max = array[0];
for (int i = 0; i < array.length; i++) {
if(max < array[i]) {
max = array[i];
}
}
int maxPlace = 1;
while(!(max <=9 && max >= 0)) {
max /= 10;
maxPlace++;
}
Queue<Integer> q0 = new LinkedList<>();
Queue<Integer> q1 = new LinkedList<>();
Queue<Integer> q2 = new LinkedList<>();
Queue<Integer> q3 = new LinkedList<>();
Queue<Integer> q4 = new LinkedList<>();
Queue<Integer> q5 = new LinkedList<>();
Queue<Integer> q6 = new LinkedList<>();
Queue<Integer> q7 = new LinkedList<>();
Queue<Integer> q8 = new LinkedList<>();
Queue<Integer> q9 = new LinkedList<>();
int place = 1;
while(place <= maxPlace) {
for (int i = 0; i < array.length; i++) {
int key = array[i] % (int)(Math.pow(10, place));
int tmpPlace = place;
while(--tmpPlace > 0) {
key = key / 10;
}
switch (key) {
case 0:
q0.offer(array[i]);
break;
case 1:
q1.offer(array[i]);
break;
case 2:
q2.offer(array[i]);
break;
case 3:
q3.offer(array[i]);
break;
case 4:
q4.offer(array[i]);
break;
case 5:
q5.offer(array[i]);
break;
case 6:
q6.offer(array[i]);
break;
case 7:
q7.offer(array[i]);
break;
case 8:
q8.offer(array[i]);
break;
case 9:
q9.offer(array[i]);
break;
}
}
int i = 0;
while(!q0.isEmpty()) {
array[i++] = q0.poll();
}
while(!q1.isEmpty()) {
array[i++] = q1.poll();
}
while(!q2.isEmpty()) {
array[i++] = q2.poll();
}
while(!q3.isEmpty()) {
array[i++] = q3.poll();
}
while(!q4.isEmpty()) {
array[i++] = q4.poll();
}
while(!q5.isEmpty()) {
array[i++] = q5.poll();
}
while(!q6.isEmpty()) {
array[i++] = q6.poll();
}
while(!q7.isEmpty()) {
array[i++] = q7.poll();
}
while(!q8.isEmpty()) {
array[i++] = q8.poll();
}
while(!q9.isEmpty()) {
array[i++] = q9.poll();
}
place++;
}
}
//归并排序
public static void mergeSort(int[] array) {
mergeSortFunc(array, 0, array.length - 1);
}
private static void mergeSortFunc(int[] array, int left, int right) {
if(left >= right) {
return;
}
int mid = (left + right) / 2;
mergeSortFunc(array, left, mid);
mergeSortFunc(array, mid + 1, right);
merge(array, left, right, mid);
}
private static void merge(int[] array, int start, int end, int mid) {
int s1 = start;
int s2 = mid + 1;
int[] ret = new int[end - start + 1];
int i = 0;
while(s1 <= mid && s2 <= end) {
if(array[s1] < array[s2]) {
ret[i++] = array[s1++];
}else {
ret[i++] = array[s2++];
}
}
while(s1 <= mid) {
ret[i++] = array[s1++];
}
while(s2 <= end) {
ret[i++] = array[s2++];
}
for (int j = 0; j < ret.length; j++) {
array[j+start] = ret[j];
}
}
//归并排序的非递归方法。
public static void mergeSort1(int[] array) {
int gap = 1;
while(gap < array.length) {
for (int i = 0; i < array.length; i += gap * 2) {
int left = i;
int mid = left + gap - 1;
if(mid >= array.length) {
mid = array.length - 1;
}
int right = mid + gap;
if (right >= array.length) {
right = array.length - 1;
}
merge(array, left, right, mid);
}
gap *= 2;
}
}
//快速排序的非递归方法
public static void quickSortNonR(int[] array, int left, int right) {
Stack<Integer> s = new Stack<>();
s.push(left);
s.push(right);
while(!s.isEmpty()) {
if(left >= right) {
return;
}
right = s.pop();
left = s.pop();
if(right - left <= 14) {
insertSort(array, left, right);
continue;
}
midThree(array, left, right);
int key = partition1(array, left, right);
s.push(left);
s.push(key - 1);
s.push(key + 1);
s.push(right);
}
}
//快速排序
public static void quickSort(int[] array) {
quick(array, 0, array.length - 1);
}
private static void quick(int[] array, int start, int end) {
//为什么取大于号,如:1,2,3,4,5,6
if(start >= end) {
return;
}
//为了优化,我们在剩余元素不多的情况下,我们可以直接插入排序(这是数组基本有序,复杂度不会很大)
if(end - start + 1 <= 14) {
//直接插入排序
insertSort(array, start, end);
return;
}
//为了优化采取三数取中法
midThree(array, start, end);
//int key = partition1(array, start, end);//划分的关键元素的下标
//int key = partition2(array, start, end);
int key = partition3(array, start, end);
quick(array, start, key - 1);
quick(array, key + 1, end);
}
//为了优化快速排序,我们采取三数取中法
private static void midThree(int[] array, int left, int right) {
int key;
int mid = (left + right) / 2;
if(array[left] < array[right]) {
if(array[mid] < array[left]) {
key = left;
}else if(array[right] < array[mid]) {
key = right;
}else {
key = mid;
}
}else {
if(array[left] < array[mid]) {
key = left;
}else if(array[right] > array[mid]) {
key = right;
}else {
key = mid;
}
}
int tmp = array[left];
array[left] = array[key];
array[key] = tmp;
}
//前后指针法
//时间复杂度:最好情况下:O(N*logN)
// 最坏情况下:O(N^2)
//空间复杂度:O(1)
private static int partition3(int[] array, int left, int right) {
int prev = left;
int cur = left + 1;
while(cur <= right) {
while(array[cur] < array[left] && ++prev != cur) {
int tmp = array[prev];
array[prev] = array[cur];
array[cur] = tmp;
}
cur++;
}