A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 蓝枫 中级黑马   /  2014-3-17 09:38  /  638 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. 打印数组最大值
  2. class Demo
  3. {
  4.         public static int getMax(int[] arr)
  5.         {
  6.                 int max =0;
  7.                 for(int x= 0;x<arr.length;x++)
  8.                 {
  9.                         if(arr[x]>max)
  10.                                 max=arr[x];
  11.                 }
  12.                 return max;
  13.         }


  14.         public static void main(String[]args)
  15.         {
  16.                 int[] arr={5,2,8,1,9,3};
  17.                 int max = getMax(arr);
  18.                 System.out.println("max="+max);
  19.         }
  20. }



  21. 打印数组最小值
  22. class Demo
  23. {
  24.         public static int getMin(int[] arr)
  25.         {
  26.                 int min = 0;
  27.                 for(int x = 0;x<arr.length;x++)
  28.                 {
  29.                         if(arr[x]<min)
  30.                         min = arr[x];
  31.                 }
  32.                 return min;
  33.         }
  34.         public static void main(String[] args)
  35.         {
  36.                 int[] arr={5,2,8,1,9,3};
  37.                 int min = getMin(arr);
  38.                 System.out.println("min="+min);
  39.         }
  40.        
  41. }



  42. 选择排序
  43. class Demo
  44. {
  45.         public static void selectSort(int[] arr)
  46.         {
  47.                 for(int x =0; x<arr.length-1;x++)
  48.                 {
  49.                         for(int y=x+1;y<arr.length;y++)
  50.                         {
  51.                                 if(arr[x]>arr[y])
  52.                                 {
  53.                                         int temp = arr[x];
  54.                                         arr[x] = arr[y];
  55.                                         arr[y]=temp;
  56.                                 }
  57.                         }
  58.                 }
  59.         }

  60.         public static void printArray(int[] arr)
  61.         {
  62.                 System.out.print("[");
  63.                 for(int x =0;x<arr.length;x++)
  64.                 {
  65.                         if(x!=arr.length-1)
  66.                                 System.out.print(arr[x]+",");
  67.                         else
  68.                                 System.out.println(arr[x]+"]");
  69.                 }
  70.         }
  71.         public static void main(String[] args)
  72.         {
  73.                 int[] arr={5,2,8,1,9,3};
  74.                 printArray(arr);
  75.                 selectSort(arr);
  76.                 printArray(arr);
  77.         }
  78. }



  79. 冒泡排序
  80. class Demo
  81. {
  82.         public static void bubbleSort(int[] arr)
  83.         {
  84.                 for(int x=0;x<arr.length-1;x++)
  85.                 {
  86.                         for(int y=0;y<arr.length-x-1;y++)
  87.                         {
  88.                                 if(arr[y]>arr[y+1])
  89.                                 {
  90.                                         int temp = arr[y];
  91.                                         arr[y] = arr[y+1];
  92.                                         arr[y+1]=temp;
  93.                                 }
  94.                         }
  95.                 }
  96.         }
  97.         public static void printArray(int[] arr)
  98.         {
  99.                 System.out.print("[");
  100.                 for(int x =0;x<arr.length;x++)
  101.                 {
  102.                         if(x!=arr.length-1)
  103.                                 System.out.print(arr[x]+",");
  104.                         else
  105.                                 System.out.println(arr[x]+"]");
  106.                 }
  107.         }
  108.     public static void main(String[] args)
  109.         {
  110.                 int[] arr={5,2,8,1,9,3};
  111.                 printArray(arr);
  112.                 bubbleSort(arr);
  113.                 printArray(arr);
  114.         }

  115. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

2 个回复

倒序浏览
帮助到了我们这些初学者  谢谢 以后又不懂的 请多指教
回复 使用道具 举报
嗯 看过了  !!辛苦了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马