- import java.util.Arrays;
- public class SortDemo {
- public static void main(String[] args) {
- int[] arr = new int[]{5,4,2,1,5,6};
-
- //选择排序
- // for (int i = 0; i < arr.length-1; i++) {
- // for (int j = i+1; j < arr.length - i; j++) {
- // if(arr[i] > arr[j]) {
- // int temp = arr[i];
- // arr[i] = arr[j];
- // arr[j] = temp;
- // }
- // }
- // }
- //
- //冒泡排序
- 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;
- }
- }
- }
-
- System.out.println(Arrays.toString(arr));
- }
- }
复制代码
|
|