JAVA小练习冒泡排序,选择排序和插入排序

冒泡:

点击(此处)折叠或打开

我们提供的服务有:网站制作、做网站、微信公众号开发、网站优化、网站认证、山阳ssl等。为上1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的山阳网站制作公司

  1. import java.util.Arrays;
  2. class arrayDemo4
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         int[] a={0,9,8,7,6,5,4,3,66,44,32,22};
  7.         int[] b=arraySort1(a);
  8.         System.out.println(Arrays.toString(b));
  9.     }
  10.     public static int[] arraySort1(int[] a){
  11.         for (int i=0; i
  12.         {
  13.             for (int j=0; j
  14.             {
  15.                 if (a[j]>a[j+1])
  16.                 {
  17.                     int t=a[j];
  18.                     a[j]=a[j+1];
  19.                     a[j+1]=t;
  20.                 }
  21.             }
  22.         }
  23.         return a;
  24.     }
  25. }


选择:

点击(此处)折叠或打开

  1. import java.util.Arrays;
  2. class arrayDemo3
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         int[] a={6,4,6,2,1,5,7,2,3,6,7,3,1};
  7.         int[] b=arraySort(a);
  8.         System.out.println(Arrays.toString(b));
  9.     }
  10.     public static int[] arraySort(int[] a){
  11.             for (int i=0; i
  12.         {
  13.             for (int j=i+1; j
  14.             {
  15.                 if (a[i]>a[j])
  16.                 {
  17.                     int t=a[i];
  18.                     a[i]=a[j];
  19.                     a[j]=t;
  20.                 }
  21.             }
  22.         }
  23.         return a;
  24.     }

插入排序

点击(此处)折叠或打开

  1. import java.util.Arrays;
  2. class arrayDemo6
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         int[] a={12,45,135,2,464,9,454,21,12423};
  7.         System.out.println(Arrays.toString(insertSort1(a)));
  8.     }
  9.     public static int[] insertSort1(int[] a){
  10.         int t,j;
  11.         for (int i=1;i
  12.         {
  13.             t=a[i];
  14.             for (j=i-1;j>=0;j--)
  15.             {
  16.                 if (t
  17.                 {
  18.                     a[j+1]=a[j];
  19.                 }else
  20.                     break;
  21.             }
  22.         a[j+1]=t;
  23.         }
  24.         return a;
  25.     }
  26. }
优化后的插入排序:

点击(此处)折叠或打开

  1. import java.util.Arrays;
  2. class arrayDemo7
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         int[] a={12,45,135,2,464,9,454,21,12423};
  7.         System.out.println(Arrays.toString(insertSort1(a)));
  8.     }
  9.     public static int[] insertSort1(int[] a){
  10.         int t,j;
  11.         for (int i=1;i
  12.         {
  13.             t=a[i];
  14.             for (j=i-1;j>=0&&t
  15.             {
  16.                     a[j+1]=a[j];
  17.             }
  18.         a[j+1]=t;
  19.         }
  20.         return a;
  21.     }
  22. }





新闻标题:JAVA小练习冒泡排序,选择排序和插入排序
当前链接:http://azwzsj.com/article/iphiis.html