<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Ytl's Java Blog

    厚積而薄發(fā)---每一天都是一個全新的開始

    Sorting algorithms --Bubble sort

    Posted on 2011-05-06 16:03 ytl 閱讀(573) 評論(0)  編輯  收藏

    Bubble Sort

    Bubble sort is a simple and well-known sorting algorithm. It is used in practice once in a blue moon and its main application is to make an introduction to the sorting algorithms. Bubble sort belongs to O(n2) sorting algorithms, which makes it quite inefficient for sorting large data volumes. Bubble sort is stable and adaptive.

    Algorithm

    1. Compare each pair of adjacent elements from the beginning of an array and, if they are in reversed order, swap them.
    2. If at least one swap has been done, repeat step 1.

    You can imagine that on every step big bubbles float to the surface and stay there. At the step, when no bubble moves, sorting stops. Let us see an example of sorting an array to make the idea of bubble sort clearer.

    Example. Sort {5, 1, 12, -5, 16} using bubble sort.

    Bubble sort example

    Complexity analysis

    Average and worst case complexity of bubble sort is O(n2). Also, it makes O(n2) swaps in the worst case. Bubble sort is adaptive. It means that for almost sorted array it gives O(n) estimation. Avoid implementations, which don't check if the array is already sorted on every step (any swaps made). This check is necessary, in order to preserve adaptive property.

    Turtles and rabbits

    One more problem of bubble sort is that its running time badly depends on the initial order of the elements. Big elements (rabbits) go up fast, while small ones (turtles) go down very slow. This problem is solved in the Cocktail sort.

    Turtle example. Thought, array {2, 3, 4, 5, 1} is almost sorted, it takes O(n2) iterations to sort an array. Element {1} is a turtle.

    Turtle example

    Rabbit example. Array {6, 1, 2, 3, 4, 5} is almost sorted too, but it takes O(n) iterations to sort it. Element {6} is a rabbit. This example demonstrates adaptive property of the bubble sort.

    Rabbit example

    Code snippets

    There are several ways to implement the bubble sort. Notice, that "swaps" check is absolutely necessary, in order to preserve adaptive property.

    Java

    public void bubbleSort(int[] arr) {

          boolean swapped = true;

          int j = 0;

          int tmp;

          while (swapped) {

                swapped = false;

                j++;

               for (int i = 0; i < arr.length - j; i++) {                               

                      if (arr[i] > arr[i + 1]) {                          

                            tmp = arr[i];

                            arr[i] = arr[i + 1];

                            arr[i + 1] = tmp;

                            swapped = true;

                      }

                }                

          }

    }

    Python

    def bubbleSort(L) :

        swapped = True;

        while swapped:

            swapped = False

            for i in range(len(L)-1):

                if L[i]>L[i+1]:

                    temp = L[i]

                    L[i] = L[i+1]

                    L[i+1] = temp

                    swapped = True

    主站蜘蛛池模板: 免费在线看黄网站| 成人电影在线免费观看| 中字幕视频在线永久在线观看免费 | 成人无码区免费视频观看| 久久久无码精品亚洲日韩京东传媒 | 成人免费视频77777| 亚洲一区二区三区在线| 国产福利在线观看免费第一福利| 亚洲人妖女同在线播放| 最近最新中文字幕完整版免费高清| 亚洲一区二区三区深夜天堂| 久久久久久99av无码免费网站| 亚洲熟女综合色一区二区三区| 色视频色露露永久免费观看| 欧美亚洲精品一区二区| 亚洲第一页综合图片自拍| 一级成人生活片免费看| 久久91亚洲精品中文字幕| 18禁无遮挡无码国产免费网站| 亚洲成AV人综合在线观看| 在线精品免费视频| 黄页网站在线免费观看| 亚洲日本va中文字幕久久| 91精品国产免费久久国语蜜臀| 亚洲啪啪免费视频| 国产大片91精品免费观看男同| 春意影院午夜爽爽爽免费| 亚洲国产精品一区第二页| 国产成人精品免费视频大| 亚洲国产精品自在自线观看| 丁香亚洲综合五月天婷婷| 成人爽a毛片免费| 亚洲自国产拍揄拍| 亚洲国产精品国产自在在线| 国产一区二区免费| 亚洲熟妇久久精品| 国产亚洲福利精品一区| 国产大片免费网站不卡美女| 黄网站色成年片大免费高清 | 亚洲精品国精品久久99热一| 一二三四免费观看在线电影|