Simple Sort Algorithm -- BubbleSort

一、冒泡排序

核心:冒泡,持续比较相邻元素,大的挪到后面,因此大的会逐步往后挪,故称之为冒泡。

  

  复杂度分析:平均情况与最坏情况均为 O(n2),使用了 temp 作为临时交换变量,空间复杂度为 O(1)。

二、编程实现

  实现方式一:  

 1 package sort;
 2 
 3 public class BubbleSort {
 4     /**
 5      * 冒泡排序,持续比较相邻元素,大的挪到后面,因此大的会逐步往后挪,故称之为冒泡。
 6      * 复杂度分析:平均情况与最坏情况均为 O(n^2), 使用了 temp 作为临时交换变量,空间复杂度为 O(1).
 7      */
 8     public static void main(String[] args) {
 9         int[] unsortedArray = new int[]{6, 5, 3, 1, 8, 7, 2, 4} ;
10         bubbleSort(unsortedArray) ;
11         System.out.println("After sort: ");
12         for (int item : unsortedArray) {
13             System.out.print(item + " ");
14         }
15     }
16     
17     public static void bubbleSort(int[] array){
18         int len = array.length ;
19         for (int i = 0; i < len; i++) {
20             for (int item : array) {
21                 System.out.print(item + " ");
22             }
23             System.out.println();
24             for (int j = 1; j < len-i; j++) {  // 表示循环比较次数,后面的额数据都是排好序的了
25                 if (array[j-1] > array[j]) {
26                     int temp = array[j-1] ;
27                     array[j-1] = array[j];
28                     array[j] = temp ;
29                 }
30             }
31         }
32     }
33 }

  实现方式二:

  定义一个类,作为存储数组,并进行排序。

 1 package bubbleSort;
 2 
 3 public class ArrayBub 
 4 {
 5     private long[] a ;                // ref to Array a
 6     private int nElems ;            // number of data items
 7     
 8     public ArrayBub(int max){        // constructor
 9         a = new long[max] ;            // create the array
10         nElems = 0 ;                // no items yet
11     }
12     
13     public void insert(long value){    // put element into array
14         a[nElems] = value ;            // insert it
15         nElems++ ;                    // increment size
16     }
17     
18     public void display(){            // display array contents
19         for (int i = 0; i < nElems; i++) {
20             System.out.print(a[i] + " ");
21         }
22         System.out.println();
23     }
24     
25     public void bubbleSort(){
26         int out , in ;
27         for (out = nElems-1; out > 1; out--) {// outer loop (backward)
28             for(in = 0; in < out; in++){      // inner loop (forward)
29                 if(a[in] > a[in+1]){
30                     swap(in,in+1);              // swap them
31                 }
32             }
33         }
34     }
35 
36     private void swap(int one, int two) {
37         long temp = a[one] ;
38         a[one] = a[two] ;
39         a[two] = temp ;        
40     }
41 }

  定义一个主程序入口main函数

package bubbleSort;

public class BubbleSort 
{
	public static void main(String[] args) {
		int maxSize = 100 ;				// array size		
		ArrayBub arr  ;					// reference to array
		arr = new ArrayBub(maxSize) ;	// create the array
		
		arr.insert(6);
		arr.insert(5);
		arr.insert(3);
		arr.insert(1);
		arr.insert(8);
		arr.insert(7);
		arr.insert(2);
		arr.insert(4);
		
		arr.display();					// display items
							
		arr.bubbleSort();				// bubble sort them
				
		arr.display();					// display items again		
	}
}

  

  第25~34行代码,这个算法的思路是将最小的数据项放在数组的开始(下标为0),将最大的数据项放在数组的最后(下标为nElems-1)。外层for循环的计数器out从数组的最后开始,即out等于nElems-1,每经过一次循环out-1.下标大于out的数据项都已经是排好序的了。变量out在每完成一次内部循环(计数器为in)后,就左移一位,因此算法就不在处理那些已经排好序的数据了。

  内层for循环计数器in从数组的最开始算起,即in=0,每完成一次内部循环体加1,当它等于out时就结束一次循环。在内层for循环体中,数组下标为in和in+1的两个数据项进行比较,如果下标为in的数据项大于下标为in+1的数据项,则交换两个数据项。

posted @ 2017-06-18 09:30  落下树的汪汪狗  阅读(762)  评论(0编辑  收藏  举报