464. 整数排序 II

来源:互联网 发布:owncloud windows安装 编辑:程序博客网 时间:2024/06/05 07:00

给一组整数,按照升序排序。使用归并排序,快速排序,堆排序或者任何其他 O(n log n) 的排序算法。

样例

给出 [3, 2, 1, 4, 5], 排序后的结果为 [1, 2, 3, 4, 5]




 public class Solution {

    /*
     * @param A: an integer array
     * @return: 
     */
         
    public void sortIntegers2(int[] A) {
        // write your code here
       
        Wsort(A, 0, A.length-1);
    }
    public int partiton(int[] A, int low, int high)
    {
            int first = low;
            int last = high;
            int key = A[low];
            while(first < last)
            {
                while(first < last && A[last] >= key)
                {
                    last--;
                }
               if(first < last) A[first] = A[last];
               
                
                while(first < last && A[first] <= key)
                {
                    first++;
                }
               if(first < last) A[last] = A[first];
               
                
            }
            A[first] = key;
            return first;
    }
      
      
    public void Wsort(int[] A, int low, int high)
        {
            if (low < high)
            {
             
            int index = partiton(A, low, high);
            Wsort(A, low, index-1);
            Wsort(A, index+1, high);
            }
           
            
        }
    
    




原创粉丝点击