a
- public class T2 {
- public static void main(String[] args) {
- int[] arr = new int[] { 1, 41, 32, 32, 16, 14, 38, 9, 7 };
- //选择排序
- System.out.println("选择排序:");
- for (int i = 0; i < arr.length-1 ; i++) {
- for (int j = i+1; j < arr.length ; j++) {
- if (arr[i] > arr[j]) {
- int temp = arr[j];
- arr[j] = arr[i];
- arr[i] = temp;
- }
- }
- for (int x : arr)
- System.out.print(x + " ");
- System.out.println();
- }
- System.out.println("--------------------------------");
- arr = new int[] { 1, 41, 32, 32, 16, 14, 38, 9, 7 };
- System.out.println("冒泡排序:");
- for (int i = 0; i < arr.length - 1; i++) {
- for (int j = 0; j < arr.length - i - 1; j++) {
- if (arr[j] > arr[j + 1]) {
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- for (int x : arr)
- System.out.print(x + " ");
- System.out.println();
- }
- }
- }
复制代码
|
|