11.5

Bubble sort

Contents

1.introduction

2.algorithm comparison

3.References

introduction

1.bubble sort

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.[2] Bubble sort can be practical if the input is in mostly sorted order with some out-of-order elements nearly in position.
-from Wikipedia

Profiling

Data structure Array
Worst-case О(n2) comparisons
performance О(n2) swaps
Best-case О(n) comparisons
performance О(1) swaps
Average О(n2) comparisons
performance О(n2) swaps
Worst-case space complexity О(1) auxiliary

Algorithm demo

Algorithm demo

Algorithm demo

Detailed steps

detailed steps

Code snippets

Define an array to hold the initial array

    private int[] array;
    private int length;
    
    public Bobsort(int[] array){
        this.array = array;
        this.length = array.length;
    }

Function that implements bubble sorting

   public void bobSort(){
        for(int i=0;i<length-1;i++){
            for(int j=0;j<length-1-i;j++){
                if(array[j]>array[j+1]){
                    int temp = array[j+1];
                    array[j+1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }

Output result

 public void print(){
        for(int i : array){
            System.out.print(i+" ");
        }
        System.out.println();
    }

Generate random data, output array before and after sorting, get time for subsequent comparison

public static void main(String[] args){
        int array[]=new int[1000000];
            for(int i=0;i<1000;i++)
                {
                array[i]=(int) ( Math.random() *500 );
                }
        Bobsort bobSort = new Bobsort(array);
        System.out.println("排序前的数据为:");
        bobSort.print();
        System.out.println("排序前的时间为:");
        System.out.println(new SimpleDateFormat("HH:mm:ss:SSS").format(new Date()));
        bobSort.bobSort();
        System.out.println("排序后的数据为:");
        bobSort.print();
        System.out.println("排序后的时间为:");
        System.out.println(new SimpleDateFormat("HH:mm:ss:SSS").format(new Date()));
    }

Result display for 1000 random number from 1-1000


Result

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.
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.

Although bubble sort is one of the simplest sorting algorithms to understand and implement, itsO(n2)complexity means that its efficiency decreases dramatically on lists of more than a small number of elements. Even among simple O(n2) sorting algorithms, algorithms like Insertion sort are usually considerably more efficient.

Due to its simplicity, bubble sort is often used to introduce the concept of an algorithm, or a sorting algorithm, to introductory computer science students. However, some researchers such as Owen Astrachan have gone to great lengths to disparage bubble sort and its continued popularity in computer science education, recommending that it no longer even be taught.

In computer graphics bubble sort is popular for its capability to detect a very small error (like swap of just two elements) in almost-sorted arrays and fix it with just linear complexity (2n). For example, it is used in a polygon filling algorithm, where bounding lines are sorted by their x coordinate at a specific scan line (a line parallel to the x axis) and with incrementing y their order changes (two elements are swapped) only at intersections of two lines. Bubble sort is a stable sort algorithm, like insertion sort.

2.merge sort

Like quick-sort, Merge Sort is a divide-and-conquer-introduction algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one. See following C implementation for details.
-from GeeksforGeeks

Profiling

Data structure Array
Worst-case O(n log n)
performance n log n - n - 1(in my opinion)
Best-case O(n log n) typical
performance O(n) natural variant
Average O(n log n)
performance О(n2) swaps
Worst-case space complexity auxiliary, O(1) auxiliary with linked lists

Algorithm demo

Algorithm demo

Algorithm demo

Detailed steps

detailed steps

Code snippets

Initial array, recursive to no longer divisible

public static void mergeSort(int[] data) {
        sort(data, 0, data.length - 1);
    }
 
    public static void sort(int[] data, int left, int right) {
        if (left >= right)
            return;
        int center = (left + right) / 2;
        sort(data, left, center);
        sort(data, center + 1, right);
        merge(data, left, center, right);
    }

Function that implements merge sorting

public static void merge(int[] data, int left, int center, int right) {
        int[] tmpArr = new int[data.length];
        int mid = center + 1;
        int third = left;
        int tmp = left;
        while (left <= center && mid <= right) {
            if (data[left] <= data[mid]) {
                tmpArr[third++] = data[left++];
            } else {
                tmpArr[third++] = data[mid++];
            }
        }
        while (mid <= right) {
            tmpArr[third++] = data[mid++];
        }
        while (left <= center) {
            tmpArr[third++] = data[left++];
        }
        while (tmp <= right) {
            data[tmp] = tmpArr[tmp++];
        }
    }

Generate random data, output array before and after sorting, get time for subsequent comparison

public static void print(int[] data) {
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i]+" ");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        int array[]=new int[1000];
        for(int i=0;i<1000;i++)
            {
            array[i]=(int) ( Math.random()*500 );
            }
        System.out.println("排序前的数据为:");
        print(array);
        System.out.println("排序前的时间为:");
        System.out.println(new SimpleDateFormat("HH:mm:ss:SSS").format(new Date()));
        mergeSort(array);
        System.out.println("排序后的数据为:");
        print(array);
        System.out.println("排序后的时间为:");
        System.out.println(new SimpleDateFormat("HH:mm:ss:SSS").format(new Date()));
    }
}

Result display for 1000 random number from 1-1000


Result

Analyzing Merge Sort

For simplicity, assume that n is a power of 2 so that each divide step yields two subproblems, both of size exactly n/2.

The base case occurs when n = 1.

When n ≥ 2, time for merge sort steps:

  • Divide: Just compute q as the average of p and r, which takes constant time i.e. Θ(1).

  • Conquer: Recursively solve 2 subproblems, each of size n/2, which is 2T(n/2).

  • Combine: MERGE on an n-element subarray takes Θ(n) time.

Summed together they give a function that is linear in n, which is Θ(n). Therefore, the recurrence for merge sort running time is

merge sort recurrence

Recursion Tree

We can understand how to solve the merge-sort recurrence without the master theorem. There is a drawing of recursion tree on page 35 in CLRS, which shows successive expansions of the recurrence.

The following figure (Figure 2.5b in CLRS) shows that for the original problem, we have a cost of cn, plus the two subproblems, each costing T (n/2).

Construction of recursion tree

The following figure (Figure 2.5c in CLRS) shows that for each of the size-n/2 subproblems, we have a cost of cn/2, plus two subproblems, each costing T (n/4).

Construction of recursion tree

The following figure (Figure: 2.5d in CLRS) tells to continue expanding until the problem sizes get down to 1.

Construction of the recursion tree

In the above recursion tree, each level has cost cn.

  • The top level has cost cn.

  • The next level down has 2 subproblems, each contributing cost cn/2.

  • The next level has 4 subproblems, each contributing cost cn/4.

  • Each time we go down one level, the number of subproblems doubles but the cost per subproblem halves. Therefore, cost per level stays the same.

The height of this recursion tree is lg n and there are lg n + 1 levels.

algorithm comparison

bubble sort

bubble sort

Clearly, the graph shows the n2 nature of the bubble sort.
In this algorithm the number of comparison is irrespective of data set i.e., input whether best or worst.


merge sort

merge sort

The inductive hypothesis is that a tree for a problem size of 2i has lg 2i + 1 = i +1 levels. Because we assume that the problem size is a power of 2, the next problem size up after 2i is 2i + 1. A tree for a problem size of 2i + 1 has one more level than the size-2i tree implying i + 2 levels. Since lg 2i + 1 = i + 2, we are done with the inductive argument.
Total cost is sum of costs at each level of the tree. Since we have lg n +1 levels, each costing cn, the total cost is cn lg n + cn.Ignore low-order term of cn and constant c, and we have,Θ(n lg n) which is the desired result.

memory requirement

As for bubble sort,it doesnt need any extra memory,and for merge sort if a temporary array is declared for each recursive call to merge, logn temporary arrays may be active at any one time, which is fatal to small memory machines. On the other hand, if merge dynamically allocates and releases the minimum amount of temporary space, the time occupied by malloc(Request the system to allocate memory space of specified size bytes) will be much longer. Since merge is on the last row of m sort, you can create the temporary array in merge sort. Therefore, only one temporary array activity is needed at any time, and any part of the temporary array can be used. We will use the same part as the input array Array. In this case, the space occupied by the algorithm is n, n is the number of array elements to be sorted.

References

bubble sort
merge sort

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 201,784评论 5 474
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,745评论 2 378
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,702评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,229评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,245评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,376评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,798评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,471评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,655评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,485评论 2 318
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,535评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,235评论 3 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,793评论 3 304
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,863评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,096评论 1 258
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,654评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,233评论 2 341

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,278评论 0 10
  • 今天是休息日,一周最开心,最放松的一天。儿子周六写完所有作业,我和儿子会一起做游戏、一起做饭、一起外出...
    niuniubaobei521阅读 162评论 2 4
  • 闻声而起,洗漱自己,整理被子,下楼排队,去五观堂用斋,按序坐下,翻开饭碗,安静等待,读诵经典,唱诵《供养偈》,合掌...
    大方demi阅读 755评论 0 1
  • 前几天大家估计也有刷过默多克的结婚的新闻吧,我真真觉得西方人真是将生命过到了极致,这老头真是对生活充满了热爱对自己...
    goddessna阅读 267评论 0 0