Java 排序及每一步过程

冒泡排序: 
1
public class MaoPao 2 { 3 public static void main(String args[]) 4 { 5 int score[]={67,89,87,69,90,100,75,90}; 6 for(int i=1;i<score.length;i++) 7 { 8 for(int j=0;j<score.length;j++) 9 { 10 if(score[i]<score[j]) 11 { 12 int temp=score[i]; 13 score[i]=score[j]; 14 score[j]=temp; 15 } 16 } 17 System.out.print("第"+i+"次排序的结果:\t"); 18 for(int j=0;j<score.length;j++) 19 { 20 System.out.print(score[j]+"\t"); 21 } 22 System.out.println("");// 换行 23 } 24 System.out.print("最终排序结果为:\t"); 25 for(int i=0;i<score.length;i++) 26 System.out.print(score[i]+"\t"); 27 } 28 } 29

 

 

 

结果:

 

 

 1 选择排序:
 2 
 3 public class XuanZePaiXu
 4 {
 5 public static void main(String args[])
 6 { 
 7 int score[]={67,89,87,69,90,100,75,90};
 8 for(int i=0;i<score.length-1;i++) 
 9 {
10 int min=i;
11 for(int j=i+1;j<score.length;j++) 
12 { 
13 if(score[min]>score[j])
14 {
15 min=j;
16 }
17 if(min!=i)
18 {
19 int temp=score[i];
20 score[i]=score[min];
21 score[min]=temp;
22 }
23 }
24 System.out.print("第"+i+"次排序的结果:\t");
25 for(int j=0;j<score.length;j++) 
26 { 
27 System.out.print(score[j]+"\t");
28 }
29 System.out.println("");// 换行
30 }
31 System.out.print("最终排序结果为:\t");
32 for(int i=0;i<score.length;i++) 
33 System.out.print(score[i]+"\t");
34 }
35 }

 

结果:

 

posted on 2019-03-28 22:45  ElisaBella  阅读(116)  评论(0)    收藏  举报

导航