操作方法
public class MaoPao { public static void main(String[] args) { int[] line = {24,36,12,25,3,67,55}; maopao(line) } //冒泡排序法 public static void maopao(int[] line){ for(int i=0;i<line.length-1;i++){//最多做line.length-1趟排序 for(int j=i;j<line.length;j++){ int temp = 0; if(line[i]>line[j]){ temp = line[i]; line[i] = line[j]; line[j] = temp; } for(int k =0;k<line.length;k++){ System.out.print(line[k]+","); } System.out.print("\n"); } System.out.print("第" + (i + 1) + "次排序结果:"); for(int z =0;z<line.length;z++){ System.out.print(line[z]+","); } System.out.print("\n"); } System.out.print("最终排序结果:"); for(int n =0;n<line.length;n++){ System.out.print(line[n]+" "); } } }
24,36,12,25,3,67,55,24,36,12,25,3,67,55,12,36,24,25,3,67,55,12,36,24,25,3,67,55,3,36,24,25,12,67,55,3,36,24,25,12,67,55,3,36,24,25,12,67,55,第1次排序结果:3,36,24,25,12,67,55,3,36,24,25,12,67,55,3,24,36,25,12,67,55,3,24,36,25,12,67,55,3,12,36,25,24,67,55,3,12,36,25,24,67,55,3,12,36,25,24,67,55,第2次排序结果:3,12,36,25,24,67,55,3,12,36,25,24,67,55,3,12,25,36,24,67,55,3,12,24,36,25,67,55,3,12,24,36,25,67,55,3,12,24,36,25,67,55,第3次排序结果:3,12,24,36,25,67,55,3,12,24,36,25,67,55,3,12,24,25,36,67,55,3,12,24,25,36,67,55,3,12,24,25,36,67,55,第4次排序结果:3,12,24,25,36,67,55,3,12,24,25,36,67,55,3,12,24,25,36,67,55,3,12,24,25,36,67,55,第5次排序结果:3,12,24,25,36,67,55,3,12,24,25,36,67,55,3,12,24,25,36,55,67,第6次排序结果:3,12,24,25,36,55,67,最终排序结果:3 12 24 25 36 55 67
public class SelectSort { public static void main(String[] args) { int[] line = {23,17,5,8,1,25,33}; selectSort(line); } //思路:在每步中选择最小数来重新排列 public static void selectSort(int[] line){ for(int i=0;i<line.length;i++){//进行line.length-1次比较 for(int j=i;j<line.length;j++){//把每次剩下的数中选择最小的数放在最 //前面的位置 if(line[i]>line[j]){ int temp = line[i]; line[i] = line[j]; line[j] = temp; } for(int k=0;k<line.length;k++){ System.out.print(line[k]+","); } System.out.print("\n"); } System.out.println("第"+(i+1)+"次排序结果:"); for(int z=0;z<line.length;z++){ System.out.print(line[z]+" "); } System.out.print("\n"); } System.out.println("最终结果:"); for(int n=0;n<line.length;n++){ System.out.print(line[n]+","); } }}
23,17,5,8,1,25,33,17,23,5,8,1,25,33,5,23,17,8,1,25,33,5,23,17,8,1,25,33,1,23,17,8,5,25,33,1,23,17,8,5,25,33,1,23,17,8,5,25,33,第1次排序结果:1 23 17 8 5 25 33 1,23,17,8,5,25,33,1,17,23,8,5,25,33,1,8,23,17,5,25,33,1,5,23,17,8,25,33,1,5,23,17,8,25,33,1,5,23,17,8,25,33,第2次排序结果:1 5 23 17 8 25 33 1,5,23,17,8,25,33,1,5,17,23,8,25,33,1,5,8,23,17,25,33,1,5,8,23,17,25,33,1,5,8,23,17,25,33,第3次排序结果:1 5 8 23 17 25 33 1,5,8,23,17,25,33,1,5,8,17,23,25,33,1,5,8,17,23,25,33,1,5,8,17,23,25,33,第4次排序结果:1 5 8 17 23 25 33 1,5,8,17,23,25,33,1,5,8,17,23,25,33,1,5,8,17,23,25,33,第5次排序结果:1 5 8 17 23 25 33 1,5,8,17,23,25,33,1,5,8,17,23,25,33,第6次排序结果:1 5 8 17 23 25 33 1,5,8,17,23,25,33,第7次排序结果:1 5 8 17 23 25 33 最终结果:1,5,8,17,23,25,33,
public class InsertSort { public static void main(String[] args) { int[] line = {34,27,15,17,28,2,33,5,64}; insertSort(line); } //对未排序的数据执行插入到合适位置进行排序 public static void insertSort(int[] line){ int i, j, k; for(i = 1; i < line.length; i++) { for(j = i; j > 0; j--) { int data; if(line[j-1] > line[j]) { data = line[j]; line[j] = line[j-1]; line[j-1] = data; } for(k=0;k<line.length;k++){ System.out.print(line[k]+","); } System.out.print("\n"); } System.out.println("第"+(i)+"次排序结果:"); for(k=0;k<line.length;k++){ System.out.print(line[k]+" "); } System.out.print("\n"); } System.out.println("最后排序结果:"); for(int n=0;n<line.length;n++){ System.out.print(line[n]+" "); } }}
27,34,15,17,28,2,33,5,64,第1次排序结果:27 34 15 17 28 2 33 5 64 27,15,34,17,28,2,33,5,64,15,27,34,17,28,2,33,5,64,第2次排序结果:15 27 34 17 28 2 33 5 64 15,27,17,34,28,2,33,5,64,15,17,27,34,28,2,33,5,64,15,17,27,34,28,2,33,5,64,第3次排序结果:15 17 27 34 28 2 33 5 64 15,17,27,28,34,2,33,5,64,15,17,27,28,34,2,33,5,64,15,17,27,28,34,2,33,5,64,15,17,27,28,34,2,33,5,64,第4次排序结果:15 17 27 28 34 2 33 5 64 15,17,27,28,2,34,33,5,64,15,17,27,2,28,34,33,5,64,15,17,2,27,28,34,33,5,64,15,2,17,27,28,34,33,5,64,2,15,17,27,28,34,33,5,64,第5次排序结果:2 15 17 27 28 34 33 5 64 2,15,17,27,28,33,34,5,64,2,15,17,27,28,33,34,5,64,2,15,17,27,28,33,34,5,64,2,15,17,27,28,33,34,5,64,2,15,17,27,28,33,34,5,64,2,15,17,27,28,33,34,5,64,第6次排序结果:2 15 17 27 28 33 34 5 64 2,15,17,27,28,33,5,34,64,2,15,17,27,28,5,33,34,64,2,15,17,27,5,28,33,34,64,2,15,17,5,27,28,33,34,64,2,15,5,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,第7次排序结果:2 5 15 17 27 28 33 34 64 2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,第8次排序结果:2 5 15 17 27 28 33 34 64 最后排序结果:2 5 15 17 27 28 33 34 64