The **minmax sort** can be described as a **variation of selection sort**. To arrange the elements in **ascending order in selection sort**, the **maximum element is found** out from the array and **positioned at the last index**(i.e. Its sorted position) .

In the second pass, the **2nd largest element will be found** out and **placed** **in** **its sorted position**, and so on. In the case of minmax sort in the first pass, the max element is found out and placed at its sorted position, in the same pass the minimum element is found out and placed in its sorted position (i.e. At index 0). In the second pass, the second largest and 2nd smallest element is found out and placed at their sorted position and so on till we get the sorted array.

It is a **comparison based**** sorting** technique. The algorithm is an **in-place**** sorting** algorithm as it does not require any extra space to sort the array.

**PROCEDURE WITH EXAMPLE:**

Suppose there are **6 elements** array as follows:

The array is to be sorted in **ascending order**.

**Step 1:**

In this pass, the **maximum element is ****98** and the **minimum element is ****12**. The **sorted position for the maximum element is ****index 5** and for the **minimum element is ****0**. So, in this case for the maximum element **98**, it will be **swapped with ****37**.

In the same pass, the **minimum element** will also be found out. In this case, the **minimum element is ****12** whose **sorted position will be ****0**. So **12**** and ****43**** will be swapped** and after the first pass the array will be:

**Step 2**:

Before the 2nd pass the array is:

Now in the **2nd pass** the **2nd highest** and **2nd lowest** item should be **found out** and **placed in its proper sorted position**. In this case, the **2nd largest element** of the array is **76 **which is to be placed at its sorted position i.e. The second last index of the array. So **76**** and ****37**** will be swapped**. The array will be:

Now, the **2nd smallest**** element** of the array is **34**. This element is to be placed at the 2nd position of the array. So, **34**** will be swapped with ****43.** After completing the 2nd pass the array will be

**Step 3:**

Before starting step 3 the array is:

Here the **3rd largest element** is **43**, the sorted position of 43 is 3rd last position of the array. **Here, it is already in its sorted position** so there will be **no swapping**.

Similarly, the **3rd smallest element is ****37** which is to be placed at the 3rd position of the array. The **element is already placed** there so there will also be **no swapping**. That means we get the sorted array as:

That means we get the **sorted array** as:

```
INPUT: An unsorted array [98,34,12,.........,n]
OUTPUT: A sorted array[12,34,...............,n]
PROCESS:
Step 1: [Take the inputs from the user]
Read n
For i=0 to n repeat
Read arr[i]
Step 2: [Sort the array elements]
Set j<-n-1
For i=0 to j-1 repeat
Set min<-max<-arr[i]
Set min_indx<-max_indx<-i
For k=i to j repeat
If arr[k]>max then
Set max<-arr[k]
Set max_indx<-k
[End of ‘if’]
else if arr[k]<min then
Set min<-arr[k]
Set min_indx<-k
[End of ‘Else if’]
[End of ‘for’ loop]
Swap arr[i] with arr[min_indx]
If arr[min_indx]=max then
Swap arr[j] with arr[min_indx]
else
Swap arr[j] with arr[max_indx]
Step 3:[Display the sorted array]
For i=0 to n repeat
Print arr[i]
Step 4: Stop.
```

__CHARACTERISTICS:__

**1.** It is a comparison-based sorting algorithm.

**2.** The algorithm is a stable sorting algorithm.

**3.** It is a variation of selection sort in which in a single pass both the maximum and minimum element is found out and placed at their sorted position.

**4.** It reduces the number of passes required to sort the elements of the array.

** **

__TIME COMPLEXITY:__

The **number of comparisons** required to sort the elements are:

__SPACE COMPLEXITY:__

The **space complexity** of this algorithm is **O(1)** as it does not require any extra space to sort the array elements.

__ADVANTAGES:__

**1.** It is an in-place sorting algorithm so no extra spaces are required to sort the data.

**2.** The number of passes required to sort data is reduced.

**3.** Faster than selection sort.

__DISADVANTAGES:__

**1.** It is less efficient than other comparison sorts like merge sort or quick sort algorithm.

Related

- Bubble Sort
- Selection Sort
- Insertion Sort
- Quick Sort using recursion
- Quicksort Iterative
- Merge Sort using recursion
- Merge Sort Iterative
- 3 way Merge Sort
- Heap Sort using recursion
- Heap Sort Iterative
- Radix Sort for positive integer
- Bucket Sort
- Counting Sort
- Cocktail Sort
- Intro Sort
- List Sort
- Tree Sort
- Shell Sort
- Gnome sort
- Comb Sort
- Bitonic Sort
- Tim Sort

Contributed by