程序員必知的8大排序(三)-------冒泡排序,快速排序(java實現)

http://blog.csdn.net/pzhtpf/article/details/7560294

5.冒泡排序

(1)基本思想:在要排序的一組數中,對當前還未排好序的範圍內的全部數,自上而下對相鄰的兩個數依次進行比較和調整,讓較大的數往下沉,較小的往上冒。即:每當兩相鄰的數比較後發現它們的排序與排序要求相反時,就將它們互換。

(2)實例:

(3)用java實現

 

  1. publi cclass bubbleSort {  
  2.   
  3. public bubbleSort(){  
  4.   
  5.     int a[]={1,54,6,3,78,34,12,45};  
  6.   
  7.     int temp=0;  
  8.   
  9.     for(int i=0;i<a.length;i++){  
  10.   
  11.        for(int j=i+1;j<a.length;j++){  
  12.   
  13.        if(a[i]>a[j]){  
  14.   
  15.            temp=a[i];  
  16.   
  17.            a[i]=a[j];  
  18.   
  19.            a[j]=temp;  
  20.   
  21.        }  
  22.   
  23.        }  
  24.   
  25.     }  
  26.   
  27.     for(int i=0;i<a.length;i++)  
  28.   
  29.        System.out.println(a[i]);     
  30.   
  31. }  
  32.   
  33. }  
publi cclass bubbleSort {

public bubbleSort(){

    int a[]={1,54,6,3,78,34,12,45};

    int temp=0;

    for(int i=0;i<a.length;i++){

       for(int j=i+1;j<a.length;j++){

       if(a[i]>a[j]){

           temp=a[i];

           a[i]=a[j];

           a[j]=temp;

       }

       }

    }

    for(int i=0;i<a.length;i++)

       System.out.println(a[i]);   

}

}

 

 

經過道友的提醒,發現上面的不是正宗的冒泡排序,所以更正過來:

正宗的冒泡排序:

  1. public class bubbleSort {  
  2. public  bubbleSort(){  
  3.      int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
  4.     int temp=0;  
  5.     for(int i=0;i<a.length-1;i++){  
  6.         for(int j=0;j<a.length-1-i;j++){  
  7.         if(a[j]>a[j+1]){  
  8.             temp=a[j];  
  9.             a[j]=a[j+1];  
  10.             a[j+1]=temp;  
  11.         }  
  12.         }  
  13.     }  
  14.     for(int i=0;i<a.length;i++)  
  15.         System.out.println(a[i]);     
  16. }  
  17. }  
public class bubbleSort {
public	bubbleSort(){
	 int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
	int temp=0;
	for(int i=0;i<a.length-1;i++){
		for(int j=0;j<a.length-1-i;j++){
		if(a[j]>a[j+1]){
			temp=a[j];
			a[j]=a[j+1];
			a[j+1]=temp;
		}
		}
	}
	for(int i=0;i<a.length;i++)
		System.out.println(a[i]);	
}
}

 

 

但第一種是什麼排序呢?跟選擇排序有點類似,但又不是,還望高手指點一二!


6.快速排序

1)基本思想:選擇一個基準元素,通常選擇第一個元素或者最後一個元素,通過一趟掃描,將待排序列分成兩部分,一部分比基準元素小,一部分大於等於基準元素,此時基準元素在其排好序後的正確位置,然後再用同樣的方法遞歸地排序劃分的兩部分。

2)實例:

3)用java實現

 

  1. public class quickSort {  
  2.   
  3.   inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
  4.   
  5. public quickSort(){  
  6.   
  7.     quick(a);  
  8.   
  9.     for(int i=0;i<a.length;i++)  
  10.   
  11.        System.out.println(a[i]);  
  12.   
  13. }  
  14.   
  15. publicint getMiddle(int[] list, int low, int high) {     
  16.   
  17.             int tmp = list[low];    //數組的第一個作爲中軸     
  18.   
  19.             while (low < high) {     
  20.   
  21.                 while (low < high && list[high] >= tmp) {     
  22.   
  23.                     high--;     
  24.   
  25.                 }     
  26.   
  27.                 list[low] = list[high];   //比中軸小的記錄移到低端     
  28.   
  29.                 while (low < high && list[low] <= tmp) {     
  30.   
  31.                     low++;     
  32.   
  33.                 }     
  34.   
  35.                 list[high] = list[low];   //比中軸大的記錄移到高端     
  36.   
  37.             }     
  38.   
  39.            list[low] = tmp;              //中軸記錄到尾     
  40.   
  41.             return low;                   //返回中軸的位置     
  42.   
  43.         }    
  44.   
  45. publicvoid _quickSort(int[] list, int low, int high) {     
  46.   
  47.             if (low < high) {     
  48.   
  49.                int middle = getMiddle(list, low, high);  //將list數組進行一分爲二     
  50.   
  51.                 _quickSort(list, low, middle - 1);        //對低字表進行遞歸排序     
  52.   
  53.                _quickSort(list, middle + 1, high);       //對高字表進行遞歸排序     
  54.   
  55.             }     
  56.   
  57.         }   
  58.   
  59. publicvoid quick(int[] a2) {     
  60.   
  61.             if (a2.length > 0) {    //查看數組是否爲空     
  62.   
  63.                 _quickSort(a2, 0, a2.length - 1);     
  64.   
  65.         }     
  66.   
  67.        }   
  68.   
  69. }  
  70.   
  71.    

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章