JAVA小練習氣泡排序,選擇排序和插入排序

kisslfcr發表於2018-01-02
冒泡:

點選(此處)摺疊或開啟

  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<a.length-1;i++ )
  12.         {
  13.             for (int j=0; j<a.length-i-1; 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<a.length-1;i++ )
  12.         {
  13.             for (int j=i+1; j<a.length;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<a.length;i++ )
  12.         {
  13.             t=a[i];
  14.             for (j=i-1;j>=0;j--)
  15.             {
  16.                 if (t<a[j])
  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<a.length;i++ )
  12.         {
  13.             t=a[i];
  14.             for (j=i-1;j>=0&&t<a[j];j--)
  15.             {
  16.                     a[j+1]=a[j];
  17.             }
  18.         a[j+1]=t;
  19.         }
  20.         return a;
  21.     }
  22. }




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29135257/viewspace-2149608/,如需轉載,請註明出處,否則將追究法律責任。

相關文章