Advertisement
satishfrontenddev4

Untitled

Jan 6th, 2024
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Given an unsorted array, sort it in wave form. That is, reorder it such that nums[0] <= nums[1] >= nums[2] <= nums[3]....
  3.  
  4. Input format
  5. There are 2 lines of input.
  6.  
  7. First line contains an integer N ,the number of input integers in the array.
  8.  
  9. Second line contains N space separated integers.
  10.  
  11. Output format
  12. N space separated elements which satisfy the given condition.
  13.  
  14. Sample Input 1
  15. 6
  16.  
  17. 1 5 1 1 6 4
  18.  
  19. Sample Output 1
  20. 1 4 1 5 1 6
  21.  
  22. Explanation 1
  23. nums[0] <= nums[1] >= nums[2] <= nums[3] >= nums[4] <= nums[5].
  24.  
  25. Constraints
  26. 1 <= N <= 100000
  27.  
  28. -10^9 <= A[i] <= 10^9
  29. */
  30.  
  31. /**
  32.  * @param {number[]} arr
  33.  * @param {number} n
  34.  * @return {number[]}
  35.  */
  36.   /*
  37.     1 5 1 1 6 4
  38.  
  39.     Sample Output 1
  40.     1 4 1 5 1 6
  41.  
  42.     Explanation 1
  43.     nums[0] <= nums[1] >= nums[2] <= nums[3] >= nums[4] <= nums[5].
  44.  
  45.     1. sort array
  46.     2. split  into lefthalf and righthalf
  47.     3. insert as pair from lefthalf and righthalf
  48.     arr.sort()=1 1 1 , 4 5 6
  49.  
  50.     leftarray = 1 1 1 -> i=0 , len1=(n+1)/2;
  51.     rightarray = 4 5 6 -> j=0; , len2=(n-len1);
  52.     */
  53. function wiggleSort(arr, n) {
  54.     arr.sort((a,b)=>a-b);
  55.     // console.log(arr);
  56.     let leftArrayLength = Math.floor((n+1)/2);
  57.     let waveArray= [];
  58.     let i=leftArrayLength-1;
  59.     let j=n-1;
  60.     while (i >=0 && j >= leftArrayLength) {
  61.       waveArray.push(arr[i]);
  62.       i--;
  63.       waveArray.push(arr[j]);
  64.       j--;
  65.     }
  66.     // console.log(waveArray);
  67.     while(i>=0){
  68.         waveArray.push(arr[i]);
  69.         i--;
  70.     }
  71.  
  72.     while(j>=leftArrayLength){
  73.         waveArray.push(arr[j]);
  74.         j--;
  75.     }
  76.     return waveArray;
  77.  
  78. }
  79.  
  80. /*
  81. Certainly, I'll explain why using default string sorting can lead to incorrect results for numbers, especially when there are multiple digits involved.
  82.  
  83. When you use the default sorting behavior (`arr.sort()` without a custom comparison function), JavaScript treats the elements in the array as strings and sorts them lexicographically (like words in a dictionary) rather than numerically. Here's an example to illustrate the issue:
  84.  
  85. Suppose you have the following array of numbers:
  86.  
  87. Input Array: `[10, 2, 25, 3, 11]`
  88.  
  89. When sorted using the default string sorting:
  90.  
  91. 1. `10` comes before `2` because `'10'` is lexicographically smaller than `'2'`.
  92. 2. `2` comes before `25` because `'2'` is lexicographically smaller than `'25'`.
  93. 3. `25` comes before `3` because `'25'` is lexicographically smaller than `'3'`.
  94. 4. `3` comes before `11` because `'3'` is lexicographically smaller than `'11'`.
  95.  
  96. So, the result of default string sorting is: `[10, 11, 2, 25, 3]`.
  97.  
  98. As you can see, the sorted result is not numerically correct. The numbers are sorted based on their string representations, leading to unexpected results.
  99.  
  100. To correctly sort the numbers, especially when you want numeric sorting, you should use a custom comparison function like `arr.sort((a, b) => a - b)` to ensure that the elements are sorted based on their numerical values. With this custom comparison function, the sorting algorithm understands that you want to compare numbers, and it arranges them accordingly:
  101.  
  102. Sorted result with custom numeric sorting: `[2, 3, 10, 11, 25]`
  103.  
  104. In summary, using default string sorting can lead to incorrect results for numbers because it doesn't consider their numerical values but treats them as strings. To achieve correct numeric sorting, it's essential to use a custom comparison function that explicitly instructs the sorting algorithm to compare the elements numerically.
  105. */
  106.  
  107. function main() {
  108.     const n = parseInt(readLine(), 10);
  109.     let arr = readIntArr()
  110.     const result = wiggleSort(arr, n)
  111.     console.log(result.join(" "))
  112.  
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement