# CREATE OWN LIBRARY

## Minmax Sort

Back to Programming

### Description

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:

### Algorithm

``````INPUT: An unsorted array [98,34,12,.........,n]

OUTPUT: A sorted array[12,34,...............,n]

PROCESS:

Step 1: [Take the inputs from the user]

For i=0 to n repeat

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.
``````

### Code

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.

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.