黑马程序员技术交流社区

标题: 基础程序代码 [打印本页]

作者: 蓝枫    时间: 2014-3-17 09:38
标题: 基础程序代码
  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. }
复制代码

作者: 学习代码    时间: 2014-3-17 10:24
帮助到了我们这些初学者  谢谢 以后又不懂的 请多指教
作者: 2528870651    时间: 2014-3-17 11:54
嗯 看过了  !!辛苦了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2