clueless coding // TODO: be smarter

LeetCode Wiggle Sort: CPP Solution



TL;DR Code Solution


void swap(vector<int>& nums, int i, int j) {
    int temp = nums[j];
    nums[j] = nums[i];
    nums[i] = temp;
}

void wiggleSort(vector<int>& nums) {
    for (int i = 0; i < (int)nums.size() - 1; i++) {
        if (i % 2 == 0) {
            if (nums[i] > nums[i+1]) {
                swap(nums, i, i+1);
            }    
        }
        else {
            if (nums[i] < nums[i+1]) {
                swap(nums, i, i+1);
            }
        }
    }
}