# Sort even and odd placed elements in increasing order

Given a list N, the task is to sort all the elements at odd and even positions in increasing order. After sorting, we need to put all odd positioned elements together, then all even positioned elements

Examples:

Input : [3, 2, 7, 6, 8]
Output : 3 7 8 2 6
Explanation:
Odd position elements in sorted order are 3, 7, 8.
Even position elements in sorted order are 2, 6.

Input : 1 0 2 7 0 0
Output : 0 1 2 0 0 7
Odd {1, 2, 0}
Even {0, 7, 0}

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Initialize two lists to store the odd and even indexed digits.
• Traverse through all the digits and store the odd indexed digits at odd_indexes list and the even indexed digits at even_indexes list.
• Print the elements in the odd_indexes list in sorted order.
• Print the elements in the even_indexes list in the sorted order.

Below is the implementation:

## C++

 // C++ implementation of above approach  #include using namespace std;    // function to prin the odd and even indexed digits void odd_even(int arr[], int n) {            // lists to store the odd and     // even positioned digits     vector odd_indexes;     vectoreven_indexes;            // traverse through all the indexes      // in the integer     for (int i = 0; i < n;i++)     {            // if the digit is in odd_index position         // append it to odd_position list         if (i % 2 == 0)         odd_indexes.push_back(arr[i]);                    // else append it to the even_position list         else         even_indexes.push_back(arr[i]);        }                // print the elements in the list in sorted order     sort(odd_indexes.begin(), odd_indexes.end());     sort(even_indexes.begin(), even_indexes.end());            for(int i = 0; i < odd_indexes.size();i++)         cout << odd_indexes[i] << " ";        for(int i = 0; i < even_indexes.size(); i++)         cout << even_indexes[i] << " ";         }     // Driver code int main() {     int arr[] = {3, 2, 7, 6, 8};     int n = sizeof(arr)/sizeof(arr[0]);     odd_even(arr, n); }    // This code is contributed by // Surendra_Gangwar

## Python3

 # function to prin the odd and even indexed digits def odd_even(n):            # lists to store the odd and     # even positioned digits     odd_indexes = []     even_indexes = []            # traverse through all the indexes      # in the integer     for i in range(len(n)):                    # if the digit is in odd_index position         # append it to odd_position list         if i % 2 == 0: odd_indexes.append(n[i])                    # else append it to the even_position list         else: even_indexes.append(n[i])                # print the elements in the list in sorted order     for i in sorted(odd_indexes): print(i, end =" ")     for i in sorted(even_indexes): print(i, end =" ")       # Driver Code n = [3, 2, 7, 6, 8] odd_even(n)

## PHP



Output:

3 7 8 2 6

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.