`
shifulong
  • 浏览: 56532 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

排序篇:bubble

阅读更多
public static void bubbleSort() {
    int[] arr = {0, 5, 6, 333, 5, 8, 999, 7, 7, 5, 45, 3};
    for (int i = arr.length - 1; i > 0; i--) {
        for (int j = arr.length - 1; j > arr.length - i - 1; j--) {
            if (arr[j] < arr[j - 1]) {
                swap(arr, j, j - 1);
            }
        }
    }
    System.out.println(Arrays.toString(arr));
}

public static void swap(int arr[], int j, int i) {
    int tmp = arr[j];
    arr[j] = arr[i];
    arr[i] = tmp;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics