I've come up with the following C# solution to this problem; O(n) time complexity
public class Kata { public static void swapElements(int i, int j, int[] inputArr) { int temp = inputArr[i]; inputArr[i] = inputArr[j]; inputArr[j] = temp; } public static void WaveSort(int[] arr) { int anchor = 0; int explorer = 1; bool isAnchorWaveSorted = false; Array.Sort(arr); while(explorer < arr.Length) { if(!isAnchorWaveSorted) { swapElements(anchor, explorer, arr); } isAnchorWaveSorted = !isAnchorWaveSorted; anchor++; explorer++; } } }
Sorting is O(n log n) - O(n²) depending on if you allocate additional space.
I'm not knee-deep in algorithm optimization having tasted the instant-gratifications of web development, but this resource was neat to me ~
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I've come up with the following C# solution to this problem;
O(n) time complexity
Sorting is O(n log n) - O(n²) depending on if you allocate additional space.
I'm not knee-deep in algorithm optimization having tasted the instant-gratifications of web development, but this resource was neat to me ~