- 上次资源分享,获取数组最小值有点问题,现已修正如下
- 获取数组最大值
- class Demo
- {
- public static void main(String[] args)
- {
- int[] arr={1,2,3,8,6,9};
- int max =getMax(arr);
- System.out.println("max="+max);
- }
- public static int getMax(int[] arr)
- {
- int max=arr[0];
- for(int x=1;x<arr.length;x++)
- {
- if(arr[x]>max)
- max=arr[x];
- }
- return max;
- }
-
- }
- 获取数组最小值
- class Demo
- {
- public static int getMin(int[] arr)
- {
- int min = arr[0];
- for(int x = 1;x<arr.length;x++)
- {
- if(arr[x]<min)
-
- min = arr[x];
-
- }
- return min;
- }
- public static void main(String[] args)
- {
- int[] arr={7,2,8,0,9,3};
- int min = getMin(arr);
- System.out.println("min="+min);
- }
-
- }
- 选择排序
- class Demo
- {
- public static void selectSort(int[] arr)
- {
- for(int x=0;x<arr.length-1;x++)
- {
- for(int y=x+1;y<arr.length;y++)
- {
- if(arr[x]>arr[y])
- {
- int temp=arr[x];
- arr[x]=arr[y];
- arr[y]=temp;
-
- }
-
- }
- }
-
- }
- public static void printArray(int[] arr)
- {
- System.out.print("[");
- for(int x=0;x<arr.length;x++)
- {
- if(x!=arr.length-1)
- System.out.print(arr[x]+", ");
- else
- System.out.println(arr[x]+"]");
-
- }
-
-
- }
- public static void main(String[] args)
- {
- int[] arr={5,2,8,1,9,3};
- printArray(arr);
- selectSort(arr);
- printArray(arr);
- }
- 冒泡排序
- class Demo
- {
- public static void bubbleSort(int[] arr)
- {
- for(int x=0;x<arr.length-1;x++)
- {
- for(int y=0;y<arr.length-x-1;y++)
- {
- if(arr[y]>arr[y+1])
- {
- int temp=arr[y];
- arr[y]=arr[y+1];
- arr[y+1]=temp;
- }
- }
- }
- }
- public static void printArray(int[] arr)
- {
- System.out.print("[");
- for(int x=0;x<arr.length;x++)
- {
- if(x!=arr.length-1)
- System.out.print(arr[x]+", ");
- else
- System.out.println(arr[x]+"]");
-
- }
- }
- public static void main(String[] args)
- {
- int[] arr={5,2,8,1,9,3};
- printArray(arr);
- bubbleSort(arr);
- printArray(arr);
- }
复制代码 |
|