1.选择排序
代码:
int[] arr = {1,8,6,3,9,5,4,7,11};
int[] brr = {1,8,6,3,9,5,4,7,11};
int[] crr = {1,8,6,3,9,5,4,7,11};
int[] drr = {1,8,6,3,9,5,4,7,11};
System.out.println("==================选择排序(左小右大)============================");
System.out.println("方法1.");
for(int i=0; i
int c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
}
System.out.print("外循环打印第"+(i+1)+"次:");
for(int ii=0;ii
}
System.out.println("");
}
System.out.println("方法2.");
for(int i=brr.length-1; i > 0; i--){
for(int j=i-1; j>0; j--){
if(brr[i] < brr[j]){
int c = brr[i];
brr[i] = brr[j];
brr[j] = c;
}
}
System.out.print("外循环打印第"+(brr.length-i)+"次:");
for(int ii=0;ii
}
System.out.println("");
}
System.out.println("==================选择排序后(左小右大)============================");
System.out.println("==================选择排序后(左大右小)============================");
System.out.println("方法1.");
for(int i=0; i
int c = crr[i];
crr[i] = crr[j];
crr[j] = c;
}
}
System.out.print("外循环打印第"+(i+1)+"次:");
for(int ii=0;ii
}
System.out.println("");
}
System.out.println("方法2.");
for(int i=drr.length-1; i > 0; i--){
for(int j=i-1; j>=0; j--){
if(drr[i] > drr[j]){
int c = drr[i];
drr[i] = drr[j];
drr[j] = c;
}
}
System.out.print("外循环打印第"+(brr.length-i)+"次:");
for(int ii=0;ii
}
System.out.println("");
}
System.out.println("==================选择排序后(左大右小)============================");
打印结果:
2.冒泡排序
代码:
int[] arr = {1,8,6,3,9,5,4,7,11};
int[] brr = {1,8,6,3,9,5,4,7,11};
int[] crr = {1,8,6,3,9,5,4,7,11};
int[] drr = {1,8,6,3,9,5,4,7,11};
System.out.println("==================冒泡排序(左小右大)============================");//升序排序
System.out.println("方法1.");
for(int i=0; i
if(arr[j-1] > arr[j]){
int c = arr[j-1];
arr[j-1] = arr[j];
arr[j] = c;
}
}
System.out.print("外循环打印第"+(i+1)+"次:");
for(int ii=0;ii
}
System.out.println("");
}
System.out.println("方法2.");
for(int i=brr.length-1; i > 0; i--){
for(int j=0; j if(brr[j+1] < brr[j]){
int c = brr[j+1];
brr[j+1] = brr[j];
brr[j] = c;
}
}
System.out.print("外循环打印第"+(brr.length-i)+"次:");
for(int ii=0;ii
}
System.out.println("");
}
System.out.println("==================冒泡排序后(左小右大)============================");
System.out.println("==================冒泡排序后(左大右小)============================");//降序排序
System.out.println("方法1.");
for(int i=0; i
if(arr[j-1] < arr[j]){
int c = arr[j-1];
arr[j-1] = arr[j];
arr[j] = c;
}
}
System.out.print("外循环打印第"+(i+1)+"次:");
for(int ii=0;ii
}
System.out.println("");
}
System.out.println("方法2.");
for(int i=brr.length-1; i > 0; i--){
for(int j=0; j if(brr[j] < brr[j+1]){
int c = brr[j+1];
vbrr[j+1] = brr[j];
brr[j] = c;
}
}
System.out.print("外循环打印第"+(brr.length-i)+"次:");
for(int ii=0;ii
}
System.out.println("");
}
System.out.println("==================冒泡排序后(左大右小)============================");
打印结果: