# 2164. Sort Even and Odd Indices Independently

## sort

T : O(nlogn)

S: O(n)

```java
class Solution {
    public int[] sortEvenOdd(int[] nums) {
        int n = nums.length;
        List<Integer> odd = new ArrayList<>();
        List<Integer> even = new ArrayList<>();
        for (int i = 0 ; i < n; i++) {
            if (i % 2 == 0) { // even, asc
                even.add(nums[i]);
            } else { // odd, desc
                odd.add(nums[i]);
            }
        }
        Collections.sort(odd, (a, b) -> b - a);
        Collections.sort(even);
        int[] res = new int[n];
        int evenIdx = 0;
        int oddIdx = 0;
        for (int i = 0 ; i < n; i++) {
            if (i % 2 == 0) { // even, asc
                res[i] = even.get(evenIdx++); // 0, 2 (i = 0)
            } else { // odd, desc
                res[i] = odd.get(oddIdx++); // 0(i = 1, 3, 5)
            }
        }
        return res;
    }
}

/*
odd -> desc
[4,1,2,3] before this step, it becomes [4,3,2,1]

even -> asc
[4,1,2,3] before this step, it becomes [2,1,4,3]
*/
```

## bucket sort

因為這題數據很小, use bucket sort

T: O(100\*n) = O(n)

S: O(202)

```java
class Solution {
    public int[] sortEvenOdd(int[] nums) {
        int n = nums.length;
        int[] odd = new int[101]; // record count, number have duplicate number
        int[] even = new int[101]; // record count
        for (int i = 0 ; i < n; i++) {
            if (i % 2 == 0) { // even, asc
                even[nums[i]]++; 
            } else { // odd, desc
                odd[nums[i]]++;
            }
        }

        int[] res = new int[n];
        int evenIdx = 0;
        int oddIdx = 100;
        for (int i = 0; i < n; ++i) {
            if (i % 2 == 0) {
                // check even
                while (even[evenIdx] == 0) {
                    evenIdx++;
                }
                res[i] = evenIdx;
                even[evenIdx]--; // count--
            } else {
                while(odd[oddIdx] == 0) { // if there is count, pass then add data
                    oddIdx--;
                }
                res[i] = oddIdx;
                odd[oddIdx]--; // count--
            }
        }
        return res;
    }
}

/*
odd -> desc
[4,1,2,3] before this step, it becomes [4,3,2,1]

even -> asc
[4,1,2,3] before this step, it becomes [2,1,4,3]
*/a
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/weekly-contest/weekly-contest-279/2164.-sort-even-and-odd-indices-independently.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
