您的位置:首页 > 其它

464. 整数排序 II

2017-12-11 22:14 246 查看
给一组整数,按照升序排序。使用归并排序,快速排序,堆排序或者任何其他 O(n log n) 的排序算法。

您在真实的面试中是否遇到过这个题? 

Yes

样例

给出 
[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);

            }

           

            

        }

    

    

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: