java基礎知識之 演算法 【冒泡排序】【快速排序】

來源:互聯網
上載者:User

 

/**

@auther:kevin

@function:

 冒泡排序(BubbleSort)的基本概念是:依次比較相鄰的兩個數,將小數放在前面,大數放在後面。即在第一趟:首先比較第1個和第2個數,將小數放前,大數放後。然後比較第2個數和第3個數,將小數放前,大數放後,如此繼續,直至比較最後兩個數,將小數放前,大數放後。至此第一趟結束,將最大的數放到了最後。在第二趟:仍從第一對數開始比較(因為可能由於第2個數和第3個數的交換,使得第1個數不再小於第2個數),將小數放前,大數放後,一直比較到倒數第二個數(倒數第一的位置上已經是最大的),第二趟結束,在倒數第二的位置上得到一個新的最大數(其實在整個數列中是第二大的數)。如此下去,重複以上過程,直至最終完成排序。
  由於在排序過程中總是小數往前放,大數往後放,相當於氣泡往上升,所以稱作冒泡排序。
*/

public class BubbleSort
{
 public static void main(String args[]){
 int a[] = {110,23,1,12,34,23} ; //定義一個數組
 int temp;  //temp用於儲存
 for(int i=1;i <=a.length-1;i++)
 {
  for (int j=0;j<=a.length-1-i; j++)
  {
   if(a[j]>a[j+1])//如果前一個數大於後一個數
   {
    temp = a[j+1];  //a[j]與a[j+1]調換位置
    a[j+1] = a[j];
    a[j] = temp;
   }
  }
 }
 for(int i=0;i <a.length;i++)
 System.out.println(a[i]);
 }
}

 

 

 

-------------------------------------------------------------------------

 【快讀排序】

import java.util.Arrays;

/**
 * 快速排序
 * @author Administrator
 *
 */
public class QuickSort {
/**
 * 一趟比較劃分結果
 * @param sortIn 傳入的數組
 * @param i 區間下界
 * @param j 區間上界
 * @return 返回基準最終位置
 */
  private static int partition(Integer[] sortIn, int i, int j) {
  
         int pivot = sortIn[i];  
         while (i < j) {  
             // 從右向左掃描,尋找第一個關鍵字小於pivot的記錄  
             while (i < j && sortIn[j] >= pivot)  
                 j--;  
             if (i < j) {  
                 swap(sortIn, i, j);// 交換sortIn[i]和sortIn[j]  
                 i++;  
             }  
  
            // System.out.println("從右向左掃描:" + Arrays.asList(sortIn).toString());  
               
             // 從左向右掃描,尋找第一個關鍵字大於pivot的記錄  
             while (i < j && sortIn[i] <= pivot)  
                 i++;  
             if (i < j) {  
                 swap(sortIn, i, j);  
                 j--;  
             }  
               
            // System.out.println("從左向右掃描:" + Arrays.asList(sortIn).toString());  
         }  
         sortIn[i] = pivot;// 基準位置已被最終定位  
         //System.out.println("標記:" + pivot);  
         return i;  
     } 
  /**
   * 遞迴實現快速排序
   * @param sortIn
   * @param low
   * @param high
   */
    public static void quickSort(Integer[] sortIn, int low, int high) {  
         int pivotpos;// 劃分後的基準記錄的位置  
         if (low < high) {// 僅當區間長度大於1時才需排序  
             pivotpos = partition(sortIn, low, high);// 做劃分  
             quickSort(sortIn, low, pivotpos - 1);// 對左區間進行遞迴排序  
             quickSort(sortIn, pivotpos + 1, high);// 對右區間進行遞迴排序  
         }
        
     }  

 
  private static void swap(Integer[] sortIn, int i, int j) {
   // TODO Auto-generated method stub
   int temp = sortIn[i];
   sortIn[i] = sortIn[j];
   sortIn[j] = temp;
   
  }
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Integer[] a=new Integer[] { 8, 6, 1, 5, 4 };
  quickSort(a,0,a.length-1);
  System.out.println("最終結果:" + Arrays.asList(a).toString());  
 }

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.