Bubble sort(冒泡排序)

转载自:http://java2novice.com/java-sorting-algorithms/bubble-sort/

Bubble sort, also referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping 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 gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.

Bubble Sort

Bubble sort has worst-case and average complexity both О(n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Even other О(n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large.Performance of bubble sort over an already-sorted list (best-case) is O(n).


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.java2novice.algos;
  
public class MyBubbleSort {
  
     // logic to sort the elements
     public static void bubble_srt( int array[]) {
         int n = array.length;
         int k;
         for ( int m = n; m >= 0 ; m--) {
             for ( int i = 0 ; i < n - 1 ; i++) {
                 k = i + 1 ;
                 if (array[i] > array[k]) {
                     swapNumbers(i, k, array);
                 }
             }
             printNumbers(array);
         }
     }
  
     private static void swapNumbers( int i, int j, int [] array) {
  
         int temp;
         temp = array[i];
         array[i] = array[j];
         array[j] = temp;
     }
  
     private static void printNumbers( int [] input) {
          
         for ( int i = 0 ; i < input.length; i++) {
             System.out.print(input[i] + ", " );
         }
         System.out.println( "\n" );
     }
  
     public static void main(String[] args) {
         int [] input = { 4 , 2 , 9 , 6 , 23 , 12 , 34 , 0 , 1 };
         bubble_srt(input);
  
     }
}

Output:
2, 4, 6, 9, 12, 23, 0, 1, 34, 

2, 4, 6, 9, 12, 0, 1, 23, 34, 

2, 4, 6, 9, 0, 1, 12, 23, 34, 

2, 4, 6, 0, 1, 9, 12, 23, 34, 

2, 4, 0, 1, 6, 9, 12, 23, 34, 

2, 0, 1, 4, 6, 9, 12, 23, 34, 

0, 1, 2, 4, 6, 9, 12, 23, 34, 

0, 1, 2, 4, 6, 9, 12, 23, 34, 

0, 1, 2, 4, 6, 9, 12, 23, 34, 

0, 1, 2, 4, 6, 9, 12, 23, 34, 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值