| 1 | class Solution { |
| 2 | public: |
| 3 | void wiggleSort(vector<int>& nums) { |
| 4 | |
| 5 | vector<int> nums_cpy=nums; //making a copy of the original array |
| 6 | sort(nums_cpy.begin(),nums_cpy.end()); //sorting the array |
| 7 | int i=1; |
| 8 | int j=nums.size()-1; |
| 9 | int k=0; |
| 10 | |
| 11 | //for the 1st iteration, we copy numbers from the end of the sorted array |
| 12 | //to the odd positions of the original array |
| 13 | //in this way, the odd indices will be greater than its neighbours |
| 14 | while(i<nums.size()){ |
| 15 | nums[i]=nums_cpy[j--]; |
| 16 | i=i+2; |
| 17 | |
| 18 | } |
| 19 | //resetting the i to store the even indices |
| 20 | i=0; |
| 21 | |
| 22 | //for the 2nd iteration, we assign the even indices of the original array |
| 23 | //using the decreasing values from the sorted array |
| 24 | //in this iteration, even indices assigned will be less that its neighbouring |
| 25 | while(i<nums.size()){ |
| 26 | nums[i]=nums_cpy[j--]; |
| 27 | i=i+2; |
| 28 | |
| 29 | } |
| 30 | } |
| 31 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected