java新手筆記7

來源:互聯網
上載者:User

標籤:

1.最小數

// 1.0public class SortDemo1 {     public static void main(String[] args) {   int[] a = {2,5,4,6,8,1,3,9};       int min = a[0];//最小數   int k = 0;   int temp;   for(int i = 0; i < a.length; i++ ) {             System.out.print( a[i] + "\t");  }   //最小數       for(int j = 0; j < a.length; j++ ) {             if( min > a[j]) {                 k = j; min = a[j]; }   }   temp = a[0];   a[0] = a[k];   a[k] = temp;         System.out.println("\n最小值 : " + min);  System.out.println("索引 : " + k);  System.out.println("a[5] : " + a[k]);  System.out.println("a[0] : " + a[0]);  for(int i = 0; i < a.length; i++ ) {             System.out.print( a[i] + "\t");  }        }}

 2.數組最小數

// 1.1public class SortDemo2 {     public static void main(String[] args) {   int[] a = {0,2,5,2,5,0};       int min = a[0];//最小數   int k = 0;   int temp;   System.out.print( "數組資料 : ");   for(int i = 0; i < a.length; i++ ) {             System.out.print( a[i] + "\t");  }   //最小數      for(int i = 0; i < a.length - 1; i++ ) { k = i; //預設當前的最小值      //從剩下的元素找最小值       for(int j = i; j < a.length; j++ ) {//內層的起始值             if( a[k] > a[j]) {//a[k]值是預設最小值                 k = j;                          }                                                       }          //資料交換  如果當前值最小 不進行交換 if(k != i) {   temp = a[i];//  0   0  k == i   a[i] = a[k];  // System.out.print(".temp = " + temp +  "  :  a[k] = " + a[k]  +  "  : a[i] = " + a[i]);   a[k] = temp;             }   /*   System.out.print( "\n");       for(int x = 0; x < a.length; x++ ) {             System.out.print( a[x] + "\t");  }  */ }    System.out.print( "\n");   System.out.print( "數組資料 : ");  for(int i = 0; i < a.length; i++ ) {             System.out.print( a[i] + "\t");  }        }}

 3.排序,大數後置

// 1.1public class SortDemo3 {     public static void main(String[] args) {   int[] a = {7,5,6,3,2,1,4,9,8,6};         int temp;   System.out.print( "數組資料 : ");   for(int i = 0; i < a.length; i++ ) {             System.out.print( a[i] + "\t");                                                     }   //相鄰的元素比較  8       for(int i = 0; i < a.length - 1; i++ ) {   //每次找最大數放在最後       for(int j = 0; j < a.length - 1 - i; j++ ) { // 0 - 3                if( a[j] > a[j+1] ) { //交換temp = a[j];a[j] = a[j+1];a[j+1] = temp;       }                                                }                                                }                        System.out.print( "\n");   System.out.print( "數組資料 : ");  for(int i = 0; i < a.length; i++ ) {                      System.out.print( a[i] + "\t");  }        }}

 

java新手筆記7

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.