Advertisement
exmkg

Untitled

Jan 12th, 2025
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. class Solution {
  2.     public void wiggleSort(int[] nums) {
  3.         int n = nums.length;
  4.         if (n == 1) return;
  5.  
  6.         int middle = (n + 1) / 2;
  7.         int[] copy = Arrays.copyOf(nums, n);
  8.        
  9.         // Partition the elements, so that numbers that are <= median
  10.         // are in the left half and numbers that are > median
  11.         // are in the right half.
  12.         int median = quickSelect(copy, 0, copy.length - 1, middle);
  13.  
  14.         /*
  15.             To get Wiggle Sort, you want to put the numbers in the following way such that:
  16.             (1) elements smaller than the 'median' are put into the even indices,
  17.                 starting from the last even index;
  18.             (2) elements larger than the 'median' are put into the odd indices,
  19.                 starting from the first odd index;
  20.             (3) the medians are put into the remaining indices.
  21.         */
  22.  
  23.         // Assign all numbers in `nums` to -1 (an 'invalid' value).
  24.         for (int i = 0; i < n; i++) {
  25.             nums[i] = -1;
  26.         }
  27.  
  28.         int last_even_index = n - 1;
  29.         if (last_even_index % 2 == 1) last_even_index--;
  30.  
  31.         // (1) Put numbers that are < median at even indices,
  32.         // starting from the last even index.
  33.         int j = last_even_index;
  34.         for (int i = middle - 1; i >= 0; i--) {  // i iterates over numbers copy[i] <= median (left half)
  35.             if (copy[i] < median) {
  36.                 nums[j] = copy[i];
  37.                 j -= 2;
  38.             }
  39.         }
  40.         // (2) Put numbers that are > median at odd indices,
  41.         // starting from the first odd index.
  42.         j = 1;
  43.         for (int i = n - 1; i >= middle; i--) {  // i iterates over numbers copy[i] >= median (right half)
  44.             if (copy[i] > median) {
  45.                 nums[j] = copy[i];
  46.                 j += 2;
  47.             }
  48.         }
  49.         // (3) Remaining indices with 'invalid' values should be assigned to `median` value.
  50.         for (int i = 0; i < n; i++) {
  51.             if (nums[i] == -1) {
  52.                 nums[i] = median;
  53.             }
  54.         }
  55.     }
  56.  
  57.     // Partition the elements and determine what is the number at index k
  58.     // in the sorted order in O(N). The elements are partitioned so
  59.     // that numbers less than the number at index k in the sorted order
  60.     // are to the left of index k, and numbers greater than it are to the
  61.     // right of index k.
  62.     private static int quickSelect(int[] arr, int left, int right, int k) {
  63.         // Take a random pivot from the whole range.
  64.         Random rand = new Random();
  65.         int randIndex = rand.nextInt(left, right + 1);
  66.         swap(arr, left, randIndex);
  67.  
  68.         int pivot = arr[left];
  69.         int storeIndex = left + 1;
  70.         for (int i = left + 1; i <= right; i++) {
  71.             if (arr[i] < pivot) {
  72.                 swap(arr, i, storeIndex);
  73.                 storeIndex++;
  74.             }
  75.         }
  76.         swap(arr, left, storeIndex - 1);
  77.         int l = storeIndex - 2;
  78.         int r = storeIndex;
  79.         while (l >= left && arr[l] == pivot) l--;
  80.         while (r <= right && arr[r] == pivot) r++;
  81.  
  82.         if (l + 1 <= k && k <= r - 1) return arr[k];
  83.         if (k <= l) return quickSelect(arr, left, l, k);
  84.         return quickSelect(arr, r, right, k);
  85.     }
  86.  
  87.     private static void swap(int[] arr, int i, int j) {
  88.         int tmp = arr[i];
  89.         arr[i] = arr[j];
  90.         arr[j] = tmp;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement