Algo-Phantoms / Algo-Tree

Algo-Tree is a collection of Algorithms and data structures which are fundamentals to efficient code and good software design. Creating and designing excellent algorithms is required for being an exemplary programmer. It contains solutions in various languages such as C++, Python and Java.
MIT License
357 stars 624 forks source link

WIGGLE SORT #1972

Open Fatema110 opened 3 years ago

Fatema110 commented 3 years ago
This program accepts an array of unsorted numbers and
sorts the array such that arr[0]<arr[1]>arr[2]<arr[3]...

arr is sorted and stored in second array res
res is partitioned into two such that left sub-array 
contains elements less than the elements in right sub-array
Elements in the left sub-array  are added to the even indices of arr
Elements in the right sub-array are added to the odd indices of arr
  1. Sample input:1 3 2 2 3 1 Sample output: [2,3,1,3,1,2]

  2. Sample input:1 4 6 2 3 7 9 2 1 0 Sample Output: [2, 9, 2, 7, 1, 6, 1, 4, 0, 3]

Time Complexity: O(N log N) (Sorting takes O(N logN) and traversal takes O(N)) Space Complexity: O(N) (New array to store the wiggle sorted elements)