{"id":22513505,"url":"https://github.com/emahtab/wiggle-sort","last_synced_at":"2026-01-28T23:33:11.782Z","repository":{"id":79525994,"uuid":"232605802","full_name":"eMahtab/wiggle-sort","owner":"eMahtab","description":"Wiggle Sort","archived":false,"fork":false,"pushed_at":"2020-06-26T16:29:54.000Z","size":8,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-01T11:59:09.227Z","etag":null,"topics":["leetcode","lintcode","problem-solving","wiggle"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-01-08T16:23:04.000Z","updated_at":"2020-06-26T16:29:57.000Z","dependencies_parsed_at":"2023-03-18T13:06:00.403Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/wiggle-sort","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/wiggle-sort","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fwiggle-sort","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fwiggle-sort/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fwiggle-sort/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fwiggle-sort/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/wiggle-sort/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fwiggle-sort/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28855156,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-28T22:56:21.783Z","status":"ssl_error","status_checked_at":"2026-01-28T22:56:00.861Z","response_time":57,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["leetcode","lintcode","problem-solving","wiggle"],"created_at":"2024-12-07T03:12:46.258Z","updated_at":"2026-01-28T23:33:11.777Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Wiggle Sort\n### https://www.lintcode.com/problem/wiggle-sort\n\nGiven an unsorted array nums, reorder it in-place such that nums[0] \u003c= nums[1] \u003e= nums[2] \u003c= nums[3]....\nFor example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4].\n\n### Approach :\nIf we carefully see the pattern in the question. \n\nWe can see that A[0] \u003c= A[1] \u003e= A[2] \u003c= A[3] \u003e= A[4] \u003c= A[5]\n\nSo we could actually observe that there is pattern\n`A[even] \u003c= A[odd]` and `A[odd] \u003e= A[even]`\n\nTherefore we can iterate over the array and compare the elements at index `i` and `i+1`.\nIf we find that above condition is not satisfied at index `i` in the array, then we can just swap the elements at index `i` and `i+1`.\n\n### Implementation :\n\n```java\n  class Solution {\n    public void wiggleSort(int[] nums) {\n        if (nums == null || nums.length \u003c 2)\n            return;\n        for(int i = 0; i \u003c nums.length - 1; i++) {\n            if(i % 2 == 0 \u0026\u0026 nums[i] \u003e nums[i+1])\n                swap(nums, i, i+1);\n            else if(i % 2 == 1 \u0026\u0026 nums[i] \u003c nums[i+1])\n                swap(nums, i, i+1);\n        }\n    }\n    \n    private void swap(int[] nums, int i , int j) {\n        int temp = nums[i];\n        nums[i] = nums[j];\n        nums[j] = temp;\n    }\n}\n```\nAbove implementation have runtime complexity of O(n) and space complexity of O(1)\n\n```\nRuntime Complexity = O(n)\nSpace Complexity   = O(1)\n```\n\n## References:\nhttp://buttercola.blogspot.com/2015/09/leetcode-wiggle-sort.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fwiggle-sort","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fwiggle-sort","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fwiggle-sort/lists"}