📜  一种排序算法,在选择排序上略有改进

📅  最后修改于: 2021-05-04 18:32:09             🧑  作者: Mango

众所周知,选择排序算法在数组的每次通过中取最小值,并将其放置在正确的位置。

这个想法是在每次通过时也取最大值,并将其放置在正确的位置。因此,在每一遍中,我们都同时跟踪最大值和最小值,并且数组从两端开始进行排序。

例子:

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 
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
# Python 3 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


输出:

Sorted array: 1 8 23 32 45 56 78