Related Articles
Rearrange a given list such that it consists of alternating minimum maximum elements
• Difficulty Level : Medium
• Last Updated : 24 Jan, 2020

Given a list of integers, rearrange the list such that it consists of alternating minimum maximum elements using only list operations. The first element of the list should be minimum and second element should be maximum of all elements present in the list. Similarly, third element will be next minimum element and fourth element is next maximum element and so on. Use of extra space is not permitted.

Examples:

```Input:  [1 3 8 2 7 5 6 4]
Output: [1 8 2 7 3 6 4 5]

Input:  [1 2 3 4 5 6 7]
Output: [1 7 2 6 3 5 4]

Input:  [1 6 2 5 3 4]
Output: [1 6 2 5 3 4]
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

The idea is to sort the list in ascending order first. Then we start popping elements from the end of the list and insert them into their correct position in the list.

Below is the implementation of above idea –

## C/C++

 `// C++ program to rearrange a given list such that it``// consists of alternating minimum maximum elements``#include ``using` `namespace` `std;`` ` `// Function to rearrange a given list such that it``// consists of alternating minimum maximum elements``void` `alternateSort(list<``int``>& inp)``{``    ``// sort the list in ascending order``    ``inp.sort();`` ` `    ``// get iterator to first element of the list``    ``list<``int``>::iterator it = inp.begin();``    ``it++;`` ` `    ``for` `(``int` `i=1; i<(inp.size() + 1)/2; i++)``    ``{``        ``// pop last element (next greatest)``        ``int` `val = inp.back();``        ``inp.pop_back();`` ` `        ``// insert it after next minimum element``        ``inp.insert(it, val);`` ` `        ``// increment the pointer for next pair``        ``++it;``    ``}``}`` ` `// Driver code``int` `main()``{``    ``// input list``    ``list<``int``> inp({ 1, 3, 8, 2, 7, 5, 6, 4 });`` ` `    ``// rearrange the given list``    ``alternateSort(inp);`` ` `    ``// print the modified list``    ``for` `(``int` `i : inp)``        ``cout << i << ``" "``;`` ` `    ``return` `0;``}`

## Java

 `// Java program to rearrange a given list such that it``// consists of alternating minimum maximum elements``import` `java.util.*;`` ` `class` `AlternateSort``{``    ``// Function to rearrange a given list such that it``    ``// consists of alternating minimum maximum elements``    ``// using LinkedList``    ``public` `static` `void` `alternateSort(LinkedList ll) ``    ``{``        ``Collections.sort(ll);``         ` `        ``for` `(``int` `i = ``1``; i < (ll.size() + ``1``)/``2``; i++)``        ``{``            ``Integer x = ll.getLast();``            ``ll.removeLast();``            ``ll.add(``2``*i - ``1``, x);``        ``}``         ` `        ``System.out.println(ll);``    ``}``     ` `    ``public` `static` `void` `main (String[] args) ``throws` `java.lang.Exception``    ``{``        ``// input list``        ``Integer arr[] = {``1``, ``3``, ``8``, ``2``, ``7``, ``5``, ``6``, ``4``};``         ` `        ``// convert array to LinkedList``        ``LinkedList ll = ``new` `LinkedList(Arrays.asList(arr));``         ` `        ``// rearrange the given list``        ``alternateSort(ll);``    ``}``}`

## Python

 `# Python program to rearrange a given list such that it``# consists of alternating minimum maximum elements``inp ``=` `[]`` ` `# Function to rearrange a given list such that it``# consists of alternating minimum maximum elements``def` `alternateSort():`` ` `    ``global` `inp``     ` `    ``# sort the list in ascending order``    ``inp.sort()`` ` `    ``# get index to first element of the list``    ``it ``=` `0``    ``it ``=` `it ``+` `1``     ` `    ``i ``=` `1``     ` `    ``while` `( i < (``len``(inp) ``+` `1``)``/``2` `):``     ` `        ``i ``=` `i ``+` `1``         ` `        ``# pop last element (next greatest)``        ``val ``=` `inp[``-``1``]``        ``inp.pop()`` ` `        ``# insert it after next minimum element``        ``inp.insert(it, val)`` ` `        ``# increment the pointer for next pair``        ``it ``=` `it ``+` `2``     ` `# Driver code`` ` `# input list``inp``=``[ ``1``, ``3``, ``8``, ``2``, ``7``, ``5``, ``6``, ``4` `]`` ` `# rearrange the given list``alternateSort()`` ` `# print the modified list``print` `(inp) `` ` `# This code is contributed by Arnab Kundu`

## C#

 `// C# program to rearrange a given list such that it``// consists of alternating minimum maximum elements ``using` `System; ``using` `System.Collections.Generic;`` ` `class` `GFG``{``    ``// Function to rearrange a given list such that it``    ``// consists of alternating minimum maximum elements``    ``// using List``    ``public` `static` `void` `alternateSort(List<``int``> ll) ``    ``{``        ``ll.Sort();``         ` `        ``for` `(``int` `i = 1; i < (ll.Count + 1)/2; i++)``        ``{``            ``int` `x = ll[ll.Count-1];``            ``ll.RemoveAt(ll.Count-1);``            ``ll.Insert(2*i - 1, x);``        ``}``        ``foreach``(``int` `a ``in` `ll)``        ``{``            ``Console.Write(a+``" "``);``        ``}``         ` `    ``}``     ` `    ``// Driver code``    ``public` `static` `void` `Main (String[] args)``    ``{``        ``// input list``        ``int` `[]arr = {1, 3, 8, 2, 7, 5, 6, 4};``         ` `        ``// convert array to List``        ``List<``int``> ll = ``new` `List<``int``>(arr);``         ` `        ``// rearrange the given list``        ``alternateSort(ll);``    ``}``}`` ` `/* This code contributed by PrinciRaj1992 */`

Output:
```1 8 2 7 3 6 4 5
```

This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up