//冒泡排序法
public class MyDemo4{
public static void main(String [] args){
int [] scores = {8,4,2,1,23,344,12};
for ( int row=0;row<scores.length-1;row++) {
for ( int col=0;col<scores.length-1-row;col++ ) {
if (scores[col]<scores[col+1]) {
int temp = scores[col];
scores[col] = scores[col+1];
scores[col+1] = temp;
}
}
}
for (int i=0;i<scores.length;i++){
System.out.println(scores[i]+"\t");
}
}
}
//选择排序法
public class MyDemo4{
public static void main(String [] args){
int [] scores = {8,4,2,1,23,344,12};
for ( int row=0;row<scores.length-1;row++) {
int last = scores.length-1-row; // 最后坐标
int max = 0;//初始时默认第一个是最高的
//第一个数与第二个数开始比较
for ( int col=1;col<scores.length-row;col++) {
//如果第一个数小于第二个数,将第二个数的值赋给第一个数
if ( scores[max] < scores[col] ) {
max = col;
}
}
//一轮结束,两数位置交换
//如果最大值的坐标不等于最后一个值的坐标则进行交换
if ( max!=last) {
//数组中最大值与数组最后一个值的位置进行互换
//定义一个空容器,将最大值的坐标赋给容器
int temp=scores[max];
//将最后一个数值的坐标赋值给最大值的坐标
scores[max] = scores[last];
//将temp中的坐标赋值给最后一个数值的坐标
scores[last] = temp;
}
}
for (int i=0;i<scores.length;i++){
System.out.println(scores[i]+"\t");
}
}
}
/ 插入排序法
public class MyDemo1{
public static void main(String [] args){
int [] scores = {8,4,2,1,23,344,12};
for ( int i=0;i<scores.length-1;i++ ) {
int curr = scores[i+1];//获取要进行判断的数字
for ( int j=i;j>=0;j--) {
//如果当前值小于比较的数
if ( curr<scores[j]) {
//将比较的数向后移一位
scores[j+1] = scores[j];
}
//否则退出循环
else {
break;
}
//退出循环后,将当前值写到后移一位数值的原位置
scores[j] = curr;
}
}
for ( int i=0;i<scores.length;i++) {
System.out.print(scores[i]+"\t");
}
}
}
//希尔排序法
public class MyDemo2{
public static void main(String [] args){
int [] scores = {8,4,2,1,23,344,12};
//分组插入
//首先计算步长 scores.length/2; 步长为3:[8、1、12] 、 [4、23] 、 [2、344]
//小组排序[1、8、12]、[4、23]、[2、344]====>[1、4、2、8、23、344、12]
//重新计算新步长 原步长/2 1===>
// 计算步长 每次步长缩短一半 直到步长为一 就结束
for ( int jump=scores.length/2;jump>0;jump/=2) {
//从每组第2个数开始(多少组取决于步长)
for ( int i=jump;i<scores.length;i++) {
//取出每组第2个数
int value = scores[i];
//循环 小组中数据排序
int j;
for ( j=i-jump;j>=0 && scores[j]>value;j-=jump) {
scores[j+jump] = scores[j];
}
scores[j+jump] = value;
}
}
for ( int i=0;i<scores.length;i++) {
System.out.print(scores[i]+"\t");
}
}
}