常见的简单排序
public class PaiXu {
public static void main(String[] args) {
int [] arrays = {2,6,4,3,5,1};
// maoPao(arrays);
// xuanZe(arrays);
// chaRu(arrays);
System.out.println(Arrays.toString(arrays));
}
//maopao
private static void maoPao(int[] arrays){
int len = arrays.length;
for (int i = 0; i < len; i++) {
for (int j = 0; j < len-i-1; j++){
if (arrays[j]>arrays[j+1]){
int temp = arrays[j];
arrays[j] = arrays[j+1];
arrays[j+1] = temp;
}
}
}
}
//选择
private static void xuanZe(int[] arrays){
int len = arrays.length;
for (int i = 0; i < len; i++) {
int temp = arrays[i];
int tempIndex = i;
for (int j = i; j < len;j++){
if (arrays[j]<temp){
temp = arrays[j];
tempIndex = j;
}
}
arrays[tempIndex] = arrays[i];
arrays[i] = temp;
}
}
//插入排序:内层两个条件并列更适合while循环
private static void chaRu(int[] arrays){
int len = arrays.length;
for (int i = 1; i < len; i++) {
int temp = arrays[i];
int leftIndex = i-1;
while (leftIndex >= 0 && temp < arrays[leftIndex]){
arrays[leftIndex+1] = arrays[leftIndex];
leftIndex--;
}
arrays[leftIndex+1] = temp;
}
}
//快速排序:未完成需要递归
// private static void kuaiPai(int[] arrays){
// int i ,j ,temp,t;
// if (arrays==null){
// return;
// }
// temp = arrays[0];
// i = 0;
// j = arrays.length-1;
// while (i<j){
// while(temp<=arrays[j]&&i<j){
// j--;
// }
// while(temp>=arrays[i]&&i<j){
// i++;
// }
// if (i<j){
// t = arrays[j];
// arrays[j] = arrays[i];
// arrays[i] = t;
// }
// }
// arrays[0] = arrays[i];
// arrays[i] = temp;
//
// }
}