一、今日学习内容
1、数字排序
(1)冒泡排序
1 import java.util.Arrays; 2 public class BubbleSortDemo { 3 public static void main(String[] args) { 4 int[] ary= {2,3,4,5,9,7,8}; 5 for(int i=0;i<ary.length-1;i++) { 6 for(int j=0;j<ary.length-i-1;j++) { 7 if(ary[j]>ary[j+1]) { 8 int t=ary[j];ary[j]=ary[j+1];ary[j+1]=t; 9 } 10 } 11 } 12 System.out.println("排序结果:"+Arrays.toString(ary)); 13 } 14 }
(2)选择排序
1 import java.util.Arrays; 2 public class SelectSortDemo { 3 public static void main(String[] args) { 4 int[] ary= {2,3,4,5,9,7,8}; 5 for(int i=0;i<ary.length-1;i++) { 6 for(int j=i+1;j<ary.length;j++) { 7 if(ary[i]>ary[j]) { 8 int t=ary[i];ary[i]=ary[j];ary[j]=t; 9 } 10 } 11 } 12 System.out.println("排序结果:"+Arrays.toString(ary)); 13 } 14 }
(3)插入排序
1 import java.util.Arrays; 2 public class InsertionSortDemo { 3 public static void main(String[] args) { 4 int[] ary= {2,3,4,5,9,7,8}; 5 int i,j,k; 6 for( i=1;i<ary.length;i++) { 7 k=ary[i]; 8 for( j=i-1;j>=0&&k<ary[i];j--) { 9 ary[j+1]=ary[j]; 10 } 11 ary[j+1]=k; 12 } 13 System.out.println("排序结果:"+Arrays.toString(ary)); 14 } 15 16 }
二、遇到的问题
对快速排序还不是很理解
三、明日计划
继续学习第三章的后两节算法,阅读大道至简