6 Lines Java Solution | Simple Solution | Easy To Understand | Beginner Friendly
Intuition\n Describe your first thoughts on how to solve this problem.  \nUsed PriorityQueue to sort the input array in ascending order. A PriorityQueue is a he
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. --> \nUsed PriorityQueue to sort the input array in ascending order. A PriorityQueue is a heap-based data structure that maintains the order of its elements based on their priority. In this case, we are using a default natural ordering where smaller elements have a higher priority.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- First creates an empty PriorityQueue, and then adds all the elements of the input array to the PriorityQueue using the add() method. Since PriorityQueue maintains the order of its elements, the elements are sorted as they are added to the PriorityQueue.\n\n- Then, iterates over the input array and replaces each element with the smallest element in the PriorityQueue using the poll() method. After iterating over all elements, the input array will be sorted in ascending order.\n\n# Complexity\n- Time complexity:O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int[] sortArray(int[] nums) {\n      PriorityQueue<Integer> pQ = new PriorityQueue<>();\n      for(int n:nums)\n      pQ.add(n);\n      for(int i=0;i<nums.length;i++)\n      nums[i]=pQ.poll(); \n      return nums;\n    }\n   \n}\n```\n\n# Brute force(Quick sort)\n```\nclass Solution {\n    public int[] sortArray(int[] nums) {\n       Sort(nums,0,nums.length-1);\n       return nums;\n    }\n    static void Sort(int []nums,int l,int h){\n        if(l>=h){\n            return ;\n        }\n        int s=l;\n        int e=h;\n        int m=s+(e-s)/2;\n        int p=nums[m];\n        while(s<=e){\n            while(nums[s]<p){\n                s++;\n            }\n            while(nums[e] > p){\n                e--;\n            }\n            if(s<=e){\n                int t=nums[s];\n                nums[s]=nums[e];\n                nums[e]=t;\n                s++;\n                e--;\n            }\n        }\n        Sort(nums,l,e);\n        Sort(nums,s,h);\n    }\n}\n\n```\n\nUpvotes are Encouraging
['Heap (Priority Queue)', 'Quickselect', 'Java']