国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

數(shù)組排序(冒泡法)

2019-11-14 14:53:07
字體:
供稿:網(wǎng)友

思路:定義了三個(gè)方法分別是sort、swap、PRintAry,分別執(zhí)行排序、數(shù)組倒置、打印數(shù)組的功能,定義倒置數(shù)組的目的呢,就是按照用戶的需要來打印出是升序還是降序的結(jié)果。

CODE:


import java.util.Scanner;

public class ArySort {

/**
* @param args
*/
//排序(降序)
  public static int[] sort(int[] ary){
    for (int i = 1; i < ary.length; i++) {
      for (int j = 0; j < ary.length-i; j++) {
        if(ary[j] < ary[j+1]){
          int temp = ary[j];
          ary[j] = ary[j+1];
          ary[j+1] = temp;
          }
        }
      }
       return ary;
   }

// 數(shù)組倒置
  public static int[] swap(int[] ary){
    for (int i = 0,j = ary.length-1; i < ary.length/2; i++,j--) {
      int temp = ary[i];
      ary[i] = ary[j];
      ary[j] = temp;
     }
  return ary;
  }
// 打印數(shù)組
  public static void printAry(int[] ary){
    for (int i = 0; i < ary.length; i++) {
      System.out.print(ary[i]+"/t");
     }
   }
  public static void main(String[] args) {
    Scanner ss = new Scanner(System.in);
    System.out.print("請(qǐng)輸入數(shù)組長(zhǎng)度:");
    int len = ss.nextInt();
     if(len < 0){
      System.out.println("數(shù)組長(zhǎng)度有誤!");
      return;
    }
    int[] ary = new int[len];
    for(int i = 0; i < ary.length;i++){
      System.out.println("請(qǐng)輸入第"+(i+1)+"數(shù)據(jù):");
      ary[i] = ss.nextInt();
    }
    System.out.println("排序前的數(shù)組為:");
    printAry(ary);
    sort(ary);
    System.out.println();
    System.out.println("1.升序, 2.降序");
    int a = ss.nextInt();
    if(a == 1){
      swap(ary);
    }
    System.out.println("排序后的數(shù)組為:");
    printAry(ary);
    System.out.println();
  }

}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 托里县| 太和县| 日土县| 东光县| 五家渠市| 乌拉特前旗| 泰安市| 苏尼特左旗| 芦山县| 利津县| 阿荣旗| 萍乡市| 惠东县| 沁阳市| 邵武市| 高州市| 临沭县| 讷河市| 嵩明县| 南澳县| 崇阳县| 金坛市| 商洛市| 富锦市| 日喀则市| 华坪县| 定结县| 青阳县| 湘阴县| 临洮县| 盘锦市| 齐河县| 来安县| 大冶市| 湖南省| 延庆县| 无为县| 恩平市| 峡江县| 五莲县| 新昌县|