常见排序算法
/**
* 快排
* */
private static void deepSort(int[] input, int start, int end) {
boolean turnFlag = false;
int standIndex = start;
int left = start, right = end;
if (left < right) {
while (left < right){
if (turnFlag) {
if (input[standIndex] < input[left]) {
int temp = input[left];
input[left] = input[standIndex];
input[standIndex] = temp;
standIndex = left;
turnFlag = false;
continue;
}
++left;
} else {
if (input[standIndex] > input[right]) {
int temp = input[right];
input[right] = input[standIndex];
input[standIndex] = temp;
standIndex = right;
turnFlag = true;
continue;
}
--right;
}
}
deepSort(input, start, standIndex - 1);
deepSort(input, standIndex + 1, end);
}
}
/**
* 希尔排序
* */
public static void sortShell(int[] input){
int len = input.length;
for(int group = len / 2; group > 0; group /= 2) {
for (int i = group; i < len; ++i) {
for (int j = i; j >= group; j -= group) {
if (input[j] < input[j - group]) {
int temp = input[j];
input[j] = input[j - group];
input[j - group] = temp;
}
}
}
}
}
/**
* 选择
* */
public static void sortX(int[] in) {
int len = in.length;
for(int i = 0;i <len; ++i){
int index = 0;
for (int j = 0; j < len - i; ++j) {
if (in[index] < in[j]) {
index = j;
}
}
int endIndex = len - (i + 1);
int temp = in[endIndex];
in[endIndex] = in[index];
in[index] = temp;
}
}
/**
* 冒泡
* */
public static void sortM(int[] in){
int len = in.length;
for(int i =0;i <len; ++i){
for (int j = i; j <len; ++j) {
if (in[i] >= in[j]) {
int temp = in[i];
in[i] = in[j];
in[j] = temp;
}
}
}
}
本文作者:bokerr
本文链接:https://www.cnblogs.com/bokers/p/16061762.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步