import java.util.Arrays;
public class Sort {
private static int[] init = new int[10];
static {
init[0] = 86;
init[1] = 95;
init[2] = 72;
init[3] = 23;
init[4] = 39;
init[5] = 41;
init[6] = 55;
init[7] = 11;
init[8] = 68;
init[9] = 75;
}
public static int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 };
public static int Index = a.length;// 数据索引变量
public static int temp ;
/**
* 冒泡排序
*/
public static void sort1(){
printBeforeSort(true);
for (int i = 0;i < a.length; i++) {
for (int j = i+1;j<a.length;j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printAfterSort(true);
}
/**
* hash排序
*/
public static void sort2() {
printBeforeSort(true);
int j, k; // 循环计数变量
int Temp; // 暂存变量
boolean Change; // 数据是否改变
int DataLength; // 分割集合的间隔长度
int Pointer; // 进行处理的位置
DataLength = (int) Index / 2; // 初始集合间隔长度
while (DataLength != 0) // 数列仍可进行分割
{
System.out.println("");
// 对各个集合进行处理
for (j = DataLength; j < Index; j++) {
Change = false;
Temp = a[j]; // 暂存Data[j]的值,待交换值时用
Pointer = j - DataLength; // 计算进行处理的位置
// 进行集合内数值的比较与交换值
while (Temp < a[Pointer] && Pointer >= 0 && Pointer <= Index) {
a[Pointer + DataLength] = a[Pointer];
// 计算下一个欲进行处理的位置
Pointer = Pointer - DataLength;
Change = true;
if (Pointer < 0 || Pointer > Index)
break;
} // 与最后的数值交换
a[Pointer + DataLength] = Temp;
// 打印排序结果
if (Change) {
System.out.print("排序中: ");
for (k = 0; k < Index; k++)
System.out.printf("%3s ", a[k]);
System.out.println("");
}
}
DataLength = DataLength / 2;
// 计算下次分割的间隔长度
}
}
/**
* 选择排序
*/
public static void sort3(){
printBeforeSort(true);
for (int i = 0;i < a.length;i++) {
int temp1 = i ;
for (int j = i + 1 ;j < a.length;j++) {
if( a[j] < a[temp1]) {
temp1 = j;
}
}
temp = a[i] ;
a[i] = a[temp1];
a[temp1] = temp;
}
printAfterSort(true);
}
public static void sort4(){
printBeforeSort(true);
Arrays.sort(a);
printAfterSort(true);
}
public static void main(String[] args) {
//sort1();
//sort2();
//sort3();
sort4();
}
public static void printBeforeSort(boolean b){
if(b){
System.out.print("排序前:");
for (int n : a) {
System.out.printf("%3s ",n);
}
System.out.println("");
}
}
public static void printAfterSort(boolean b){
if(b){
System.out.print("排序后:");
for (int n : a) {
System.out.printf("%3s ",n);
}
System.out.println("");
}
}
}