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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. 上次资源分享,获取数组最小值有点问题,现已修正如下
  2. 获取数组最大值
  3. class Demo
  4. {
  5.         public static void main(String[] args)
  6.         {
  7.                 int[] arr={1,2,3,8,6,9};
  8.                 int max =getMax(arr);

  9.                 System.out.println("max="+max);
  10.         }
  11.         public static int getMax(int[] arr)
  12.         {
  13.                 int max=arr[0];
  14.                 for(int x=1;x<arr.length;x++)
  15.                 {
  16.                         if(arr[x]>max)
  17.                                 max=arr[x];
  18.                 }
  19.                 return max;
  20.     }
  21.                
  22. }



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




  46. 选择排序
  47. class Demo
  48. {

  49. public static void selectSort(int[] arr)
  50.         {

  51. for(int x=0;x<arr.length-1;x++)
  52.                 {
  53.           for(int y=x+1;y<arr.length;y++)

  54.                         {
  55.                         if(arr[x]>arr[y])
  56.                                 {
  57.                              int temp=arr[x];
  58.                                  arr[x]=arr[y];
  59.                                  arr[y]=temp;
  60.                        
  61.                             }
  62.                        
  63.                         }
  64.         }

  65.      
  66.         }

  67. public static void printArray(int[] arr)
  68.         {
  69.                  System.out.print("[");
  70.          for(int x=0;x<arr.length;x++)
  71.                 {
  72.            if(x!=arr.length-1)
  73.               System.out.print(arr[x]+", ");
  74.            else
  75.               System.out.println(arr[x]+"]");

  76.         }


  77.     }

  78. public static void main(String[] args)
  79.         {
  80.       int[] arr={5,2,8,1,9,3};
  81.        printArray(arr);
  82.        selectSort(arr);
  83.        printArray(arr);

  84.         }




  85. 冒泡排序
  86. class Demo
  87. {

  88. public static void bubbleSort(int[] arr)
  89.         {

  90.           for(int x=0;x<arr.length-1;x++)
  91.                 {
  92.            for(int y=0;y<arr.length-x-1;y++)
  93.                         {

  94.                   if(arr[y]>arr[y+1])
  95.                                 {
  96.                              int temp=arr[y];
  97.                                  arr[y]=arr[y+1];
  98.                                  arr[y+1]=temp;
  99.                                 }
  100.             }
  101.                 }

  102.     }


  103. public static void printArray(int[] arr)
  104.         {
  105.                  System.out.print("[");
  106.          for(int x=0;x<arr.length;x++)
  107.                 {
  108.            if(x!=arr.length-1)
  109.               System.out.print(arr[x]+", ");
  110.            else
  111.               System.out.println(arr[x]+"]");

  112.         }


  113.    }


  114. public static void main(String[] args)
  115.         {
  116.       int[] arr={5,2,8,1,9,3};
  117.        printArray(arr);
  118.        bubbleSort(arr);
  119.        printArray(arr);

  120.         }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马