排序算法

冒泡排序:

for(int i = 0 ; i < A.length ; i++){
     for(int j = i+1 ; j < A.length ; j++){
           if(A[i] > A[j]){
                A[i] ^= A[j];
                A[j] ^= A[i];
                A[i] ^= A[j];
             }
      }
 }

 快速排序:

public class Solution {
    /**
     * @param A an integer array
     * @return void
     */
    public void sortIntegers2(int[] A) {
        if(A.length == 0)return;
        sort(A,0,A.length -1);
        
    }
    public static void sort(int arr[], int low, int high) {
        int l = low;
        int h = high;
        int povit = arr[low];

        while (l < h) {
            while (l < h && arr[h] >= povit)
                h--;
            if (l < h) {
                int temp = arr[h];
                arr[h] = arr[l];
                arr[l] = temp;
                l++;
            }

            while (l < h && arr[l] <= povit)
                l++;

            if (l < h) {
                int temp = arr[h];
                arr[h] = arr[l];
                arr[l] = temp;
                h--;
            }
        }
        if (l > low)
            sort(arr, low, l - 1);
        if (h < high)
            sort(arr, l + 1, high);
    }
}

 

posted @ 2016-11-01 13:56  从余  阅读(157)  评论(0编辑  收藏  举报