Skip to content
Related Articles

Related Articles

Improve Article

A sorting algorithm that slightly improves on selection sort

  • Difficulty Level : Easy
  • Last Updated : 29 Apr, 2021

As we know, selection sort algorithm takes the minimum on every pass on the array, and place it at its correct position.
The idea is to take also the maximum on every pass and place it at its correct position. So in every pass, we keep track of both maximum and minimum and array becomes sorted from both ends.

Examples: 

First example: 7 8 5 4 9 2 
Input :pass 1:|7 8 5 4 9 2| 
       pass 2: 2|8 5 4 7|9
       pass 3: 2 4|5 7|8 9       
Output :A sorted array:  2 4 5 7 8 9

second example: 23 78 45 8 32 56 1      
Input :pass 1:|23 78 45 8 32 56 1|
       pass 2: 1|23 45 8 32 56 |78
       pass 3: 1 8|45 23 32|56 78
       pass 4: 1 8 23 |32|45 56 78
       in a case of odd elements, so one element
       left for sorting, so sorting stops and the
       array is sorted.
Output : A sorted array: 1 8 23 32 45 56 78

C++




// C++ program to implement min max selection
// sort.
#include <iostream>
using namespace std;
 
void minMaxSelectionSort(int* arr, int n)
{
    for (int i = 0, j = n - 1; i < j; i++, j--) {
        int min = arr[i], max = arr[i];
        int min_i = i, max_i = i;
        for (int k = i; k <= j; k++)  {
            if (arr[k] > max) {
                max = arr[k];
                max_i = k;
            } else if (arr[k] < min) {
                min = arr[k];
                min_i = k;
            }
        }
 
        // shifting the min.
        swap(arr[i], arr[min_i]);
 
        // Shifting the max. The equal condition
        // happens if we shifted the max to arr[min_i]
        // in the previous swap.
        if (arr[min_i] == max)
            swap(arr[j], arr[min_i]);
        else
            swap(arr[j], arr[max_i]);
    }
}
 
// Driver code
int main()
{
    int arr[] = { 23, 78, 45, 8, 32, 56, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    minMaxSelectionSort(arr, n);
    printf("Sorted array:\n");
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
    return 0;
}

Java




// Java program to implement min max selection
// sort.
class GFG
{
static void minMaxSelectionSort(int[] arr, int n)
{
    for (int i = 0, j = n - 1; i < j; i++, j--)
    {
        int min = arr[i], max = arr[i];
        int min_i = i, max_i = i;
        for (int k = i; k <= j; k++)
        {
            if (arr[k] > max)
            {
                max = arr[k];
                max_i = k;
            }
             
            else if (arr[k] < min)
            {
                min = arr[k];
                min_i = k;
            }
        }
 
        // shifting the min.
        swap(arr, i, min_i);
 
        // Shifting the max. The equal condition
        // happens if we shifted the max to arr[min_i]
        // in the previous swap.
        if (arr[min_i] == max)
            swap(arr, j, min_i);
        else
            swap(arr, j, max_i);
    }
}
 
static int[] swap(int []arr, int i, int j)
{
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    return arr;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 23, 78, 45, 8, 32, 56, 1 };
    int n = arr.length;
    minMaxSelectionSort(arr, n);
    System.out.printf("Sorted array:\n");
    for (int i = 0; i < n; i++)
        System.out.print(arr[i] + " ");
    System.out.println("");
}
}
 
// This code is contributed by Princi Singh

Python3




# Python3 program to implement min
# max selection sort.
 
def minMaxSelectionSort(arr, n):
    i = 0
    j = n - 1
    while(i < j):
        min = arr[i]
        max = arr[i]
        min_i = i
        max_i = i
        for k in range(i, j + 1, 1):
            if (arr[k] > max):
                max = arr[k]
                max_i = k
            elif (arr[k] < min):
                min = arr[k]
                min_i = k
         
        # shifting the min.
        temp = arr[i]
        arr[i] = arr[min_i]
        arr[min_i] = temp
 
        # Shifting the max. The equal condition
        # happens if we shifted the max to
        # arr[min_i] in the previous swap.
        if (arr[min_i] == max):
            temp = arr[j]
            arr[j] = arr[min_i]
            arr[min_i] = temp
        else:
            temp = arr[j]
            arr[j] = arr[max_i]
            arr[max_i] = temp
 
        i += 1
        j -= 1
 
    print("Sorted array:", end = " ")
    for i in range(n):
        print(arr[i], end = " ")
 
# Driver code
if __name__== '__main__':
    arr = [23, 78, 45, 8, 32, 56, 1]
    n = len(arr)
    minMaxSelectionSort(arr, n)
     
# This code is contributed by
# Surendra_Gangwar

C#




// C# program to implement min max selection
// sort.
using System;
 
class GFG
{
static void minMaxSelectionSort(int[] arr, int n)
{
    for (int i = 0, j = n - 1;
             i < j; i++, j--)
    {
        int min = arr[i], max = arr[i];
        int min_i = i, max_i = i;
        for (int k = i; k <= j; k++)
        {
            if (arr[k] > max)
            {
                max = arr[k];
                max_i = k;
            }
             
            else if (arr[k] < min)
            {
                min = arr[k];
                min_i = k;
            }
        }
 
        // shifting the min.
        swap(arr, i, min_i);
 
        // Shifting the max. The equal condition
        // happens if we shifted the max to arr[min_i]
        // in the previous swap.
        if (arr[min_i] == max)
            swap(arr, j, min_i);
        else
            swap(arr, j, max_i);
    }
}
 
static int[] swap(int []arr, int i, int j)
{
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    return arr;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 23, 78, 45, 8, 32, 56, 1 };
    int n = arr.Length;
    minMaxSelectionSort(arr, n);
    Console.Write("Sorted array:\n");
    for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
    Console.WriteLine("");
}
}
 
// This code is contributed by Rajput-Ji

Javascript




<script>
 
// Javascript program to implement min
// max selection sort.
function swap(arr, xp, yp)
{
    var temp = arr[xp];
    arr[xp] = arr[yp];
    arr[yp] = temp;
}
 
function minMaxSelectionSort(arr, n)
{
    for(var i = 0, j = n - 1; i < j; i++, j--)
    {
        var min = arr[i];
        var max = arr[i];
        var min_i = i;
        var max_i = i;
         
        for(var k = i; k <= j; k++)
        {
            if (arr[k] > max)
            {
                max = arr[k];
                max_i = k;
            }
            else if (arr[k] < min)
            {
                min = arr[k];
                min_i = k;
            }
        }
 
        // Shifting the min.
        swap(arr, i, min_i);
 
        // Shifting the max. The equal condition
        // happens if we shifted the max to arr[min_i]
        // in the previous swap.
        if (arr[min_i] == max)
            swap(arr, j, min_i);
        else
            swap(arr, j, max_i);
    }
}
 
// Driver code
var arr = [ 23, 78, 45, 8, 32, 56, 1 ];
var n = 7;
 
minMaxSelectionSort(arr, n);
 
document.write("Sorted array:\n");
for(var i = 0; i < n; i++)
    document.write(arr[i] + " ");
     
document.write("<br>");
 
// This code is contributed by akshitsaxenaa09
 
</script>

Output: 

 Sorted array: 1 8 23 32 45 56 78

This article is contributed by Shlomi Elhaiani. 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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :